Search code examples
graphqlpaginationapollographql-fragments

GraphQL cursor pagination and fragments?


So I jumped in the middle of some project, and I'm supposed to add cursor pagination in GraphQL to a list within an object.

The query (fetches only a single object, within which do we get the data we need to paginate):

export const GET_OBJECT = gql`
  query objectGroup($id: id) {
    objectGroup(objectId: $id) {
      nodes {
        ...fragment1
      }
    }
  }
${FRAGMENT1}
`

Fragment1 roughly looks like this:

export const FRAGMENT1 = gql`
fragment fragment1 on objectGroup {
 // some irrelevant content
 listItems {
  ...fragment2
  someMoreData {
    blah
    blah
  }
 }
}
${FRAGMENT2}
`

And finally, fragment 2 contains individual fields for each item:

export const FRAGMENT2 = gql`
fragment listItem on ListItem {
   id
   name
   size
 }
`

Now, I've analyzed other parts of the app where this kind of pagination is implemented, but it's always added to a query, next to nodes, within pageInfo they added stuff like first, after, hasNextPage, hasPreviousPage (which come from another fragment that contains just those fields).

However, here, as you can see, it's a bit more complex. If I add pageInfo with the accompanying fields to the query, it does nothing, since we're always fetching only one object anyway, and it doesn't affect the part I need.

The logical thing, judging from this structure, seems to be adding some arguments to the listItems within Fragment 1. But if I try adding first or anything like that, I just get a message saying "Field 'listItems' doesn't accept argument 'after'", or if I try adding the pageInfo field anywhere, it says 'Field 'pageInfo' doesn't exist on type 'ListItem'.

And no, I don't have access to the schema or anything backend.

Is there a way to implement this? I'm sorry if it's a stupid question, but it's a very specific situation, and Google wasn't very helpful


Solution

    1. If the backend doesn't support pagination on the objectGroup query you won't be able to add it from the client
    2. Hopefully you can gain access to the schema so that you can explore and see what else is available. If there is a paginated query on your listItem then you could use that.

    Otherwise you'll need to request support from the backend for what you're trying to do.

    Not giving you schema access is pretty evil on the part of the backend. Without a schema or docs you are shooting in the dark.