why would gh api graphql --paginate -f query='{ organization(login: "ORG") {…
- User
why would
gh api graphql --paginate -f query='{ organization(login: "ORG") { membersWithRole(first: 100, after: $endCursor) { totalCount edges { node { login name email } } pageInfo { endCursor hasNextPage } } } }'
return the error "gh: Variable $endCursor is used by anonymous query but not declared"
Assistant
This error occurs because of how the
gh api graphql --paginateflag works.Here's the breakdown:
-
GraphQL Variable Declaration: In a standard GraphQL query that uses variables, you need to declare them at the top level of your query operation. For example:
query GetMembers($endCursor: String) { # <-- Variable $endCursor declared here organization(login: "ORG") { membersWithRole(first: 100, after: $endCursor) { # ... } } }Your query is an "anonymous query" (it doesn't have
query NameOfQuery(...) { ... }), and it uses$endCursorbut doesn't declare it. -
gh api graphql --paginate's Role: The entire purpose of the--paginateflag is for theghCLI to automatically handle theaftercursor for you.- It takes your base query.
- For the first request, it sends the query without an
afterargument (or withafter: nullif you included it). - It receives the
pageInfo { endCursor, hasNextPage }. - If
hasNextPageis true, it then modifies your query internally for the next request, adding theafter: "the_actual_cursor_value"argument to the relevant field (in this case,membersWithRole). - It continues this process until
hasNextPageis false.
The Problem:
You've put
$endCursordirectly into your query string. This tells the GraphQL server (andgh's pre-processor) that you intend to pass a variable namedendCursor. However, since you're not declaring that variable in the query itself, it's an undeclared variable.The
ghCLI expects you to provide the query without the variable for theafterargument. It will inject the concreteafter: "cursor_value"argument directly into your query for each subsequent page.The Solution:
Remove
after: $endCursorfrom your query. Letghhandle that part entirely.Corrected Command:
gh api graphql --paginate -f query='{ organization(login: "ORG") { membersWithRole(first: 100) { # <-- Remove after: $endCursor here totalCount edges { node { login name email } } pageInfo { endCursor hasNextPage } } } }'This will now work as expected, with
ghautomatically fetching all pages of members.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.