Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a Relay GraphQL connection and when a plain list?

In Relay GraphQL, connections and lists are both array-like, but they have different features. When should I use each?

like image 506
Greg Hurrell Avatar asked Oct 12 '15 18:10

Greg Hurrell


People also ask

Should I use Relay for GraphQL?

Relay is recommended to use in the frontend to have more structured, modular, future-proofed applications that can scale easily to millions of users. The first thing that needs to be implemented in order to use Relay is to make a Relay-compatible GraphQL server. That's what we're going to do now.

What is Relay in GraphQL?

Relay is a JavaScript framework for fetching and managing GraphQL data in React applications that emphasizes maintainability, type safety and runtime performance. Relay achieves this by combining declarative data fetching and a static build step.

What are GraphQL connections?

A connection is a way to get all of the nodes that are connected to another node in a specific way. In this case we want to get all of the nodes connected to our users that are friends. Another connection might be between a user node to all of the posts that they liked.


1 Answers

Connections

  • More powerful and flexible than simple lists.
  • Support pagination (forward and back), with cursors.
  • Fine-grained mutation support (eg. RANGE_ADD, RANGE_DELETE, NODE_DELETE, as described in the guide).
  • Requires a first or last argument in order to limit the size of the result set.
  • Has an edges field that provides a place to locate per-edge, edge-specific data.
  • A heavier-weight concept, requiring more work to define in the schema.

Lists

  • Simple and lightweight.
  • No support for pagination (the entire list is always returned).
  • No special mutations features for prepending, appending etc (although it is a requested feature).

Which to use?

  • Whenever you need pagination, you should use a connection.
  • If you need fine-grained control over mutations, you may choose to use a connection, even if you don't need pagination.
  • If you want all the items in a connection, you can use first with some large number.
  • If you want to expose a short list with minimal effort, use a simple list.
like image 158
Greg Hurrell Avatar answered Sep 17 '22 13:09

Greg Hurrell