Pagination with cursor in GraphQL API

Hi all,

I’m using the datacite commons GraphQL API https://api.datacite.org/graphql and noticed that when querying a connection between two objects I only get 25 datasets returned (default). Pagination and cursor are supposedly working on datacite commons (answer I got yesterday at a datacite talk), but every time I add a cursor to a query, I get a DOWNSTREAM_SERVICE_ERROR.

I followed this description https://graphql.org/learn/pagination/ where the cursor is added to an edge and the UI on the GraphQL API also suggests a cursor at this point, so it is adhering to the provided schema.

Here is one example of a query with a cursor (not working) but the same occurs on every query I tried:

 {
 organization(id: "https://ror.org/04qmmjx98") {
  id

people {
  edges {
    cursor
    node {
      id
    }
  }
}
}
}

So what am I doing wrong? How do I get all the connected data from the GraphQL API?

Sandram,

pagination via cursor is the general recommendation for GraphQL, and is also how it is implemented in the DataCite GraphQL API.

In your case I would start with a query that checks for the number of people found:

{
  organization(id: "https://ror.org/04qmmjx98") {
    id
    people {
     totalCount
    }
  }
}

This query returns 434 people.

For your paginated results you need pageInfo, which return the cursor for the next query:

{
  organization(id: "https://ror.org/04qmmjx98") {
    id
    people {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        id
        givenName
        familyName
      }
    }
  }
}

This query returns endCursor: "MQ". To get the next set of results using this cursor, use the after property:

{
  organization(id: "https://ror.org/04qmmjx98") {
    id
    people(after: "MQ") {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        id
        givenName
        familyName
      }
    }
  }
}

You can stop paginating if hasNextPage returns false. Let me know if that doesn’t solve it for you.

1 Like