Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

why would gh api graphql --paginate -f query='{ organization(login: "ORG") {…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-mini-beta’s answer.
  1. 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"

  2. Gemini Assistant

    This error occurs because of how the gh api graphql --paginate flag works.

    Here's the breakdown:

    1. 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 $endCursor but doesn't declare it.

    2. gh api graphql --paginate's Role: The entire purpose of the --paginate flag is for the gh CLI to automatically handle the after cursor for you.

      • It takes your base query.
      • For the first request, it sends the query without an after argument (or with after: null if you included it).
      • It receives the pageInfo { endCursor, hasNextPage }.
      • If hasNextPage is true, it then modifies your query internally for the next request, adding the after: "the_actual_cursor_value" argument to the relevant field (in this case, membersWithRole).
      • It continues this process until hasNextPage is false.

    The Problem:

    You've put $endCursor directly into your query string. This tells the GraphQL server (and gh's pre-processor) that you intend to pass a variable named endCursor. However, since you're not declaring that variable in the query itself, it's an undeclared variable.

    The gh CLI expects you to provide the query without the variable for the after argument. It will inject the concrete after: "cursor_value" argument directly into your query for each subsequent page.

    The Solution:

    Remove after: $endCursor from your query. Let gh handle 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 gh automatically fetching all pages of members.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.