Skip to content

Commit db97bd3

Browse files
authored
Merge pull request 0xPolygon#2484 from alinobrasil/tools/thegraph
Added section for the graph: quick start full guide
2 parents 65f8338 + 26fbff4 commit db97bd3

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

docs/tools/data/thegraph.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
2+
!!! note "Content disclaimer"
3+
4+
Please view the third-party content disclaimer [here](https://github.com/0xPolygon/polygon-docs/blob/main/CONTENT_DISCLAIMER.md).
5+
6+
# The Graph
7+
8+
Getting historical data on a smart contract can be frustrating when building a dapp. [The Graph](https://thegraph.com/) provides an easy way to query smart contract data through APIs known as subgraphs. The Graph’s infrastructure relies on a decentralized network of indexers, enabling your dapp to become truly decentralized.
9+
10+
The Graph supports:
11+
- Polygon Mainnet
12+
- Polygon zkEVM Mainnet
13+
- Polygon Amoy Testnet
14+
- Polygon zkEVM Cardona Testnet
15+
16+
## Quick Start
17+
18+
These subgraphs only take a few minutes to set up. To get started, follow these three steps:
19+
20+
1. Initialize your subgraph project
21+
2. Deploy & Publish
22+
3. Query from your dapp
23+
24+
Here’s a step by step walk through:
25+
26+
## 1. Initialize your subgraph project
27+
28+
### Create a subgraph on Subgraph Studio⁠
29+
30+
Go to the [Subgraph Studio](https://thegraph.com/studio/) and connect your wallet. Once your wallet is connected, you can begin by clicking “Create a Subgraph”. When choosing a name, it is recommended to use Title Case: “Subgraph Name Chain Name.”
31+
32+
![Create a Subgraph](https://lh7-us.googleusercontent.com/docsz/AD_4nXf8OTdwMxlKQGKzIF_kYR7NPKeh9TmWnZBYxb7ft_YbdOdx_VVtbp6PslN7N1KGUzNpIDCmaXppdrllM1cw_J4L8Na03BXOWzJTK1POCve0nkRjQYgWJ60QHAdtQ4Niy83SMM8m0F0f-N-AJj4PDqDPlA5M?key=fnI6SyFgXU9SZRNX5C5vPQ)
33+
34+
35+
You will then land on your subgraph’s page. All the CLI commands you need will be visible on the right side of the page:
36+
37+
![CLI commands](https://lh7-us.googleusercontent.com/docsz/AD_4nXe3YvCxiOH_LupSWe8zh9AmP-VrV4PlOq3f7Ix6hNlBUYcANUFuLuVIWR74OGiBs0nrugTyT0v3o6RPmTsgHONdv_ZJNWtcDWEkRntXPHlQGFcqmEBa-D6j4aoIPzUKYdOJMVUPu8O3fwjdZ4IaXXZoTzY?key=fnI6SyFgXU9SZRNX5C5vPQ)
38+
39+
40+
### Install the Graph CLI⁠
41+
42+
On your local machine run the following:
43+
```
44+
npm install -g @graphprotocol/graph-cli
45+
```
46+
47+
### Initialize your Subgraph⁠
48+
49+
You can copy this directly from your subgraph page to include your specific subgraph slug:
50+
```
51+
graph init --studio <SUBGRAPH_SLUG>
52+
```
53+
You’ll be prompted to provide some info on your subgraph like this:
54+
55+
![cli sample](https://lh7-us.googleusercontent.com/docsz/AD_4nXdTAUsUb5vbs3GtCrhKhuXM1xYoqqooYTxw6lfJfYtLJNP8GKVOhTPmjxlM1b6Qpx-pXNVOzRuc8BL12wZXqy4MIj8ja0tp15znfuJD_Mg84SSNj3JpQ4d31lNTxPYnpba4UOzZx8pmgOIsbI7vCz70v9gC?key=fnI6SyFgXU9SZRNX5C5vPQ)
56+
57+
58+
Simply have your contract verified on the block explorer and the CLI will automatically obtain the ABI and set up your subgraph. The default settings will generate an entity for each event.
59+
60+
## 2. Deploy & Publish
61+
62+
### Deploy to Subgraph Studio⁠
63+
64+
First run these commands:
65+
66+
```bash
67+
$ graph codegen
68+
$ graph build
69+
```
70+
71+
Then run these to authenticate and deploy your subgraph. You can copy these commands directly from your subgraph’s page in Studio to include your specific deploy key and subgraph slug:
72+
73+
```bash
74+
$ graph auth --studio <DEPLOY_KEY>
75+
$ graph deploy --studio <SUBGRAPH_SLUG>
76+
```
77+
78+
You will be asked for a version label. You can enter something like v0.0.1, but you’re free to choose the format.
79+
80+
### Test your subgraph⁠
81+
82+
You can test your subgraph by making a sample query in the playground section. The Details tab will show you an API endpoint. You can use that endpoint to test from your dapp.
83+
84+
![Playground](https://lh7-us.googleusercontent.com/docsz/AD_4nXf3afwSins8_eO7BceGPN79VvwolDxmFNUnkPk0zAJCaUA-3-UAAjVvrMzwr7q9vNYWdrEUNgm2De2VfQpWauiT87RkFc-cVfoPSsQbYSgsmwhyY1-tpPdv2J1H4JAMq70nfWBhb8PszZBFjsbDAaJ5eto?key=fnI6SyFgXU9SZRNX5C5vPQ)
85+
86+
87+
### Publish Your Subgraph to The Graph’s Decentralized Network
88+
89+
Once your subgraph is ready to be put into production, you can publish it to the decentralized network. On your subgraph’s page in Subgraph Studio, click on the Publish button:
90+
91+
![publish button](https://edgeandnode.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fa7d6afae-8784-4b15-a90e-ee8f6ee007ba%2F2f9c4526-123d-4164-8ea8-39959c8babbf%2FUntitled.png?table=block&id=37005371-76b4-4780-b044-040a570e3af6&spaceId=a7d6afae-8784-4b15-a90e-ee8f6ee007ba&width=1420&userId=&cache=v2)
92+
93+
94+
Before you can query your subgraph, Indexers need to begin serving queries on it. In order to streamline this process, you can curate your own subgraph using GRT.
95+
96+
When publishing, you’ll see the option to curate your subgraph. As of May 2024, it is recommended that you curate your own subgraph with at least 3,000 GRT to ensure that it is indexed and available for querying as soon as possible.
97+
98+
![Publish screen](https://lh7-us.googleusercontent.com/docsz/AD_4nXerUr-IgWjwBZvp9Idvz5hTq8AFB0n_VlXCzyDtUxKaCTANT4gkk-2O77oW-a0ZWOh3hnqQsY7zcSaLeCQin9XU1NTX1RVYOLFX9MuVxBEqcMryqgnGQKx-MbDnOWKuMoLBhgyVWQereg3cdWtCPcTQKFU?key=fnI6SyFgXU9SZRNX5C5vPQ)
99+
100+
> **Note:** The Graph's smart contracts are all on Arbitrum One, even though your subgraph is indexing data from Ethereum, BSC or any other [supported chain](https://thegraph.com/docs/en/developing/supported-networks/).
101+
102+
## 3. Query your Subgraph
103+
104+
Congratulations! You can now query your subgraph on the decentralized network!
105+
106+
For any subgraph on the decentralized network, you can start querying it by passing a GraphQL query into the subgraph’s query URL which can be found at the top of its Explorer page.
107+
108+
Here’s an example from the [CryptoPunks Ethereum subgraph](https://thegraph.com/explorer/subgraphs/HdVdERFUe8h61vm2fDyycHgxjsde5PbB832NHgJfZNqK) by Messari:
109+
110+
![Query URL](https://lh7-us.googleusercontent.com/docsz/AD_4nXebivsPOUjPHAa3UVtvxoYTFXaGBao9pQOAJvFK0S7Uv0scfL6TcTVjmNCzT4DgsIloAQyrPTCqHjFPtmjyrzoKkfSeV28FjS32F9-aJJm0ILAHey2gqMr7Seu4IqPz2d__QotsWG3OKv2dEghiD74eypzs?key=fnI6SyFgXU9SZRNX5C5vPQ)
111+
112+
113+
The query URL for this subgraph is:
114+
115+
`https://gateway-arbitrum.network.thegraph.com/api/`**[api-key]**`/subgraphs/id/HdVdERFUe8h61vm2fDyycgxjsde5PbB832NHgJfZNqK`
116+
117+
Now, you simply need to  fill in your own API Key to start sending GraphQL queries to this endpoint.
118+
119+
### Getting your own API Key
120+
121+
![API keys](https://lh7-us.googleusercontent.com/docsz/AD_4nXdz7H8hSRf2XqrU0jN3p3KbmuptHvQJbhRHOJh67nBfwh8RVnhTsCFDGA_JQUFizyMn7psQO0Vgk6Vy7cKYH47OyTq5PqycB0xxLyF4kSPsT7hYdMv2MEzAo433sJT6VlQbUAzgPnSxKI9a5Tn3ShSzaxI?key=fnI6SyFgXU9SZRNX5C5vPQ)
122+
123+
124+
In Subgraph Studio, you’ll see the “API Keys” menu at the top of the page. Here you can create API Keys.
125+
126+
## Appendix
127+
128+
### Sample Query
129+
130+
This query shows the most expensive CryptoPunks sold.
131+
132+
```graphql
133+
{
134+
trades(orderBy: priceETH, orderDirection: desc) {
135+
priceETH
136+
tokenId
137+
}
138+
}
139+
140+
```
141+
142+
Passing this into the query URL returns this result:
143+
144+
```
145+
{
146+
"data": {
147+
"trades": [
148+
{
149+
"priceETH": "124457.067524886018255505",
150+
"tokenId": "9998"
151+
},
152+
{
153+
"priceETH": "8000",
154+
"tokenId": "5822"
155+
},
156+
// ...
157+
```
158+
159+
<aside>
160+
💡 Trivia: Looking at the top sales on [CryptoPunks website](https://cryptopunks.app/cryptopunks/topsales) it looks like the top sale is Punk #5822, not #9998. Why? Because they censor the flash-loan sale that happened.
161+
162+
</aside>
163+
164+
### Sample code
165+
166+
```jsx
167+
const axios = require('axios');
168+
169+
const graphqlQuery = `{
170+
trades(orderBy: priceETH, orderDirection: desc) {
171+
priceETH
172+
tokenId
173+
}
174+
}`;
175+
const queryUrl = 'https://gateway-arbitrum.network.thegraph.com/api/[api-key]/subgraphs/id/HdVdERFUe8h61vm2fDyycHgxjsde5PbB832NHgJfZNqK'
176+
177+
const graphQLRequest = {
178+
method: 'post',
179+
url: queryUrl,
180+
data: {
181+
query: graphqlQuery,
182+
},
183+
};
184+
185+
// Send the GraphQL query
186+
axios(graphQLRequest)
187+
.then((response) => {
188+
// Handle the response here
189+
const data = response.data.data
190+
console.log(data)
191+
192+
})
193+
.catch((error) => {
194+
// Handle any errors
195+
console.error(error);
196+
});
197+
```
198+
199+
### Additional resources:
200+
201+
- To explore all the ways you can optimize & customize your subgraph for a better performance, read more about [creating a subgraph here](https://thegraph.com/docs/en/developing/creating-a-subgraph/).
202+
- For more information about querying data from your subgraph, read more [here](https://thegraph.com/docs/en/querying/querying-the-graph/).

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ nav:
527527
- PARSIQ: tools/data/parsiq.md
528528
- SQD (Subsquid): tools/data/sqd.md
529529
- SubQuery: tools/data/subquery.md
530+
- The Graph: tools/data/thegraph.md
530531
- Storage:
531532
- IPFS: tools/storage/ipfs.md
532533
- Filecoin: tools/storage/filecoinhelpers.md

0 commit comments

Comments
 (0)