GraphQL API Pagination

I have a GraphQL query to search repositories and I want to paginate my result.

search(first: $first, after:$cursor, query: $query, type: $type) {
    edges {
        node {
            ... on Repository {
                name
                description
                forkCount
                owner {
                    avatarUrl
                }
            }
        }
    }
    pageInfo {
        hasNextPage
        endCursor
    }
}

The query works fine when provided with a cursor. When I want to get only the first page and provide the query with a null cursor to get the first page, I get " Variable cursor of type String! was provided invalid value"  error.

How can I retrieve the first page without having to write another query with no cursor parameter?

Any help would be appreciated.

2 Likes

Hi @tartarjr,

Thanks for being here! If I’m not mistaken you’d have to send the initial request again because no variables are specified :frowning: Let me do a little digging on my end and see if there are any alternatives. 

1 Like

Hi Andrea 

Thank you for your help. Please let me know if you can find any alternatives.

Hey @tartarjr,

Thanks for your patience, looks like the query works fine on our end take a look:

Would you please attempt you query again making sure to define the $after cursor parameter as $after: String. Please let me know if this does the trick, I hope this helps!

3 Likes

Just remove the ! from String to make the variable accept either a String or a null value.

5 Likes

Hey @andreagriffiths11 

I was working on the search option to list all the available repositories using github api. The search option has an first field and has a limit of 100, iam trying to fetch 500 repositories, is there an alternative to do that ?

Code:

query {
rateLimit{
cost
remaining
resetAt
}
search( query: “-js created:>2015-01-01 language:JavaScript”, type:REPOSITORY ,first: 500) {
repositoryCount
pageInfo {
hasNextPage
endCursor
startCursor
}
edges {
node {
… on Repository {
name
url
}
}
}
}

}

“message”: “Requesting 500 records on the search connection exceeds the first limit of 100 records.”,
“type”: “EXCESSIVE_PAGINATION”,

Thank you