← Winnitude! Search  ·  Reader Mode (1208 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 1 LINKS

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

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 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. feat(api): paginate any GraphQL connection of the schema -- the generic connection and the gl.graphql property. 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

Skip to content

Repository navigation