简体   繁体   中英

Prisma cursor based pagination

Just finished the excellent tutorial on building Prisma based graphql backend. They explain how to implement first-offset pagination here https://www.howtographql.com/graphql-js/8-filtering-pagination-and-sorting/ .

Now I am wondering, how to implement cursor-based pagination?

Here are my types (they are the same as in tutorial):

type User
    implements Node {
    id: ID!
    name: String!
    email: String!
    password: String!
    links(...): [Link!]
    votes(...): [Vote!]
}

type Link
    implements Node {
    id: ID!
    createdAt: DateTime!
    description: String!
    url: String!
    postedBy(...): User
    votes(...): [Vote!]
}

In the playground I am trying to query user information along with the links, created by the user:

{
  user(where: {id:"cjimzqrshb3nf0c29z1p7km0j"}) {
    email
    links {
      id
      url
      description
    }
  }
}

It gives me back all the links, created by the user. How can I paginate them? Links object does not have paging information while linksConnection does not fit inside user object.

You can query the linksConnection related to the user, and therefore access cursors :

{
  linksConnection(where:{user:{id:"cjimzqrshb3nf0c29z1p7km0j"}}){
     pageInfo{
       endCursor
       startCursor
     }
     edges{
       cursor
     }
  }
}

You can implement cursor based pagination with Prisma like this:

{
  users{
    links(first: 10, after:"some-id") {
      description
    }
  }
}

This is possible because the id and the cursor is the same.

Alternatively you can use offset based pagination like this:

{
  users{
    links(first: 10, skip: 30) {
      description
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM