# Cursor based pagination

**URL:** <https://community.apollographql.com/t/cursor-based-pagination/867>\
**Category:** Client SDKs\
**Tags:** client, server, web, mobile\
**Created:** [July 26, 2021, 9:09am UTC](https://community.apollographql.com/t/cursor-based-pagination/867 "2021-07-26T09:09:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![redderick2](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@redderick2](https://community.apollographql.com/u/redderick2)\
**Post date:** [July 26, 2021, 9:09am UTC](https://community.apollographql.com/t/cursor-based-pagination/867/1 "2021-07-26T09:09:09Z")

</div>

I was going through the [apollo docs](https://www.apollographql.com/docs/react/pagination/cursor-based/) for cursor based pagination and I assumed that these fields were given to us:

```auto
   edges {
        node {
          author
          text
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }

```

That is because there is no exact mention on the page that this has to be created separately in the backend. However, it makes sense. Does apollo at least provide an example showing how this would work? It isn’t exactly intuitive.

---

<div class="post-metadata">

**Author:** ![hwillson](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/hwillson/32/9_2.png) [@hwillson](https://community.apollographql.com/u/hwillson)\
**Post date:** [July 26, 2021, 2:49pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/2 "2021-07-26T14:49:23Z")

</div>

Thanks for calling this out @redderick2. This style of cursor/connection based pagination was made popular in the GraphQL space by [Relay](https://relay.dev), and is outlined in their [GraphQL Cursor Connections Specification](https://relay.dev/graphql/connections.htm) doc. We mention this a bit further down in the docs, but we could be more clear about this overall so we’ll get things adjusted.

---

<div class="post-metadata">

**Author:** ![redderick2](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@redderick2](https://community.apollographql.com/u/redderick2)\
**Post date:** [July 27, 2021, 5:43am UTC](https://community.apollographql.com/t/cursor-based-pagination/867/3 "2021-07-27T05:43:21Z")

</div>

Thank you. I’d love to see an example, even if it is using prefilled data, it would make a night and day difference. So far I love everything the apollo community is doing and thank you guys for making such a great service.

---

<div class="post-metadata">

**Author:** ![hwillson](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/hwillson/32/9_2.png) [@hwillson](https://community.apollographql.com/u/hwillson)\
**Post date:** [July 29, 2021, 12:51pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/4 "2021-07-29T12:51:23Z")

</div>

Thanks @redderick2, great suggestion (cc @StephenBarlow).

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 25, 2022, 7:40pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/5 "2022-05-25T19:40:55Z")

</div>

Hello Team and @hwillson , I was going through the docs and had a query related to Cursor based pagination, I checked that there is already an existing query related to this and hence wanted to pitch in.

Is the Cursor Based Pagination [GraphQL Cursor Connections Specification](https://relay.dev/graphql/connections.htm) only a part of Apollo React Client or it is also supported for Apollo Kotlin Client.

I have seen some of the graphQL-java implementations are also supporting this. I couldn’t find a sample example here and thought of checking whether the concept of wrapping it in Edges and PageInfo attributes is also supported in Apollo Client for Kotlin.

If yes then i would really appreciate if some-one can provide a sample on how to use it using Apollo Kotlin Client.

Appreciate your help and thanks in Advance.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [May 25, 2022, 9:28pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/6 "2022-05-25T21:28:33Z")

</div>

Hi @ashuspeed 👋

You can query relay connection with Apollo Kotlin like any other GraphQL object:

```nohighlight
query {
  connection {
    edges {
      node {}
    }
    pageInfo {
      hasNextPage
    }
  }
}

```

You can then use them from Kotlin with something like this:

```nohighlight
connection.edges[0].node
connection.pageInfo.hasNextPage

```

This usually works well albeit a bit verbose. Can you point to the existing graphql-java sample you were mentioning? Maybe I can point you to the good direction?

Cheers,

Martin

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 26, 2022, 4:51am UTC](https://community.apollographql.com/t/cursor-based-pagination/867/7 "2022-05-26T04:51:19Z")

</div>

Thank You @mbonnin for sharing the details, i was searching the docs in Apollo Client and found Cursor based pagination in [Apollo React Client](https://www.apollographql.com/docs/react/v2/data/pagination/#the-connection-directive) but the same example i couldn’t find in Pagination section for [Apollo Kotlin Client](https://www.apollographql.com/docs/kotlin/tutorial/07-paginate-results) .

I have seen this being supported in DGS Framework as well as in graphql-java-codegen.

Thank You

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [May 26, 2022, 8:02am UTC](https://community.apollographql.com/t/cursor-based-pagination/867/8 "2022-05-26T08:02:33Z")

</div>

Hi @ashuspeed !

If you’re looking for Normalized Cache stable keys like their [Apollo Client](https://www.apollographql.com/docs/react/v2/data/pagination/#the-connection-directive) counterparts then we unfortunately don’t have anything like that just yet in Apollo Kotlin ([but there are plans to!](https://github.com/apollographql/apollo-kotlin/issues/3807))

As far as DGS Framework and graphql-java-codegen are concerned, these are mostly server frameworks. So they are working quite differently and I don’t think it applies to Apollo Kotlin? Or did you have something specific in mind?

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 26, 2022, 10:32am UTC](https://community.apollographql.com/t/cursor-based-pagination/867/9 "2022-05-26T10:32:31Z")

</div>

Hi @mbonnin i was evaluating if i can use Apollo client in Java and so far i really liked the set of features that Apollo Client support.

To evaluate the exhaustive set of functionality, i was comparing it with how can i leverage the features described in GraphQL Server spec.

Most of our use-case is Java based and based on the documentation, i think we can use it by generating Java Classes, I came across the pagination feature and looked at how it has to be used then came across GraphQL cursor connection spec to see what has been supported in Apollo Kotlin Client.

Can you share the information or documents if any about how the existing users use it on Java Platform. I would be really happy to explore more about it and if possible to contribute to something.

Please do let me know if you want me to raise a new query on forum rather than using this one.

This was closely related to something i was exploring so pitched in right here but at the broader level would like to know how users use it in Java ?

Appreciate your help.  
Thank You in Advance.

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [May 26, 2022, 1:04pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/10 "2022-05-26T13:04:52Z")

</div>

Thanks for the details!!

If you’re targeting Java, I would recommend using version 2.x. There’s a ‘getting started’ there: [Get started with Java - Apollo GraphQL Docs](https://www.apollographql.com/docs/kotlin/v2/essentials/get-started-java)

Version 3 is focused at Kotlin and while it’s usable from Java, some of the APIs are less idiomatic. We’re planning to improve this, you can follow [this issue](https://github.com/apollographql/apollo-kotlin/issues/3694) for updates.

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 26, 2022, 5:00pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/11 "2022-05-26T17:00:58Z")

</div>

Thank You @mbonnin for the pointers, let me explore this. I really appreciate your help.

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 30, 2022, 2:59pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/12 "2022-05-30T14:59:23Z")

</div>

Hi @mbonnin, i am evaluating the android version 2.x client, so far its looking good, is there any functionality or performance difference between version 2 and version 3, if there are any can you please share the details a bit.

Thanks,  
Ashudeep

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [May 30, 2022, 3:17pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/13 "2022-05-30T15:17:44Z")

</div>

Hi @ashuspeed !

You can read about what’s new in version 3 in [this blog post](https://www.apollographql.com/blog/announcement/introducing-apollo-kotlin/). A lot of the changes are for Kotlin APIs and multiplatform support so from Java you shouldn’t miss much. There are a few other, in particular reading from the SQL cache has been sped up a bunch and there’s support for new directives.

HTH,

–  
Martin

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/ashuspeed/32/2422_2.png) [@ashuspeed](https://community.apollographql.com/u/ashuspeed)\
**Post date:** [May 30, 2022, 6:57pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/14 "2022-05-30T18:57:41Z")

</div>

Thank You @mbonnin , this will be really helpful.

---

<div class="post-metadata">

**Author:** ![vade](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/vade/32/5050_2.png) [@vade](https://community.apollographql.com/u/vade)\
**Post date:** [April 25, 2024, 4:18pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/15 "2024-04-25T16:18:06Z")

</div>

Hi friends.

I’m look for some advice on cursor based pagination with Apollo iOS for scrolling views (im actually on desktop Mac).

Are there good examples that show driving a data source driven collection view with paginated items, responding to scroll events and delegate methods to fetch the next or previous cursor correctly?

Thanks!

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [April 25, 2024, 4:28pm UTC](https://community.apollographql.com/t/cursor-based-pagination/867/16 "2024-04-25T16:28:18Z")

</div>

Hi @vade 👋 if you haven’t seen it yet, check out the Apollo iOS Pagination module:

> **[Pagination](https://www.apollographql.com/docs/ios/pagination/introduction/)**

It’s a flexible API that supports several common pagination patterns (including cursors). Hope that helps!
