In the previous post I wired up a React Native client to a Rails API GraphQL server. In this post I’ll enable adding, removing and retrieving paginated data.
Data Models
The root of all my GraphQL queries are now a user and a user has a number of meetings. A user can sign-up, then meetings can be created or destroyed via mutations. See 33-minutes-server@838200 for implementation details.
Relay-Style Mutations
Relay has a specified add/remove behavior via RANGE_ADD and NODE_DELETE.
RANGE_ADD
To enable this on the server, return the range connection and edge from the mutation.
The client-side mutation needs a parent ID (a user ID), has to request the edge, and include RANGE_ADD in its configs.
The client-side mutation needs the parent ID (a user ID) and to include a NODE_DELETE in its configs.
Note that NODE_DELETE empties a node in the local store, but doesn’t remove it. Therefore a if (node) is needed in the list renderer. This is relay#2155.
Relay’s support for pagination relies on the GraphQL server exposing connections in a standardized way. To expose user’s meetings as a GraphQL field you would write field :meetings, -> { !types[Types::MeetingType] }. To enable this to be Relay-style, use connection.
This returns meetings with edge and node elements along with pageInfo and much more.
To enable this on the client requires a lot of boilerplate, including a pagination container and a call to Relay to load more data as documented. I suggest just copy-pasting and adapting working code from 33-minutes-app@4fd9bd.