feat(api): paginate GraphQL connections and add gl.wikis() by bufferoverflow · Pull Request #3477 · python-gitlab/python-gitlab
Repository navigation
feat(api): paginate GraphQL connections and add gl.wikis() - #3477
Open
bufferoverflow wants to merge 3 commits into main from feat/graphql-wikis-pagination
Open
feat(api): paginate GraphQL connections and add gl.wikis() #3477 bufferoverflow wants to merge 3 commits into main from feat/graphql-wikis-pagination
Conversation
bufferoverflow commented Dec 1, 2026 · edited Loading
Copy link Copy Markdown
Member
Connections of the GraphQL API return one page at a time, and callers have to
pass the cursor themselves. This adds a generic, cursor-paginated
GraphQLConnection that fetches one page per round-trip, and uses it for the
wiki pages of a project with gl.wikis().
The REST wiki list returns every page of a wiki in a single response, which
times out on a large wiki and cannot be split into batches, while the
project.wikiPages GraphQL field is cursor-paginated.
The branch is split into small commits that can be reviewed (or merged) on
their own, as asked in review:
fix(graphql): raise GitlabGraphQLError instead of the gql error-- GraphQL
errors surfaced asgql.TransportQueryError, which callers cannot catch with
the exceptions of the library. Convert them toGitlabGraphQLErrorin
GraphQL.execute()andAsyncGraphQL.execute(), with the errors of the
server in itserrorsattribute. Also installsgql[httpx]in the test
requirements, which the unit environment needs to import the client.feat(api): paginate any GraphQL connection of the schema-- the generic
connection and thegl.graphqlproperty.feat(api): add gl.wikis() for the wiki pages of a project-- the wiki
helper, built on the generic connection.
Example usage
import gitlab
gl = gitlab.Gitlab("https://gitlab.example.com", private_token="glpat-...")
# One request per page, 50 pages per request at most.
for page in gl.wikis("group/project", page_size=50):
print(page.slug, "-", page.title)
# The content comes back in the same round-trips when you ask for it.
pages = gl.wikis("group/project", page_size=50, content=True, web_url=True)
for page in pages:
print(page.web_url)
print(page.content[:80])
print(f"{len(pages)} pages fetched")
# A single page later on, still REST:
gl.projects.get("group/project").wikis.get("docs/setup")
Changes
Generic pagination
GraphQLConnection is not tied to a field: the document, its variables and the
keys to follow in the result are given to the constructor. One class serves every
connection of the schema, including connections nested under a resource.
from gitlab.graphql import GraphQLConnection
document = """
query($fullPath: ID!, $iid: String!, $after: String) {
project(fullPath: $fullPath) {
mergeRequest(iid: $iid) {
diffs(first: 20, after: $after) {
pageInfo { hasNextPage endCursor }
nodes { newPath }
}
}
}
}
"""
diffs = GraphQLConnection(
gl.graphql,
document,
{"fullPath": "group/project", "iid": "42"},
path=("project", "mergeRequest", "diffs"),
)
for diff in diffs:
print(diff["newPath"])
An optional build callable turns the entries of the server into objects, which
is how WikiPagesConnection returns WikiPage objects. The page size is only
sent when the connection is created with one, and is capped at 100.
Gitlab.graphql exposes the GraphQL client of the connection (URL and auth of
the Gitlab instance, private_token first, then oauth_token), and it is
closed with the connection.
Wiki pages
gl.wikis() is a method of Gitlab rather than of Project, because
project.wikiPages takes the full path of the project, and a Project may have
been created from an id. Project.wikis stays the REST manager.
The content of the pages is only requested on demand, as the server reads it
from Gitaly per page. wikiPages is an experiment GraphQL field; the docs say
so.
Documentation and testing
Please consider whether this PR needs documentation and tests. This is not required , but highly appreciated:
- Documentation in the matching docs section
- Unit tests and/or functional tests
View session
Copilot PR reviewer Copilot
AI
reviewed Dec 1, 2026
View reviewed changes
Copilot
AI
left a comment
Copy link Copy Markdown
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
🟡 Changes recommended
Pagination fails with supported gql 3.5.x before any HTTP request, while aliased fields are mishandled and public execution methods inconsistently convert server query errors.
3 open issues- Pass document and variables separately for gql 3.5 compatibility · New
- Convert transport errors in both execute methods · New
- Preserve GraphQL fields aliased as data or errors · New
This PR adds cursor-based GraphQL pagination to python-gitlab, providing a batched alternative to the unpaginated REST wiki listing.
Changes:
- Adds
GraphQLConnection, a cachedgl.graphqlclient, andgl.wikis()with optional content and web URLs. - Introduces
GitlabGraphQLErrorhandling in the new query helper. - Adds usage documentation, unit tests, and a pinned GraphQL test dependency.
| File | Description |
|---|---|
tests/unit/test_graphql_wikis.py |
Tests pagination, client caching, optional fields, and errors. |
requirements-test.txt |
Adds the GraphQL dependency for tests. |
gitlab/graphql.py |
Defines paginated connections and wiki page models. |
gitlab/exceptions.py |
Adds and exports GitlabGraphQLError. |
gitlab/client.py |
Adds the cached GraphQL client, wiki helper, and query execution wrapper. |
gitlab/__init__.py |
Exposes the GraphQL helper module. |
docs/gl_objects/wikis.rst |
Adds a paginated wiki listing example. |
docs/api/gitlab.rst |
Documents GraphQL clients and helper classes. |
docs/api-usage-graphql.rst |
Explains pagination, custom queries, and error handling. |
Review effort: Balanced
codecov
Bot
commented
Dec 1, 2026
·
edited
Loading
Copy link Copy Markdown
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@ ## main #3477 +/- ## ========================================== + Coverage 95.94% 96.04% +0.09% ========================================== Files 100 102 +2 Lines 6172 6315 +143 ========================================== + Hits 5922 6065 +143 Misses 250 250
Flags with carried forward coverage won't be shown. Click here to find out more.
|
View reviewed changes
nejch left a comment
Copy link Copy Markdown
Member
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
@bufferoverflow this has quite a bit of drive-by stuff added, maybe it could be split into REST / GraphQL parts and then take it bit by bit.
I'm sure AI now can tackle the arbitrary nested pagination issue in graphql here, but would be nice if we can have a generic solution for all types of graphql calls.
bufferoverflow added 3 commits December 2, 2026 07:08
fix(graphql): raise GitlabGraphQLError instead of the gql error
88503ed
Errors returned by the GraphQL API surfaced as gql.TransportQueryError, which callers cannot catch with the exceptions of the library and which is not a GitlabError. Convert them in GraphQL.execute() and AsyncGraphQL.execute(), next to the handling of TransportServerError, so every entry point of the client converts errors the same way. The errors of the server are available in the new errors attribute. The unit tests also need gql to import the client, and it is not part of the requirements, so install the extra with the same pin as requirements.txt.
feat(api): paginate any GraphQL connection of the schema
9ef064e
Connections of the GraphQL API return one page at a time and callers have to pass the cursor themselves. Add GraphQLConnection, which fetches one page per round-trip and stops when the server reports that there are no more entries, and expose the errors-free result of a query as GraphQL.execute_query(). The connection is not tied to a field: the document, its variables and the keys to follow in the result are given to the constructor, so one class serves every connection of the schema, including the ones nested under a resource such as project.mergeRequest.diffs. An optional build callable turns the entries of the server into an object. Expose the GraphQL client of a connection as gitlab.Gitlab.graphql so callers no longer have to rebuild the URL and auth of it, and close it with the connection.
feat(api): add gl.wikis() for the wiki pages of a project
3c7e142
The REST wiki list returns every page of a wiki in one response, which can take long enough on a large wiki to time out and fail as a whole, and the endpoint ignores pagination arguments. Build a WikiPagesConnection on the generic GraphQL connection instead, which fetches the pages in batches and returns WikiPage objects. The project path is a full path rather than an id, which is why the helper is a method of Gitlab and not of Project: a Project may have been created from an id, and Project.wikis is the REST manager. The content of the pages is only requested on demand, as the server reads it from Gitaly per page.bufferoverflow force-pushed the feat/graphql-wikis-pagination branch from
e4119f9 to
3c7e142
Compare
December 2, 2026 06:43
bufferoverflow commented Dec 2, 2026
Copy link Copy Markdown
Member
Author
|
Thanks, agreed on both points -- I reworked the branch in place. Split into small commits. The PR is now three commits on top of
The REST side is only docs: Generic pagination. diffs = GraphQLConnection(
gl.graphql,
document,
{'fullPath': 'group/project', 'iid': '42'},
path=('project', 'mergeRequest', 'diffs'),
)
for diff in diffs:
print(diff['newPath'])
Not in scope yet: aliased connections in a single document ( |
bufferoverflow commented Dec 2, 2026 · edited Loading
Copy link Copy Markdown
Member
Author
|
@nejch Let me know if you like the approach, I can then craft dedicated PRs. |
This branch has not been deployed
No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment