Skip to content

Commit 6ed5cba

Browse files
Creating subgraphs edits & restructure (#758)
* one round of changes --------- Co-authored-by: Michael Macaulay <[email protected]>
1 parent aa721d8 commit 6ed5cba

File tree

1 file changed

+89
-56
lines changed

1 file changed

+89
-56
lines changed

website/pages/en/developing/creating-a-subgraph.mdx

Lines changed: 89 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,47 @@
22
title: Creating a Subgraph
33
---
44

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.
66

7-
![Defining a Subgraph](/img/defining-a-subgraph.png)
7+
A subgraph extracts data from a blockchain, processes it, and stores it for efficient querying via GraphQL.
88

9-
The subgraph definition consists of a few files:
9+
![Defining a Subgraph](/img/defining-a-subgraph.png)
1010

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.
1212
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
1414

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
1616

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

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.
2020

21-
## Install the Graph CLI
21+
On your local machine, run one of the following commands:
2222

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/)
2424

25-
Once you have `yarn`, install the Graph CLI by running
25+
```bash
26+
npm install -g @graphprotocol/graph-cli@latest
27+
```
2628

27-
**Install with yarn:**
29+
#### Using [yarn](https://yarnpkg.com/)
2830

2931
```bash
3032
yarn global add @graphprotocol/graph-cli
3133
```
3234

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.
3436

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`.
3838

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.
4040

41-
## From An Existing Contract
41+
## Create a subgraph
4242

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:
4446

4547
```sh
4648
graph init \
@@ -51,21 +53,29 @@ graph init \
5153
<SUBGRAPH_SLUG> [<DIRECTORY>]
5254
```
5355

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.
5561

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.
5763

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:
5967

6068
```sh
61-
graph init --studio <SUBGRAPH_SLUG>
69+
graph init --studio <SUBGRAPH_SLUG> --from-example=example-subgraph
6270
```
6371

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.
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.
6575

66-
## Add New dataSources To An Existing Subgraph
76+
## Add new `dataSources` to an existing subgraph
6777

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:
6979

7080
```sh
7181
graph add <address> [<subgraph-manifest default: "./subgraph.yaml">]
@@ -78,22 +88,45 @@ Options:
7888
--network-file <path> Networks config file path (default: "./networks.json")
7989
```
8090

81-
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
82116

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)
84118

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:
87120

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

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.
91124

92-
## The Subgraph Manifest
125+
- Add an entry for each contract that requires indexing to the `dataSources` array.
93126

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

96-
For the example subgraph, `subgraph.yaml` is:
129+
For the example subgraph listed above, `subgraph.yaml` is:
97130

98131
```yaml
99132
specVersion: 0.0.4
@@ -180,9 +213,9 @@ A single subgraph can index data from multiple smart contracts. Add an entry for
180213

181214
The triggers for a data source within a block are ordered using the following process:
182215

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.
186219

187220
These ordering rules are subject to change.
188221

@@ -305,29 +338,29 @@ Imagine you have a subgraph that needs to make three Ethereum calls to fetch dat
305338

306339
Traditionally, these calls might be made sequentially:
307340

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
311344

312345
Total time taken = 3 + 2 + 4 = 9 seconds
313346

314347
### Scenario with Declarative `eth_calls`
315348

316349
With this feature, you can declare these calls to be executed in parallel:
317350

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
321354

322355
Since these calls are executed in parallel, the total time taken is equal to the time taken by the longest call.
323356

324357
Total time taken = max (3, 2, 4) = 4 seconds
325358

326359
### How it Works
327360

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.
331364

332365
### Example Configuration in Subgraph Manifest
333366

@@ -346,8 +379,8 @@ calls:
346379

347380
Details for the example above:
348381

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.
351384
- 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)`
352385
- The `address` and `arguments` can be replaced with variables that will be available when the handler is executed.
353386

@@ -593,7 +626,7 @@ This more elaborate way of storing many-to-many relationships will result in les
593626

594627
#### Adding comments to the schema
595628

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:
597630

598631
```graphql
599632
type MyFirstEntity @entity {
@@ -931,9 +964,9 @@ dataSources:
931964
932965
> **Note:** The contract creation block can be quickly looked up on Etherscan:
933966
>
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.
937970

938971
## Indexer Hints
939972

@@ -945,9 +978,9 @@ The `indexerHints` setting in a subgraph's manifest provides directives for inde
945978

946979
`indexerHints.prune`: Defines the retention of historical block data for a subgraph. Options include:
947980

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.
951984

952985
```
953986
indexerHints:

0 commit comments

Comments
 (0)