Create a Subgraph
This section covers what is a subgraph, why should you care about it, and how to create one.
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 an API Key and Activate Your Subscription
You can find out about how to create an API key in the Create API 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):
Deploy the subgraph using the following command. You'll be presented with specific values for the
GRAPH_INDEX_URI
andGRAPH_IPFS_URI
parameters. Make sure to replace<YOUR_API_KEY>
with your actual API key.
Query Your Subgraph
To query your subgraph, you can use the playground provided by Proteus Shield:
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):
To query your subgraph from your code, send a POST request to the GraphQL endpoint specified in the subgraph details under Queries (HTTP)
.
Last updated