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:

1

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.

2

Register Your Subgraph

To register your subgraph, you need to follow these steps:

  1. Go to the Subgraphs tab.

  2. Click Create a New Subgraph.

  3. Enter a name for your subgraph.

  4. Click Create

3

Deploy Your Subgraph

Once you have registered your subgraph, you'll be presented with detailed instructions on how to deploy it. A brief summary:

  1. Install prerequisites: NodeJS, Yarn, GraphCLI, Docker and Docker Compose.

  2. Build the subgraph (as an exercise, you can use this one):

yarn install && graph codegen && graph build
  1. Deploy the subgraph using the following command. You'll be presented with specific values for the GRAPH_INDEX_URI and GRAPH_IPFS_URI parameters. Make sure to replace <YOUR_API_KEY> with your actual API key.

graph deploy <SUBGRAPH_NAME> \
  --node <GRAPH_INDEX_URI> \
  --ipfs <GRAPH_IPFS_URI> \
  --access-token <YOUR_API_KEY>  \
  --version-label <VERSION>

Query Your Subgraph

To query your subgraph, you can use the playground provided by Proteus Shield:

  1. Go to the Subgraphs tab.

  2. Find and select the subgraph you wish to query.

  3. In the Playground area, click the Show GraphiQL Explorer button (represented by a folder icon).

  4. Select the elements you want to query and modify the query as needed.

  5. 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).

Last updated