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

A clearer explanation for cursor pagination configuration #1791

Closed
jacob-s-son opened this issue Aug 17, 2018 · 4 comments
Closed

A clearer explanation for cursor pagination configuration #1791

jacob-s-son opened this issue Aug 17, 2018 · 4 comments

Comments

@jacob-s-son
Copy link

We finally got to the point, where we build something in GraphQL that also requires pagination.

Pro version has cursors pagination built in, but it's not very clear how to:

  1. Paginate lists returned by root fields
  2. What is the cleanest of extending connection type to accept arbitrary number of filter fields (in our REST API this was passed as a JSON string)?
  3. How to override scope and cursor generation

I'd guess the first one is rather self-explanatory

The second one is to allow our users as before select from a large number of different filtering options like "status is X", "type is one of(x,y,z)" etc.

Thirdly, we want to paginate records that can be sorted by variety of fields (and maybe even by several fields simultaneously). This requires these fields to be included in both ORDER BY and WHERE clauses together with primary key. Hence, the question: how to override scope and cursor parameters generated by built in functionality?

Thanks.

@rmosolgo
Copy link
Owner

Hi, did you read the connections guide? http://graphql-ruby.org/relay/connections.html It answers a few of those questions, let me know what you think.

@jacob-s-son
Copy link
Author

How about arbitrary JSON object? Just define the type ourselves?

Also that page does not give any answers on root field pagination.

@mskubenich
Copy link

mskubenich commented Jan 20, 2019

Cursor doesn't work as expected.

I don't want to have my cursors based on id only.

``` field :posts, PostConnectionType, null: false

def posts
  Post.all
end

  ``` class PostConnectionType < GraphQL::Types::Relay::BaseConnection
      edge_type(Types::Posts::PostEdgeType)
   
      def cursor_from_node(object)
        "#{ object.class.name }-#{ object.id }"
      end
    end

result:

{
  "data": {
    "viewer": {
      "posts": {
        "edges": [
          {
            "node": {
              "title": "Test"
            },
            "cursor": "Post-1"
          }
        ],
        "pageInfo": {
          "endCursor": "MQ==",
          "startCursor": "MQ==",
          "hasNextPage": true,
          "hasPreviousPage": false
        },
        "totalCount": 15
      }
    }
  }
}

pageInfo cursors are wrong - they are merely based on id - Base64.encode(id)

If i'm trying to pass "Post-1" as a 'before' or 'after' parameter - i'm receiving "message": "Invalid cursor: "Post-1""

graphql (1.8.13)

@rmosolgo
Copy link
Owner

👋 I've redone the connection system in 1.10 and you can find the new docs here: https://graphql-ruby.org/pagination/overview.html

Sorry for the late response here, but if you still have specific questions, please open a new issue!

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

No branches or pull requests

3 participants