Repository navigation
feat: add a minimal GraphQL client #2967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
| assert response["currentUser"]["active"] is True |
This file contains hidden or 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 hidden or 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 hidden or 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.