Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realtime pagination in GraphQL

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Arnaud Rinquin Arnaud Rinquin
September 05, 2017

Realtime pagination in GraphQL

We review the canonical way of doing pagination in GraphQL and explore a solution to make it work realtime with subscriptions.

Avatar for Arnaud Rinquin

Arnaud Rinquin

September 05, 2017
Tweet

Other Decks in Technology

Transcript

  1. // TODO 1. offset&limit is not ok 2. cursor based

    pagination 3. the case of realtime
  2. Uses cases • large number of sorted items • frequent

    additions to the list • no full refresh from navigation
  3. Cursor.based.navigation Instead of offsets and index, use cursors relative to

    the items. Basically: /api/users?after={lastUserOfTheList.id}&first=10
  4. Cursor.based.navigation.PROPER • give the clients the nextPageCursor • hide the

    cursor's meaning • give all the information a client might need: • total count • hasNext
  5. // how to build cursors endCursor = base64encode(item.createdAt) // how

    to use decode and use the after param afterCreatedAt = base64decode(params.after) psql.get(‘items’, where: { createdAt: { greaterThen: afterCreatedAt, }, limit: params.first, })
  6. The real deal: how to update already fetched lists All

    we have are event base subscriptions subscription { newsFeedItemAdded: NewsFeedItem } Non-options: • Refetch the whole list • Duplicate ordering / filtering in the client
  7. Known limits and work-arounds What if you get out-of-sync? We

    refetch all the things What if there are params to your the lists? We could add mirroring params to the subscription