177

I am consuming a GraphQL endpoint and I get results that contain edges and node tags. I am supplying a clean JSON structure for my query, so this doesn't make sense to me.

It seems as if the GraphQL server is polluting my data with no obvious benefit. Why are these terms included in the GraphQL endpoint's response and is it possible to get rid of those for faster/simpler parsing of data?

serraosays
  • 7,163
  • 3
  • 35
  • 60
Ska
  • 6,658
  • 14
  • 53
  • 74
  • 1
    Connections, edges and nodes is terminology mainly used in the context of Relay, the GraphQL client. More information can be found in this [FAQ](https://www.graph.cool/blog/connections-edges-nodes-relay-tioghei9go/). – marktani Mar 06 '17 at 12:42
  • 4
    Just to clarify: connections are not a Relay-specific thing. For an in-depth look, see this article: https://medium.com/p/explaining-graphql-connections-c48b7c3d6976 – helfer Mar 06 '17 at 16:36
  • 3
    Its somewhat of a standard way of providing paging for long lists of results. Not tied to any implementation. – hookenz Oct 18 '19 at 09:02
  • 5
    https://graphql.org/learn/pagination/ – hookenz Oct 18 '19 at 09:03

2 Answers2

114

Let's begin with a short introduction in simple words


GraphQL Relay specifications

  • mechanism for refetching an object
  • description of how to page through connections
  • structure around mutations to make them predictable

Connections:

  • a connection is a collection of objects with metadata such as edges, pageInfo...

  • pageInfo will contain hasNextPage, hasPreviousPage, startCursor, endCursor

    • hasNextPage will tell us if there are more edges available, or if we’ve reached the end of this connection.
  • The array of records: edges

    • edges will provide you flexibility to use your data(node)
    • edges will help you for the pagination, There is GraphQL GraphQLList but with no functionality such as pagination, only with array of objects (data)
  • Each edge has

    • a node: a record or a data
    • a cursor: base64 encoded string to help relay with pagination

https://facebook.github.io/relay/graphql/connections.htm

Node:

  • you can set the number of nodes you need to show using the relay connectionArgs(first, last, after, before)

Relay Pagination works as

  • Fetches all objects in the collection and return a slice based on the first/last x records, used thru connectionArgs

  • after/before are used to indicate to the GraphQL server the number of required slice (data) using cursor from the node

There are many more things to consider like nodeDefinitions, globalFieldId, nodeInterfaces

https://github.com/graphql/graphql-relay-js#object-identification

Nhan
  • 3,595
  • 6
  • 30
  • 38
parwatcodes
  • 6,669
  • 5
  • 27
  • 39
  • 29
    I think this answer has the gist right, but it contains many misconceptions. This article explains the reasoning behind GraphQL connections pretty well: https://medium.com/p/explaining-graphql-connections-c48b7c3d6976 – helfer Mar 06 '17 at 16:37
  • 9
    where do you find the misconceptions, it's just the brief information, if you found any misconceptions, you can always improve it to make it better – parwatcodes Mar 06 '17 at 16:39
  • Can I work with these constructs through _graphql_ UI? For example _hasNextPage_, or are they available only through JS with Relay? – Ska Mar 14 '17 at 08:00
  • yes, you can work on these from graphiql UI using graphql-relay – parwatcodes Mar 14 '17 at 09:00
  • not `each node will have a cursor`, rather `each edge will have a cursor`, refer that https://blog.apollographql.com/explaining-graphql-connections-c48b7c3d6976 – wind Dec 26 '19 at 04:09
  • 9
    This answer leans too much on an existing understanding of GraphQL terminology. It's more a list of the terms rather than an explanation. – Alexander Trauzzi Feb 10 '21 at 03:25
  • This is the answer you would tell to an alien. Why wouldn't you explain what an edge actually is, and why we even have them? – Mondo Duke Jan 04 '23 at 22:31
20

GraphQL stands for Graph Query Language, and has two parts: the server and client. The server effectively puts a graph structure in front of your database and your queries are traversing that graph.

In computer science:

  • a graph is a network
  • a node is one of the vertices in that network
  • an edge is one of the links between the nodes

Take all of this together, a GraphQL query effectively asks the GraphQL server instance to traverse its graph of data and find some representation of that data. You'll see edges and node in your queries because you're literally looking at those entries in the graph.

Typical query for allXYZ records will look like this:

{
  _allXYZ {
    edges {
      node {
        // your data shape will be in here
      }
    }
  }
}
serraosays
  • 7,163
  • 3
  • 35
  • 60