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/querying/graphql-api.mdx
+33-20Lines changed: 33 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,19 @@
2
2
title: GraphQL API
3
3
---
4
4
5
-
This guide explains the GraphQL Query API that is used for The Graph Protocol.
5
+
Learn about the GraphQL Query API used in The Graph.
6
6
7
-
## Queries
7
+
## What is GraphQL?
8
8
9
-
In your subgraph schema you define types called `Entities`. For each `Entity` type, an `entity` and `entities` field will be generated on the top-level `Query` type. Note that `query` does not need to be included at the top of the `graphql` query when using The Graph.
9
+
[GraphQL](https://graphql.org/learn/) is the query language for APIs and a runtime for executing those queries with your existing data. The Graph uses GraphQL to query subgraphs.
10
+
11
+
- To understand the larger role that GraphQL plays, review [developing](/network/developing/) and [creating a subgraph](/developing/creating-a-subgraph/).
12
+
13
+
## Queries with GraphQL
14
+
15
+
In your subgraph schema you define types called `Entities`. For each `Entity` type, an `entity` and `entities` field will be generated on the top-level `Query` type.
16
+
17
+
> Note: `query` does not need to be included at the top of the `graphql` query when using The Graph.
10
18
11
19
### Examples
12
20
@@ -21,7 +29,7 @@ Query for a single `Token` entity defined in your schema:
21
29
}
22
30
```
23
31
24
-
> **Note:** When querying for a single entity, the `id` field is required, and it must be a string.
32
+
> Note: When querying for a single entity, the `id` field is required, and it must be writen as a string.
25
33
26
34
Query all `Token` entities:
27
35
@@ -36,7 +44,10 @@ Query all `Token` entities:
36
44
37
45
### Sorting
38
46
39
-
When querying a collection, the `orderBy` parameter may be used to sort by a specific attribute. Additionally, the `orderDirection` can be used to specify the sort direction, `asc` for ascending or `desc` for descending.
47
+
When querying a collection, you may:
48
+
49
+
- Use the `orderBy` parameter to sort by a specific attribute.
50
+
- Use the `orderDirection` to specify the sort direction, `asc` for ascending or `desc` for descending.
40
51
41
52
#### Example
42
53
@@ -53,7 +64,7 @@ When querying a collection, the `orderBy` parameter may be used to sort by a spe
53
64
54
65
As of Graph Node [`v0.30.0`](https://github.com/graphprotocol/graph-node/releases/tag/v0.30.0) entities can be sorted on the basis of nested entities.
55
66
56
-
In the following example, we sort the tokens by the name of their owner:
67
+
The following example shows tokens sorted by the name of their owner:
57
68
58
69
```graphql
59
70
{
@@ -70,11 +81,12 @@ In the following example, we sort the tokens by the name of their owner:
70
81
71
82
### Pagination
72
83
73
-
When querying a collection, the `first` parameter can be used to paginate from the beginning of the collection. It is worth noting that the default sort order is by ID in ascending alphanumeric order, not by creation time.
74
-
75
-
Further, the `skip` parameter can be used to skip entities and paginate. e.g. `first:100` shows the first 100 entities and `first:100, skip:100` shows the next 100 entities.
84
+
When querying a collection, it's best to:
76
85
77
-
Queries should avoid using very large `skip` values since they generally perform poorly. For retrieving a large number of items, it is much better to page through entities based on an attribute as shown in the last example.
86
+
- Use the `first` parameter to paginate from the beginning of the collection.
87
+
- The default sort order is by `ID` in ascending alphanumeric order, **not** by creation time.
88
+
- Use The `skip` parameter to skip entities and paginate. e.g. `first:100` shows the first 100 entities and `first:100, skip:100` shows the next 100 entities.
89
+
- Avoid using `skip` values in queries because they generally perform poorly. To retrieve a large number of items, it's best to page through entities based on an attribute as shown in the previous example above.
78
90
79
91
#### Example using `first`
80
92
@@ -106,7 +118,7 @@ Query 10 `Token` entities, offset by 10 places from the beginning of the collect
106
118
107
119
#### Example using `first` and `id_ge`
108
120
109
-
If a client needs to retrieve a large number of entities, it is much more performant to base queries on an attribute and filter by that attribute. For example, a client would retrieve a large number of tokens using this query:
121
+
If a client needs to retrieve a large number of entities, it's best to base queries on an attribute and filter by that attribute. For example, a client could retrieve a large number of tokens using this query:
The first time, it would send the query with `lastID = ""`, and for subsequent requests would set `lastID` to the `id` attribute of the last entity in the previous request. This approach will perform significantly better than using increasing `skip` values.
132
+
The first time, it would send the query with `lastID = ""`, and for subsequent requests it would set `lastID` to the `id` attribute of the last entity in the previous request. This approach will perform significantly better than using increasing `skip` values.
121
133
122
134
### Filtering
123
135
124
-
You can use the `where` parameter in your queries to filter for different properties. You can filter on multiple values within the `where` parameter.
136
+
- You can use the `where` parameter in your queries to filter for different properties.
137
+
- You can filter on multiple values within the `where` parameter.
125
138
126
139
#### Example using `where`
127
140
@@ -155,7 +168,7 @@ You can use suffixes like `_gt`, `_lte` for value comparison:
155
168
156
169
#### Example for block filtering
157
170
158
-
You can also filter entities by the `_change_block(number_gte: Int)` - this filters entities which were updated in or after the specified block.
171
+
You can also filter entities by the `_change_block(number_gte: Int)`, which filters entities that were updated in or after the specified block.
159
172
160
173
This can be useful if you are looking to fetch only entities which have changed, for example since the last time you polled. Or alternatively it can be useful to investigate or debug how entities are changing in your subgraph (if combined with a block filter, you can isolate only entities that changed in a specific block).
161
174
@@ -193,7 +206,7 @@ As of Graph Node [`v0.30.0`](https://github.com/graphprotocol/graph-node/release
193
206
194
207
##### `AND` Operator
195
208
196
-
In the following example, we are filtering for challenges with `outcome``succeeded` and `number` greater than or equal to `100`.
209
+
The following example filters for challenges with `outcome``succeeded` and `number` greater than or equal to `100`.
197
210
198
211
```graphql
199
212
{
@@ -223,7 +236,7 @@ In the following example, we are filtering for challenges with `outcome` `succee
223
236
224
237
##### `OR` Operator
225
238
226
-
Inthefollowingexample, wearefilteringforchallengeswith `outcome` `succeeded` or `number` greaterthanorequalto `100`.
239
+
Thefollowingexamplefiltersforchallengeswith `outcome` `succeeded` or `number` greaterthanorequalto `100`.
You can query the state of your entities not just for the latest block, which is the default, but also for an arbitrary block in the past. The block at which a query should happen can be specified either by its block number or its block hash by including a `block` argument in the toplevel fields of queries.
280
293
281
-
The result of such a query will not change over time, i.e., querying at a certain past block will return the same result no matter when it is executed, with the exception that if you query at a block very close to the head of the chain, the result might change if that block turns out to not be on the main chain and the chain gets reorganized. Once a block can be considered final, the result of the query will not change.
294
+
-The result of such a query will not change over time, i.e., querying at a certain past block will return the same result no matter when it is executed, with the exception that if you query at a block very close to the head of the chain, the result might change if that block turns out to **not** be on the main chain and the chain gets reorganized. Once a block can be considered final, the result of the query will not change.
282
295
283
-
Note that the current implementation is still subject to certain limitations that might violate these guarantees. The implementation can not always tell that a given block hash is not on the main chain at all, or that the result of a query by block hash for a block that can not be considered final yet might be influenced by a block reorganization running concurrently with the query. They do not affect the results of queries by block hash when the block is final and known to be on the main chain. [This issue](https://github.com/graphprotocol/graph-node/issues/1405) explains what these limitations are in detail.
296
+
> Note: The current implementation is still subject to certain limitations that might violate these guarantees. The implementation can not always tell that a given block hash is not on the main chain at all, or if a query result by a block hash for a block that is not yet considered final could be influenced by a block reorganization running concurrently with the query. They do not affect the results of queries by block hash when the block is final and known to be on the main chain. [This issue](https://github.com/graphprotocol/graph-node/issues/1405) explains what these limitations are in detail.
The schema of your data source--that is, the entity types, values, and relationships that are available to query--are defined through the [GraphQL Interface Definition Langauge (IDL)](https://facebook.github.io/graphql/draft/#sec-Type-System).
392
+
The schema of your `data source`, which is the entity types, values, and relationships that are available to query, are defined through the [GraphQL Interface Definition Langauge (IDL)](https://facebook.github.io/graphql/draft/#sec-Type-System).
380
393
381
-
GraphQL schemas generally define root types for `queries`, `subscriptions` and `mutations`. The Graph only supports `queries`. The root `Query` type for your subgraph is automatically generated from the GraphQL schema that's included in your subgraph manifest.
394
+
GraphQL schemas generally define root types for `queries`, `subscriptions` and `mutations`. The Graph only supports `queries`. The root `Query` type for your subgraph is automatically generated from the GraphQL schema that's included in your [subgraph manifest](/developing/creating-a-subgraph/#components-of-a-subgraph).
382
395
383
396
> **Note:** Our API does not expose mutations because developers are expected to issue transactions directly against the underlying blockchain from their applications.
0 commit comments