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

GraphqlRelayjs

Graphql Problem Overview


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

Graphql Solutions


Solution 1 - Graphql

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg HurrellView Question on Stackoverflow
Solution 1 - GraphqlGreg HurrellView Answer on Stackoverflow