Pagination in apollo-graphql server
Asked Answered
D

1

6

I want to implement cursor based pagination in Apollo graphql server. I have prepared schema with pagination requirement. But i am stuck at resolver side. Here is my schema

const typeDefinitions = `
input CreateDeveloperInput {
  # An arbitrary string value with no semantic meaning. Will be included in the
  # payload verbatim. May be used to track mutations by the client.
  clientMutationId: String
  developer: DeveloperInput!
}

type CreateDeveloperPayload {
  clientMutationId: String
  developerEdge(orderBy: DevelopersOrderBy = PRIMARY_KEY_ASC): DevelopersEdge
  query: Query
}
input DeleteDeveloperByIdInput {
  # An arbitrary string value with no semantic meaning. Will be included in the
  # payload verbatim. May be used to track mutations by the client.
  clientMutationId: String
  id: Int!
}

input DeleteDeveloperInput {
  clientMutationId: String
  nodeId: ID!
}

type DeleteDeveloperPayload {
  clientMutationId: String
  developer: Developer
  deletedDeveloperId: ID

  # Our root query field type. Allows us to run any query from our mutation payload.
  query: Query
}

type Developer implements Node {
  nodeId: ID!
  id: Int!
  name: String!
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperCondition {
  id: Int
  name: String
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperInput {
  id: Int
  name: String!
  place: String
  salary: Int
  joiningDate: String
}

input DeveloperPatch {
  id: Int
  name: String
  place: String
  salary: Int
  joiningDate: String
}

type DevelopersConnection {
  # Information to aid in pagination.
  pageInfo: PageInfo!
  totalCount: Int
  edges: [DevelopersEdge]
  nodes: [Developer!]
}

type DevelopersEdge {
  # A cursor for use in pagination.
  cursor: String
  node: Developer!
}

enum DevelopersOrderBy {
  PRIMARY_KEY_ASC
  PRIMARY_KEY_DESC
  NATURAL
  ID_ASC
  ID_DESC
  NAME_ASC
  NAME_DESC
  PLACE_ASC
  PLACE_DESC
  SALARY_ASC
  SALARY_DESC
  JOINING_DATE_ASC
  JOINING_DATE_DESC
}

# The root mutation type which contains root level fields which mutate data.


interface Node {
  # A globally unique identifier. Can be used in various places throughout the system to identify this single value.
  nodeId: ID!
}

# Information about pagination in a connection.
type PageInfo {
  # When paginating forwards, are there more items?
  hasNextPage: Boolean!

  # When paginating backwards, are there more items?
  hasPreviousPage: Boolean!

  # When paginating backwards, the cursor to continue.
  startCursor: String

  # When paginating forwards, the cursor to continue.
  endCursor: String
}

# The root query type which gives access points into the data universe.
type Query implements Node {
   allDevelopers(

    # Read all values in the set before (above) this cursor.
    before: String,

    # Read all values in the set after (below) this cursor.
    after: String, first: Int, last: Int, offset: Int,

    # A condition to be used in determining which values should be returned by the collection.
    condition: DeveloperCondition): DevelopersConnection

  # Exposes the root query type nested one level down. This is helpful for Relay 1
  # which can only query top level fields if they are in a particular form.
  
  nodeId: ID!
}
schema {
query: Query
}

`;

export default [typeDefinitions];

Is it possible to resolve in resolvers? If yes, can any one please tell me how to implement it

Disturbance answered 6/6, 2017 at 7:11 Comment(1)
The implementation will depend on what database you are using.Abiding
K
0

If you're using Mongo and Mongoose with Apollo Express GraphQL I have found three ways to do pagination:

  1. You can create a cursor field on your schema and implement your resolver with the pagination logic, but I don't recommend this method if you have a complex schema that contain unions, interfaces and different nested objects on the type, but if you want to implement it by yourself here's the reference

  2. You can use a module which will nest your Mongoose model and provide you an pagination interface, but this requires a few changes on your schema and resolvers, more work then the third method

  3. You can use sort, skip and limit from Mongo / Mongoose, but if you do this by yourself you can find some issues, e.g if the document was sorted by a date key which can have different documents with the same date it can duplicate documents and mess up with your pagination, so I recommend that you install a pagination plugin on your Mongoose schema and use it like any other method from Mongoose

Karilynn answered 16/4, 2020 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.