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
+18-32Lines changed: 18 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,14 @@
2
2
title: Creating a Subgraph
3
3
---
4
4
5
-
This comprehensive guide provides detailed instructions to create a subgraph successfully.
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
-
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
-
11
9

12
10
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.
12
+
13
13
## Getting Started
14
14
15
15
### Install the Graph CLI
@@ -53,27 +53,29 @@ graph init \
53
53
<SUBGRAPH_SLUG> [<DIRECTORY>]
54
54
```
55
55
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.
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.
57
59
58
60
- If any of the optional arguments are missing, it guides you through an interactive form.
59
61
60
-
- The `<SUBGRAPH_SLUG>` is the ID of your subgraph in Subgraph Studio. It can be found on your subgraph details page.
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.
61
63
62
-
### From a demo subgraph
64
+
### From an example subgraph
63
65
64
66
The following command creates a new project from an example subgraph:
65
67
66
68
```sh
67
69
graph init --studio <SUBGRAPH_SLUG>
68
70
```
69
71
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.
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.
71
73
72
74
- The subgraph handles these events by writing `Gravatar` entities to the Graph Node store and ensuring these are updated according to the events
73
75
74
76
### Add new dataSources to an existing subgraph
75
77
76
-
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. Subsequently, it will create a new `dataSource` like `graph init` command creates a dataSource `--from-contract` and will update the schema and mappings accordingly.
92
-
93
-
> Note: This allows you to index implementation contracts from their proxy contracts.
93
+
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. This allows you to index implementation contracts from their proxy contracts.
94
94
95
95
- The `--merge-entities` option identifies how the developer would like to handle `entity` and `event` name conflicts:
96
96
@@ -106,39 +106,25 @@ The `add` command will fetch the ABI from Etherscan unless an ABI path is specif
106
106
107
107
### The Subgraph Manifest
108
108
109
-
The subgraph manifest, `subgraph.yaml`, specifies the following:
110
-
111
-
- The smart contracts to index
112
-
113
-
- The events from these contracts
109
+
The subgraph manifest, `subgraph.yaml`, defines the smart contracts 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.
114
110
115
-
- The mapping of event data that Graph Node stores and queries
116
-
117
-
The subgraph definition consists of the following files:
111
+
The **subgraph definition** consists of the following files:
118
112
119
113
-`subgraph.yaml`: Contains the subgraph manifest
120
114
121
115
-`schema.graphql`: a GraphQL schema defining the data stored for your subgraph and how to query it via GraphQL.
122
116
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)
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)
124
118
125
119
A single subgraph can:
126
120
127
-
- Index data from multiple smart contracts
121
+
- Index data from multiple smart contracts (but not multiple networks)
128
122
129
123
- Add an entry for each contract that requires indexing to the `dataSources array`.
130
124
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)
138
-
139
125
The full specification for subgraph manifests can be found [here](https://github.com/graphprotocol/graph-node/blob/master/docs/subgraph-manifest.md).
140
126
141
-
For the demo subgraph listed above, `subgraph.yaml` is:
127
+
For the example subgraph listed above, `subgraph.yaml` is:
142
128
143
129
```yaml
144
130
specVersion: 0.0.4
@@ -392,7 +378,7 @@ calls:
392
378
Details for the example above:
393
379
394
380
- ` global0X128`is the declared `eth_call`.
395
-
- The text before colon(`global0X128`) is the label for this `eth_call` which is used when logging errors.
381
+
- The text (`global0X128`) is the label for this `eth_call` which is used when logging errors.
396
382
- 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)`
397
383
- The `address` and `arguments` can be replaced with variables that will be available when the handler is executed.
0 commit comments