You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/pages/en/developing/creating-a-subgraph.mdx
+78-33Lines changed: 78 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,45 +2,47 @@
2
2
title: Creating a Subgraph
3
3
---
4
4
5
-
A subgraph extracts data from a blockchain, processing it and storing it so that it can be easily queried via GraphQL.
5
+
This comprehensive guide provides detailed instructions to create a subgraph successfully.
6
6
7
-

7
+
A subgraph extracts data from a blockchain, processes it, and stores it for efficient querying via GraphQL.
8
8
9
-
The subgraph definition consists of a few files:
9
+
Before you begin, ensure you [create an API Key](deploying/subgraph-studio-faqs/#2-how-do-i-create-an-api-key) to enable your subgraph on The Graph.
10
10
11
-
-`subgraph.yaml`: a YAML file containing the subgraph manifest
11
+

12
12
13
-
-`schema.graphql`: a GraphQL schema that defines what data is stored for your subgraph, and how to query it via GraphQL
13
+
## Getting Started
14
14
15
-
-`AssemblyScript Mappings`: [AssemblyScript](https://github.com/AssemblyScript/assemblyscript) code that translates from the event data to the entities defined in your schema (e.g. `mapping.ts` in this tutorial)
15
+
### Install the Graph CLI
16
16
17
-
> In order to use your subgraph on The Graph's decentralized network, you will need to [create an API key](/deploying/subgraph-studio-faqs/#2-how-do-i-create-an-api-key). It is recommended that you [add signal](/network/curating/#how-to-signal) to your subgraph with at least [3,000 GRT](/sunrise/#how-can-i-ensure-high-quality-of-service-and-redundancy-for-subgraphs-on-the-graph-network).
17
+
To build and deploy a subgraph, you will need the [Graph CLI](https://github.com/graphprotocol/graph-tooling).
18
18
19
-
Before you go into detail about the contents of the manifest file, you need to install the [Graph CLI](https://github.com/graphprotocol/graph-tooling) which you will need to build and deploy a subgraph.
19
+
The Graph CLI is written in TypeScript, and you must have `node` and either `npm` or `yarn` installed to use it. Check for the [most recent](https://github.com/graphprotocol/graph-tooling/releases?q=%40graphprotocol%2Fgraph-cli&expanded=true) CLI version.
20
20
21
-
## Install the Graph CLI
21
+
On your local machine, run one of the following commands:
22
22
23
-
The Graph CLI is written in JavaScript, and you will need to install either `yarn` or `npm` to use it; it is assumed that you have yarn in what follows.
23
+
Using [npm](https://www.npmjs.com/):
24
24
25
-
Once you have `yarn`, install the Graph CLI by running
25
+
```bash
26
+
npm install -g @graphprotocol/graph-cli@latest
27
+
```
26
28
27
-
**Install with yarn:**
29
+
Using [yarn](https://yarnpkg.com/):
28
30
29
31
```bash
30
32
yarn global add @graphprotocol/graph-cli
31
33
```
32
34
33
-
**Install with npm:**
35
+
- The `graph init` command can be used to set up a new subgraph project, either from an existing contract or from a demo subgraph.
34
36
35
-
```bash
36
-
npm install -g @graphprotocol/graph-cli
37
-
```
37
+
- This command can be used to create a subgraph in Subgraph Studio by passing in graph: `init --product subgraph-studio`.
38
38
39
-
Once installed, the `graph init` command can be used to set up a new subgraph project, either from an existing contract or from an example subgraph. This command can be used to create a subgraph in Subgraph Studio by passing in `graph init --product subgraph-studio`. If you already have a smart contract deployed to your preferred network, bootstrapping a new subgraph from that contract can be a good way to get started.
39
+
-If you already have a smart contract deployed to your preferred network, you can bootstrap a new subgraph from that contract to get started.
40
40
41
-
## From An Existing Contract
41
+
## Create a Subgraph
42
42
43
-
The following command creates a subgraph that indexes all events of an existing contract. It attempts to fetch the contract ABI from Etherscan and falls back to requesting a local file path. If any of the optional arguments are missing, it takes you through an interactive form.
43
+
### From an existing contract
44
+
45
+
The following command creates a subgraph that indexes all events of an existing contract:
44
46
45
47
```sh
46
48
graph init \
@@ -51,21 +53,27 @@ graph init \
51
53
<SUBGRAPH_SLUG> [<DIRECTORY>]
52
54
```
53
55
54
-
The `<SUBGRAPH_SLUG>` is the ID of your subgraph in Subgraph Studio, it can be found on your subgraph details page.
56
+
- The graph-cli relies on a public RPC endpoint, which is rate-limited. While occasional failures are expected, retries typically resolve the issue. If failures persist, consider using a local ABI.
57
+
58
+
- If any of the optional arguments are missing, it guides you through an interactive form.
59
+
60
+
- The `<SUBGRAPH_SLUG>` is the ID of your subgraph in Subgraph Studio. It can be found on your subgraph details page.
55
61
56
-
## From An Example Subgraph
62
+
###From a demo subgraph
57
63
58
-
The second mode `graph init` supports is creating a new project from an example subgraph. The following command does this:
64
+
The following command creates a new project from an example subgraph:
59
65
60
66
```sh
61
67
graph init --studio <SUBGRAPH_SLUG>
62
68
```
63
69
64
-
The [example subgraph](https://github.com/graphprotocol/example-subgraph) is based on the Gravity contract by Dani Grant that manages user avatars and emits `NewGravatar` or `UpdateGravatar` events whenever avatars are created or updated. The subgraph handles these events by writing `Gravatar` entities to the Graph Node store and ensuring these are updated according to the events. The following sections will go over the files that make up the subgraph manifest for this example.
70
+
-The example subgraph is based on the Gravity contract by Dani Grant, which manages user avatars and emits `NewGravatar` or `UpdateGravatar` events whenever avatars are created or updated.
65
71
66
-
## Add New dataSources To An Existing Subgraph
72
+
- The subgraph handles these events by writing `Gravatar` entities to the Graph Node store and ensuring these are updated according to the events
67
73
68
-
Since `v0.31.0` the `graph-cli` supports adding new dataSources to an existing subgraph through the `graph add` command.
74
+
### Add new dataSources to an existing subgraph
75
+
76
+
Since v0.31.0, the `graph-cli` supports adding new dataSources to an existing subgraph through the `graph add` command:
The `add` command will fetch the ABI from Etherscan (unless an ABI path is specified with the `--abi` option), and will create a new `dataSource` in the same way that `graph init` command creates a `dataSource``--from-contract`, updating the schema and mappings accordingly.
89
+
**Specifics**
90
+
91
+
The `add` command will fetch the ABI from Etherscan unless an ABI path is specified with the `--abi` option. Subsequently, it will create a new `dataSource` like `graph init` command creates a dataSource `--from-contract` and will update the schema and mappings accordingly.
82
92
83
-
The `--merge-entities` option identifies how the developer would like to handle `entity` and `event` name conflicts:
93
+
> Note: This allows you to index implementation contracts from their proxy contracts.
94
+
95
+
- The `--merge-entities` option identifies how the developer would like to handle `entity` and `event` name conflicts:
84
96
85
97
- If `true`: the new `dataSource` should use existing `eventHandlers` & `entities`.
86
-
- If `false`: a new entity & event handler should be created with `${dataSourceName}{EventName}`.
87
98
88
-
The contract `address` will be written to the `networks.json` for the relevant network.
99
+
- If `false`: a new `entity` & `event` handler should be created with `${dataSourceName}{EventName}`.
100
+
101
+
- The contract `address `will be written to the `networks.json` for the relevant network.
102
+
103
+
> Note: When using the interactive cli, after successfully running `graph init`, you'll be prompted to add a new `dataSource`.
104
+
105
+
## Components of a Subgraph
89
106
90
-
> **Note:** When using the interactive cli, after successfully running `graph init`, you'll be prompted to add a new `dataSource`.
107
+
### The Subgraph Manifest
91
108
92
-
## The Subgraph Manifest
109
+
The subgraph manifest, `subgraph.yaml`, specifies the following:
110
+
111
+
- The smart contracts to index
112
+
113
+
- The events from these contracts
114
+
115
+
- The mapping of event data that Graph Node stores and queries
116
+
117
+
The subgraph definition consists of the following files:
118
+
119
+
-`subgraph.yaml`: Contains the subgraph manifest
120
+
121
+
-`schema.graphql`: a GraphQL schema defining the data stored for your subgraph and how to query it via GraphQL.
122
+
123
+
-`AssemblyScript` Mappings: [Assembly Script](https://github.com/AssemblyScript/assemblyscript) code that translates event data into entities defined in your schema (e.g. mapping.ts in this guide)
124
+
125
+
A single subgraph can:
126
+
127
+
- Index data from multiple smart contracts
128
+
129
+
- Add an entry for each contract that requires indexing to the `dataSources array`.
130
+
131
+
The subgraph definition consists of a few files:
132
+
133
+
-`subgraph.yaml`: a YAML file containing the subgraph manifest
134
+
135
+
-`schema.graphql`: a GraphQL schema that defines what data is stored for your subgraph, and how to query it via GraphQL
136
+
137
+
-`AssemblyScript Mappings`: [AssemblyScript](https://github.com/AssemblyScript/assemblyscript) code that translates from the event data to the entities defined in your schema (e.g. `mapping.ts` in this tutorial)
93
138
94
-
The subgraph manifest `subgraph.yaml` defines the smart contracts your subgraph indexes, which events from these contracts to pay attention to, and how to map event data to entities that Graph Node stores and allows to query. The full specification for subgraph manifests can be found [here](https://github.com/graphprotocol/graph-node/blob/master/docs/subgraph-manifest.md).
139
+
The full specification for subgraph manifests can be found [here](https://github.com/graphprotocol/graph-node/blob/master/docs/subgraph-manifest.md).
95
140
96
-
For the example subgraph, `subgraph.yaml` is:
141
+
For the demo subgraph listed above, `subgraph.yaml` is:
0 commit comments