In GraphQL what's the meaning of "edges" and "node"?
I am consuming a particular GraphQL endpoint and although I am supplying a clean JSON structure as a query, when I get the results I get "edges" and "node" tags. It seems as if it is polluting my data with no obvious benefit. Why is it there and is it possible to get rid of those for faster and simpler parsing of data?
Solution 1:
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
- a
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 connectionArgsafter/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
Solution 2:
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
}
}
}
}