« Back to Search  |  Reader Mode  |  Switch to Full Retro Site View Links: Stay in Reader  |  Retro Mode: ON  |  Original »
GitHub · Extracted in 982 ms

feat(api): paginate GraphQL connections and add gl.wikis() by bufferoverflow · Pull Request #3477 · python-gitlab/python-gitlab

Source: https://github.com/python-gitlab/python-gitlab/pull/3477

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:

  1. fix(graphql): raise GitlabGraphQLError instead of the gql error -- GraphQL
    errors surfaced as gql.TransportQueryError, which callers cannot catch with
    the exceptions of the library. Convert them to GitlabGraphQLError in
    GraphQL.execute() and AsyncGraphQL.execute(), with the errors of the
    server in its errors attribute. Also installs gql[httpx] in the test
    requirements, which the unit environment needs to import the client.
  2. feat(api): paginate any GraphQL connection of the schema -- the generic
    connection and the gl.graphql property.
  3. 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:

JohnVillalovos requested a balanced review from Copilot December 1, 2026 18:27 Copilot started reviewing on behalf of JohnVillalovos December 1, 2026 18:27
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 What changed in this PR

This PR adds cursor-based GraphQL pagination to python-gitlab, providing a batched alternative to the unpaginated REST wiki listing.

Changes:

  • Adds GraphQLConnection, a cached gl.graphql client, and gl.wikis() with optional content and web URLs.
  • Introduces GitlabGraphQLError handling 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.
✅ Project coverage is 96.04%. Comparing base (c5b345f ) to head (3c7e142 ).

Additional details and impacted files
@@            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              
Flag Coverage Δ
api_func_v4 82.77% <40.97%> (-1.03%) ⬇️
cli_func_v4 77.92% <40.97%> (-0.87%) ⬇️
unit 91.16% <100.00%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
gitlab/__init__.py 100.00% <100.00%> (ø)
gitlab/client.py 98.63% <100.00%> (+0.07%) ⬆️
gitlab/exceptions.py 100.00% <100.00%> (ø)
gitlab/graphql.py 100.00% <100.00%> (ø)
gitlab/v4/objects/__init__.py 100.00% <100.00%> (ø)
gitlab/v4/objects/graphql.py 100.00% <100.00%> (ø)
gitlab/v4/objects/wikis.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics : Detect flaky tests, report on failures, and find test suite problems.
nejch requested changes Dec 2, 2026
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 main, each of which stands on its own:

  1. fix(graphql): raise GitlabGraphQLError instead of the gql error -- GraphQL errors came back as gql.TransportQueryError, which callers cannot catch with the library exceptions. They are converted in GraphQL.execute() and AsyncGraphQL.execute() now, with the server errors on .errors. This commit also carries the gql[httpx] test requirement, because the unit environment cannot import the client without it.
  2. feat(api): paginate any GraphQL connection of the schema -- the generic pagination, plus gl.graphql.
  3. feat(api): add gl.wikis() for the wiki pages of a project -- the wiki helper on top of it.

The REST side is only docs: docs/gl_objects/wikis.rst says the wiki list endpoint ignores pagination, which was already merged separately in #3476 . gl.wikis() is GraphQL only; Project.wikis stays the REST manager.

Generic pagination. GraphQLConnection is no longer tied to a field. The document, its variables and the keys to follow in the result are constructor arguments, so one class handles any connection, including nested ones:

diffs = GraphQLConnection(
    gl.graphql,
    document,
    {'fullPath': 'group/project', 'iid': '42'},
    path=('project', 'mergeRequest', 'diffs'),
)
for diff in diffs:
    print(diff['newPath'])

end_cursor and page_size_variable can be overridden per connection, and an optional build= callable turns entries into objects; WikiPagesConnection is now a thin subclass that passes build=WikiPage. A unit test covers a non-wiki top-level connection (runners) to keep it honest.

Not in scope yet: aliased connections in a single document (a: diffs(...), b: notes(...)). One connection per document is the current model; if you would rather have aliases handled now, I can extend the path to accept an alias instead of a field name.

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.

bufferoverflow requested a review from nejch December 2, 2026 07:13

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

Labels

None yet

3 participants