Connections
Welcome to a quick guide that will help you grasp the core concepts of connections, nodes, and edges as they apply to our GraphQL API.
1. What Are They?
Connection: It’s the link between data points. In GraphQL, a connection models a one-to-many relationship, and it is the heart of pagination.
Node: Represents an individual piece of data in your dataset. Think of nodes as the primary elements you’re trying to fetch.
Edge: Serves as the bridge between connections and nodes. It contains both the node’s data and additional information about the connection, like a cursor for pagination.
2. Visual Representation
Imagine a necklace. The necklace itself is the connection. Each bead on it is a node, and the thread passing through each bead is the edge.
3. Why Use This Model?
- Scalability: This model ensures efficient data fetching, especially when dealing with large datasets.
- Flexibility: It supports intricate pagination and data fetching patterns, such as infinite scrolling.
- Consistency: It provides a standardized way to interact with varying datasets.
4. A Simple Query Example
{
units(first: 5) {
edges {
node {
id
name
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
In this example:
- We’re querying a connection called
unitsto fetch the first 5 properties. - Each
edgecontains anode(actual property data) and acursor(used for pagination). - The
pageInfogives us details about the pagination status.
5. Key Concepts Recap
- Connection: The whole dataset or its subset.
- Node: Individual data items you’re fetching.
- Edge: Contains the node and additional info about its position in the dataset.
6. Best Practices
- While paginating, always use the cursors provided within edges. Manual modifications can lead to unexpected results.
- Familiarize yourself with the specific fields and data types within nodes relevant to your queries.
You can read more in the specification: https://relay.dev/graphql/connections.htm