# Create a Subgraph

## 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](https://thegraph.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:

{% stepper %}
{% step %}
**Create a Query Key and Activate Your Subscription**

You can find out about how to create a query key in the [Create Query Keys](https://chain-love.gitbook.io/chain.loves-api-management-module/getting-started-with-rpc/create-query-keys) section.
{% endstep %}

{% step %}
**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**
   {% endstep %}

{% step %}
**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](https://nodejs.org/en/download/package-manager), [Yarn](https://classic.yarnpkg.com/lang/en/docs/install/#debian-stable), [GraphCLI](https://www.npmjs.com/package/@graphprotocol/graph-cli), [Docker](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/).
2. Build the subgraph (as an exercise, you can use [this one](https://github.com/protofire/filecoin-subgraph)):

```bash
yarn install && graph codegen && graph build
```

3. 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_QUERY_KEY>` with your actual deploy key available at Deploy Keys tab in the Subgra Query Keys table.

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

{% endstep %}
{% endstepper %}

### Query Your Subgraph

To query your subgraph, you can use the playground provided by Chain.Love Indexing:

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](https://api.node.glif.io/graph/21/mainnet%2Fblocks) subgraph):

```graphql
query MyQuery {
  blocks(block: { number: 2867000 }) {
    number
  }
}
```

To query your subgraph from your code, generate [Query Keys](https://chain-love.gitbook.io/chain.loves-api-management-module/getting-started-with-rpc/create-query-keys) (same as in RPC section, but in Subgraphs), then send a POST request to the GraphQL endpoint specified in the subgraph details under `Queries (HTTP)`.

## 3rd-party tutorials

1. [How to deploy Subgraphs on Somnia network](https://docs.somnia.network/developer/partners/indexing-data-on-somnia-using-graph-services)
2. [How to deploy Subgraphs on Filecoin network](https://docs.filecoin.io/smart-contracts/advanced/fevm-indexers#the-graph)
