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
+89-56Lines changed: 89 additions & 56 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 detailed guide provides instructions to successfully create a subgraph.
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
+

10
10
11
-
-`subgraph.yaml`: a YAML file containing the subgraph manifest
11
+
> 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) in [Subgraph Studio](https://thegraph.com/studio/apikeys/). It is recommended that you add signal to your subgraph with at least 3,000 GRT to attract 2-3 Indexers.
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/tree/main/packages/cli).
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 an example subgraph.
34
36
35
-
```bash
36
-
npm install -g @graphprotocol/graph-cli
37
-
```
37
+
- This `graph init` command can also create a subgraph in Subgraph Studio by passing in `--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,29 @@ 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 command tries to retrieve the contract ABI from Etherscan.
57
+
58
+
- The Graph CLI relies on a public RPC endpoint. While occasional failures are expected, retries typically resolve this issue. If failures persist, consider using a local ABI.
59
+
60
+
- If any of the optional arguments are missing, it guides you through an interactive form.
55
61
56
-
## From An Example Subgraph
62
+
- The `<SUBGRAPH_SLUG>` is the ID of your subgraph in [Subgraph Studio](https://thegraph.com/studio/). It can be found on your subgraph details page.
57
63
58
-
The second mode `graph init` supports is creating a new project from an example subgraph. The following command does this:
64
+
### From an example subgraph
65
+
66
+
The following command initializes a new project from an example subgraph:
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.
72
+
- The [example subgraph](https://github.com/graphprotocol/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.
73
+
74
+
- The subgraph handles these events by writing `Gravatar` entities to the Graph Node store and ensuring these are updated according to the events.
65
75
66
-
## Add New dataSources To An Existing Subgraph
76
+
## Add new `dataSources` to an existing subgraph
67
77
68
-
Since `v0.31.0` the `graph-cli`supports adding new dataSources to an existing subgraph through the `graph add` command.
78
+
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.
91
+
### Specifics
92
+
93
+
The `graph add` command will fetch the ABI from Etherscan (unless an ABI path is specified with the `--abi` option) and creates a new `dataSource`, similar to how the `graph init` command creates a `dataSource``--from-contract`, updating the schema and mappings accordingly. 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:
96
+
97
+
- If `true`: the new `dataSource` should use existing `eventHandlers` & `entities`.
98
+
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
106
+
107
+
### The Subgraph Manifest
108
+
109
+
The subgraph manifest, `subgraph.yaml`, defines the smart contracts & network your subgraph will index, the events from these contracts to pay attention to, and how to map event data to entities that Graph Node stores and allows to query.
110
+
111
+
The **subgraph definition** consists of the following files:
112
+
113
+
-`subgraph.yaml`: Contains the subgraph manifest
114
+
115
+
-`schema.graphql`: A GraphQL schema defining the data stored for your subgraph and how to query it via GraphQL
82
116
83
-
The `--merge-entities` option identifies how the developer would like to handle `entity` and `event` name conflicts:
117
+
-`mapping.ts`: [AssemblyScript Mappings](https://github.com/AssemblyScript/assemblyscript) code that translates event data into entities defined in your schema (e.g. `mapping.ts` in this guide)
84
118
85
-
- If `true`: the new `dataSource` should use existing `eventHandlers` & `entities`.
86
-
- If `false`: a new entity & event handler should be created with `${dataSourceName}{EventName}`.
119
+
A single subgraph can:
87
120
88
-
The contract `address` will be written to the `networks.json` for the relevant network.
121
+
- Index data from multiple smart contracts (but not multiple networks).
89
122
90
-
> **Note:** When using the interactive cli, after successfully running `graph init`, you'll be prompted to add a new `dataSource`.
123
+
- Index data from IPFS files using File Data Sources.
91
124
92
-
## The Subgraph Manifest
125
+
- Add an entry for each contract that requires indexing to the `dataSources` array.
93
126
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).
127
+
The full specification for subgraph manifests can be found [here](https://github.com/graphprotocol/graph-node/blob/master/docs/subgraph-manifest.md).
95
128
96
-
For the example subgraph, `subgraph.yaml` is:
129
+
For the example subgraph listed above, `subgraph.yaml` is:
97
130
98
131
```yaml
99
132
specVersion: 0.0.4
@@ -180,9 +213,9 @@ A single subgraph can index data from multiple smart contracts. Add an entry for
180
213
181
214
The triggers for a data source within a block are ordered using the following process:
182
215
183
-
1. Event and call triggers are first ordered by transaction index within the block.
184
-
2. Event and call triggers within the same transaction are ordered using a convention: event triggers first then call triggers, each type respecting the order they are defined in the manifest.
185
-
3. Block triggers are run after event and call triggers, in the order they are defined in the manifest.
216
+
1. Event and call triggers are first ordered by transaction index within the block.
217
+
2. Event and call triggers within the same transaction are ordered using a convention: event triggers first then call triggers, each type respecting the order they are defined in the manifest.
218
+
3. Block triggers are run after event and call triggers, in the order they are defined in the manifest.
186
219
187
220
These ordering rules are subject to change.
188
221
@@ -305,29 +338,29 @@ Imagine you have a subgraph that needs to make three Ethereum calls to fetch dat
305
338
306
339
Traditionally, these calls might be made sequentially:
307
340
308
-
1. Call 1 (Transactions): Takes 3 seconds
309
-
2. Call 2 (Balance): Takes 2 seconds
310
-
3. Call 3 (Token Holdings): Takes 4 seconds
341
+
1. Call 1 (Transactions): Takes 3 seconds
342
+
2. Call 2 (Balance): Takes 2 seconds
343
+
3. Call 3 (Token Holdings): Takes 4 seconds
311
344
312
345
Total time taken = 3 + 2 + 4 = 9 seconds
313
346
314
347
### Scenario with Declarative `eth_calls`
315
348
316
349
With this feature, you can declare these calls to be executed in parallel:
317
350
318
-
1. Call 1 (Transactions): Takes 3 seconds
319
-
2. Call 2 (Balance): Takes 2 seconds
320
-
3. Call 3 (Token Holdings): Takes 4 seconds
351
+
1. Call 1 (Transactions): Takes 3 seconds
352
+
2. Call 2 (Balance): Takes 2 seconds
353
+
3. Call 3 (Token Holdings): Takes 4 seconds
321
354
322
355
Since these calls are executed in parallel, the total time taken is equal to the time taken by the longest call.
323
356
324
357
Total time taken = max (3, 2, 4) = 4 seconds
325
358
326
359
### How it Works
327
360
328
-
1. Declarative Definition: In the subgraph manifest, you declare the Ethereum calls in a way that indicates they can be executed in parallel.
329
-
2. Parallel Execution Engine: The Graph Node's execution engine recognizes these declarations and runs the calls simultaneously.
330
-
3. Result Aggregation: Once all calls are complete, the results are aggregated and used by the subgraph for further processing.
361
+
1. Declarative Definition: In the subgraph manifest, you declare the Ethereum calls in a way that indicates they can be executed in parallel.
362
+
2. Parallel Execution Engine: The Graph Node's execution engine recognizes these declarations and runs the calls simultaneously.
363
+
3. Result Aggregation: Once all calls are complete, the results are aggregated and used by the subgraph for further processing.
331
364
332
365
### Example Configuration in Subgraph Manifest
333
366
@@ -346,8 +379,8 @@ calls:
346
379
347
380
Details for the example above:
348
381
349
-
- `global0X128`is the declared `eth_call`.
350
-
- The text before colon(`global0X128`) is the label for this `eth_call` which is used when logging errors.
382
+
- `global0X128`is the declared `eth_call`.
383
+
- The text (`global0X128`) is the label for this `eth_call` which is used when logging errors.
351
384
- The text (`Pool[event.address].feeGrowthGlobal0X128()`) is the actual `eth_call` that will be executed, which is in the form of `Contract[address].function(arguments)`
352
385
- The `address` and `arguments` can be replaced with variables that will be available when the handler is executed.
353
386
@@ -593,7 +626,7 @@ This more elaborate way of storing many-to-many relationships will result in les
593
626
594
627
#### Adding comments to the schema
595
628
596
-
As per GraphQL spec, comments can be added above schema entity attributes using the hash symble`#`. This is illustrated in the example below:
629
+
As per GraphQL spec, comments can be added above schema entity attributes using the hash symbol`#`. This is illustrated in the example below:
597
630
598
631
```graphql
599
632
typeMyFirstEntity@entity {
@@ -931,9 +964,9 @@ dataSources:
931
964
932
965
> **Note:** The contract creation block can be quickly looked up on Etherscan:
933
966
>
934
-
> 1. Search for the contract by entering its address in the search bar.
935
-
> 2. Click on the creation transaction hash in the `Contract Creator` section.
936
-
> 3. Load the transaction details page where you'll find the start block for that contract.
967
+
> 1. Search for the contract by entering its address in the search bar.
968
+
> 2. Click on the creation transaction hash in the `Contract Creator` section.
969
+
> 3. Load the transaction details page where you'll find the start block for that contract.
937
970
938
971
## Indexer Hints
939
972
@@ -945,9 +978,9 @@ The `indexerHints` setting in a subgraph's manifest provides directives for inde
945
978
946
979
`indexerHints.prune`: Defines the retention of historical block data for a subgraph. Options include:
947
980
948
-
1. `"never"`: No pruning of historical data; retains the entire history.
949
-
2. `"auto"`: Retains the minimum necessary history as set by the indexer, optimizing query performance.
950
-
3. A specific number: Sets a custom limit on the number of historical blocks to retain.
981
+
1. `"never"`: No pruning of historical data; retains the entire history.
982
+
2. `"auto"`: Retains the minimum necessary history as set by the indexer, optimizing query performance.
983
+
3. A specific number: Sets a custom limit on the number of historical blocks to retain.
0 commit comments