Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define a relay connection for pagination against an ORM?

I've looked through all of Relay's documentation and there doesn't seem to be a straightforward explanation on how to build a Relay connection with an ORM. All the examples seem to use the connectionFromArray method which is fine if you're storing your data in memory but when you're storing the data in a database how would you go about providing the information necessary for a connection's pagination to work?

like image 297
vincentriemer Avatar asked Sep 15 '15 13:09

vincentriemer


1 Answers

The cursor is opaque:

The result of this field is considered opaque by Relay, but will be passed back to the server as described in the "Arguments" section below.

So, depending on your data model, it may be appropriate to pass something as simple as an ID string as a cursor, which you could then use on the server to load the connection using a clause like WHERE id > ?. You could pack arbitrary info in here necessary for pagination here and Base-64 encode it, for example.

It you look at the code which returns a connection from an array, you'll get a rough idea of the semantics you'll need do implement on top of your ORM-backed storage, but instead of indexing into the array like it does, you'll be synthesizing an opaque cursor that contains enough info to do subsequent pagination with your ORM.

like image 151
Greg Hurrell Avatar answered Oct 21 '22 16:10

Greg Hurrell