Reputation: 19242
I'm new to GraphQL and I'm using WPGraphQL and WooGraphQL.
Their top level connections allow me to expand both nodes
and edges
like so:
{
wpgraphql {
productCategories {
# What's the difference between these?
# 1) top-level nodes
nodes {
id
name
}
# 2) top-level edges
edges {
node {
id
name
}
}
}
}
}
Which returns a response like so (IDs omitted):
{
"data": {
"wpgraphql": {
"productCategories": {
"nodes": [
{
"name": "Accessories"
},
{
"name": "Gift Cards"
},
{
"name": "Indoor"
}
],
"edges": [
{
"node": {
"name": "Accessories"
}
},
{
"node": {
"name": "Gift Cards"
}
},
{
"node": {
"name": "Indoor"
}
}
]
}
}
}
}
My question is simply: Which one do I use? Why are there both?
Here is a screenshot of the GraphiQL explorer if that helps.
Upvotes: 1
Views: 814
Reputation: 84697
GraphQL schemas that implement the Relay specification utilize Connection types to model one-to-many or many-to-many relationships.
Each connection includes a list of edges and a PageInfo
object. Each edge includes a node
and the cursor
for that node.
Edges may also contain additional fields -- for example, if we have a friends connection between User nodes, we might include the timestamps when the friendships were created. Normally, though, edges are only used for the cursor
field they expose. The cursor
value is used when paginating through the connection and exposing it for every edge means you can start your pagination from any arbitrary point in the results. The cursor is not included as part of the node because it's may be specific to the connection and not just the node itself (for example, some cursors encode sort criteria).
However, if as a client you don't need to paginate the results of a connection and just want to fetch all the nodes, you probably don't care about cursors. In these scenarios, having edges doesn't add any value and just increases the depth of your query. As a result, as a convenience to the client, some GraphQL services have opted to exposing just the nodes for the connection in addition to the edges.
Upvotes: 4