Search code examples
androidgraphqlgithub-graphqlapollo-android

GitHub GraphQL cursor Pagination on first request


Hi I am using Apollo android to get a list of repositories with a keyword from the Github GraphQl

I have to add cursor based pagination as well.

This is the .graphql file

  query Search($query: String!,$afterCursor: String!){
  search(query:$query, after:$afterCursor,type: REPOSITORY, first: 50) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          id
          name
          description
          forkCount
          owner{
            login
            id
            avatarUrl
          }
        }
      }
      cursor
    }
    pageInfo {
        endCursor
        hasNextPage
      }
  }
}

What will be the value for the afterCursor variable when the request is sent for the first time

I tried null but the response failed

and I tried empty string as well

Thanks in advance..


Solution

  • If you can remove the ! from $afterCursor: String!, that should work.

    The problem of sending null or empty is that is not a valid cursor, so if we make it not required (by removing ! from the parameters), then you don't have to send it at all, and that works for the first time you run the query.`

    Something like this:

    query Search($query: String!, $afterCursor: String){
      search(query:$query, after:$afterCursor,type: REPOSITORY, first: 50) {
        repositoryCount
        edges {
          node {
            ... on Repository {
              id
              name
              description
              forkCount
              owner{
                login
                id
                avatarUrl
              }
            }
          }
          cursor
        }
        pageInfo {
            endCursor
            hasNextPage
          }
      }
    }