Create a Subgraph
This section covers what is a subgraph, why should you care about it, and how to create one.
Create a Subgraph
What is a Subgraph and Why Should I Care?
A subgraph is a custom schema for indexing blockchain data. It provides efficient access to data and allows easy querying via GraphQL.
Subgraphs have several advantages over traditional RPC endpoints:
Faster querying: Subgraphs are designed for efficient querying, allowing for faster data retrieval and processing.
Lightweight API: By providing a GraphQL API, subgraphs reduce the complexity of querying blockchain data.
Customizable schema: Developers can create a schema that specifically fits their needs, making it easier to work with blockchain data.
Cost-effective: Subgraphs are more cost-effective than running archival nodes or parsing entire blockchain histories.
If you're interested in learning more about subgraphs, we recommend checking out The Graph Academy for a comprehensive guide on how to develop a subgraph.
How to Create a Subgraph
To create a subgraph, you need to follow these steps:
Create a Query Key and Activate Your Subscription
You can find out about how to create a query key in the Create Query Keys section.
Deploy Your Subgraph
Once you have registered your subgraph, you'll be presented with detailed instructions on how to deploy it. A brief summary:
Install prerequisites: NodeJS, Yarn, GraphCLI, Docker and Docker Compose.
Build the subgraph (as an exercise, you can use this one):
yarn install && graph codegen && graph buildDeploy the subgraph using the following command. You'll be presented with specific values for the
GRAPH_INDEX_URIandGRAPH_IPFS_URIparameters. Make sure to replace<YOUR_QUERY_KEY>with your actual deploy key available at Deploy Keys tab in the Subgra Query Keys table.
graph deploy <SUBGRAPH_NAME> \
--node <GRAPH_INDEX_URI> \
--ipfs <GRAPH_IPFS_URI> \
--access-token <YOUR_QUERY_KEY> \
--version-label <VERSION>Query Your Subgraph
To query your subgraph, you can use the playground provided by Chain.Love Indexing:
Go to the Subgraphs tab.
Find and select the subgraph you wish to query.
In the Playground area, click the Show GraphiQL Explorer button (represented by a folder icon).
Select the elements you want to query and modify the query as needed.
Click Execute query to run the query.
The resulting query may look like the following (based on mainnet/blocks subgraph):
query MyQuery {
blocks(block: { number: 2867000 }) {
number
}
}To query your subgraph from your code, send a POST request to the GraphQL endpoint specified in the subgraph details under Queries (HTTP).
3rd-party tutorials
Last updated