Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow simple limit / offset pagination #247

Open
amarsyla opened this issue Oct 7, 2017 · 2 comments · May be fixed by #535
Open

Allow simple limit / offset pagination #247

amarsyla opened this issue Oct 7, 2017 · 2 comments · May be fixed by #535

Comments

@amarsyla
Copy link

amarsyla commented Oct 7, 2017

I tried to use LIMIT / OFFSET using the first / after arguments for simple pagination, but it's not currently possible with mariadb. Currently, 'after' accepts only a string cursor, but it should also accept an integer for SQL offset.

@leonardfactory
Copy link

Quoting the docs:

The Relay Cursor contains the offset and is therefore predictable. You can produce the cursor for any row in the middle because you can predict the offset value. graphql-relay has a helper for this. (..)

What's really going on with after and the cursor (if I've got the pagination docs right), when you use sqlPaginate: true, is just a simple limit / offset with a base64 encoded cursor as graphql-relay specificates.

You should not worry about it being a string, since:

  • The cursor will be provided to you by the endCursor field in pageInfo; just add it to the query to get it out (so when you paginate you can just use it in the following query)
  • The cursor is just an encoding, and as the example in the docs shows, you can determinate it given an offset usinggraphql-relay function offsetToCursor:
import { offsetToCursor } from 'graphql-relay'

let cursor = offsetToCursor(9)

// jump straight to page 3!
let query = `{
  user(id: 1) {
    posts(first: 5, after: "${cursor}") {
      pageInfo {
        endCursor
      }
      total
      edges {
        node { body }
      }
    }
  }
}`

I hope I've answered your question.

@mposborne
Copy link

For anyone not using Relay but still wanting simple LIMIT / OFFSET, here's an example (rather rough) dialect you can use/modify to do that:

https://github.com/RollHQ/join-monster-mysql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants