-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d44ddd2
commit d6b1b0a
Showing
14 changed files
with
210 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
############################ | ||
Using the GraphQL API (beta) | ||
############################ | ||
|
||
python-gitlab provides basic support for executing GraphQL queries and mutations. | ||
|
||
.. danger:: | ||
|
||
The GraphQL client is experimental and only provides basic support. | ||
It does not currently support pagination, obey rate limits, | ||
or attempt complex retries. You can use it to build simple queries and mutations. | ||
|
||
It is currently unstable and its implementation may change. You can expect a more | ||
mature client in one of the upcoming versions. | ||
|
||
The ``gitlab.GraphQL`` class | ||
================================== | ||
|
||
As with the REST client, you connect to a GitLab instance by creating a ``gitlab.GraphQL`` object: | ||
|
||
.. code-block:: python | ||
import gitlab | ||
# anonymous read-only access for public resources (GitLab.com) | ||
gq = gitlab.GraphQL() | ||
# anonymous read-only access for public resources (self-hosted GitLab instance) | ||
gq = gitlab.GraphQL('https://gitlab.example.com') | ||
# personal access token or OAuth2 token authentication (GitLab.com) | ||
gq = gitlab.GraphQL(token='glpat-JVNSESs8EwWRx5yDxM5q') | ||
# personal access token or OAuth2 token authentication (self-hosted GitLab instance) | ||
gq = gitlab.GraphQL('https://gitlab.example.com', token='glpat-JVNSESs8EwWRx5yDxM5q') | ||
Sending queries | ||
=============== | ||
|
||
Get the result of a query: | ||
|
||
.. code-block:: python | ||
query = """{ | ||
query { | ||
currentUser { | ||
name | ||
} | ||
} | ||
""" | ||
result = gq.execute(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
cli-usage | ||
api-usage | ||
api-usage-advanced | ||
api-usage-graphql | ||
cli-examples | ||
api-objects | ||
api/gitlab | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
|
||
import httpx | ||
from gql.transport.httpx import HTTPXTransport | ||
|
||
|
||
class GitlabTransport(HTTPXTransport): | ||
"""A gql httpx transport that reuses an existing httpx.Client. | ||
By default, gql's transports do not have a keep-alive session | ||
and do not enable providing your own session that's kept open. | ||
This transport lets us provide and close our session on our own | ||
and provide additional auth. | ||
For details, see https://github.com/graphql-python/gql/issues/91. | ||
""" | ||
|
||
def __init__(self, *args: Any, client: httpx.Client, **kwargs: Any): | ||
super().__init__(*args, **kwargs) | ||
self.client = client | ||
|
||
def connect(self) -> None: | ||
pass | ||
|
||
def close(self) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
gql==3.5.0 | ||
httpx==0.27.0 | ||
requests==2.32.3 | ||
requests-toolbelt==1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
|
||
import pytest | ||
|
||
import gitlab | ||
|
||
|
||
@pytest.fixture | ||
def gl_gql(gitlab_url: str, gitlab_token: str) -> gitlab.GraphQL: | ||
logging.info("Instantiating gitlab.GraphQL instance") | ||
instance = gitlab.GraphQL(gitlab_url, token=gitlab_token) | ||
|
||
return instance | ||
|
||
|
||
def test_query_returns_valid_response(gl_gql: gitlab.GraphQL): | ||
query = "query {currentUser {active}}" | ||
|
||
response = gl_gql.execute(query) | ||
Check failure on line 19 in tests/functional/api/test_graphql.py GitHub Actions / functional (api_func_v4)
|
||
assert response["currentUser"]["active"] is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
|
||
import gitlab | ||
|
||
|
||
def test_import_error_includes_message(monkeypatch: pytest.MonkeyPatch): | ||
monkeypatch.setattr(gitlab.client, "_GQL_INSTALLED", False) | ||
with pytest.raises(ImportError, match="GraphQL client could not be initialized"): | ||
gitlab.GraphQL() | ||
|
||
|
||
def test_graphql_as_context_manager_exits(): | ||
with gitlab.GraphQL() as gl: | ||
assert isinstance(gl, gitlab.GraphQL) |