Skip to content

Commit eed83ff

Browse files
committed
one round of changes
1 parent 3dcc640 commit eed83ff

File tree

1 file changed

+78
-33
lines changed

1 file changed

+78
-33
lines changed

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

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

11-
- `subgraph.yaml`: a YAML file containing the subgraph manifest
11+
![Defining a Subgraph](/img/defining-a-subgraph.png)
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).
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 a demo subgraph.
3436

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`.
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,27 @@ 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 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.
5561

56-
## From An Example Subgraph
62+
### From a demo subgraph
5763

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

6066
```sh
6167
graph init --studio <SUBGRAPH_SLUG>
6268
```
6369

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

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
6773

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

7078
```sh
7179
graph add <address> [<subgraph-manifest default: "./subgraph.yaml">]
@@ -78,22 +86,59 @@ Options:
7886
--network-file <path> Networks config file path (default: "./networks.json")
7987
```
8088

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

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

8597
- If `true`: the new `dataSource` should use existing `eventHandlers` & `entities`.
86-
- If `false`: a new entity & event handler should be created with `${dataSourceName}{EventName}`.
8798

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
89106

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
91108

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

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

96-
For the example subgraph, `subgraph.yaml` is:
141+
For the demo subgraph listed above, `subgraph.yaml` is:
97142

98143
```yaml
99144
specVersion: 0.0.4

0 commit comments

Comments
 (0)