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/src/pages/en/subgraphs/querying/graphql-api.mdx
+34-51Lines changed: 34 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,31 @@ title: GraphQL API
4
4
5
5
Explore the GraphQL Query API for interacting with Subgraphs on The Graph Network.
6
6
7
-
## Introduction
7
+
## What is GraphQL?
8
8
9
-
GraphQL serves as the query language for retrieving data from Subgraphs. This reference covers the syntax, parameters, and features available for querying Subgraph data. For foundational concepts, see [Subgraph Development](/subgraphs/developing/introduction/) and [Creating a Subgraph](/developing/creating-a-subgraph/).
9
+
[GraphQL](https://graphql.org/learn/) is a query language for APIs and a runtime for executing those queries with your existing data. The Graph uses GraphQL to query Subgraphs.
10
10
11
11
## Core Concepts
12
12
13
13
### Entities
14
-
-**Definition**: Types annotated with `@entity` in your schema represent queryable data objects.
15
-
-**Structure**: Each entity requires an `id: ID!` field as its primary identifier.
14
+
-**What they are**: Persistent data objects defined with `@entity` in your schema
15
+
-**Key requirement**: Must contain `id: ID!` as primary identifier
16
+
-**Usage**: Foundation for all query operations
16
17
17
18
### Schema
18
-
-**Role**: Defines the data structure and relationships via GraphQL's Interface Definition Language (IDL).
19
-
-**Query Type**: Auto-generated from your Subgraph schema. Supports only read operations (`queries`).
20
-
21
-
22
-
## What is GraphQL?
23
-
24
-
[GraphQL](https://graphql.org/learn/) is a query language for APIs and a runtime for executing those queries with your existing data. The Graph uses GraphQL to query Subgraphs.
19
+
-**Purpose**: Blueprint defining the data structure and relationships using GraphQL [IDL](https://facebook.github.io/graphql/draft/#sec-Type-System)
20
+
-**Key characteristics**:
21
+
- Auto-generates query endpoints
22
+
- Read-only operations (no mutations)
23
+
- Defines entity interfaces and derived fields
25
24
26
25
## Queries with GraphQL
27
26
28
-
In your Subgraph schema you define types called `Entities`. For each `Entity` type, `entity` and `entities` fields will be generated on the top-level `Query` type.
27
+
In the Subgraph schema, types called `Entities`. For each `Entity` type, `entity` and `entities` fields will be generated on the top-level `Query` type.
29
28
30
-
> Note: `query` does not need to be included at the top of the `graphql` query when using The Graph.
29
+
### Example Queries
31
30
32
-
### Examples
31
+
> Note: `query` does not need to be included at the top of the `graphql` query when using The Graph.
33
32
34
33
Query for a single `Token` entity defined in your schema:
35
34
@@ -73,7 +72,7 @@ When querying a collection, you can:
73
72
}
74
73
```
75
74
76
-
#### Example for nested entity sorting
75
+
#### Example for Nested Entity Sorting
77
76
78
77
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.
79
78
@@ -101,7 +100,7 @@ When querying a collection, it's best to:
101
100
- Use the `skip` parameter to skip entities and paginate. For instance, `first:100` shows the first 100 entities and `first:100, skip:100` shows the next 100 entities.
102
101
- 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.
103
102
104
-
#### Example using`first`
103
+
#### Example Using`first`
105
104
106
105
Query the first 10 tokens:
107
106
@@ -116,7 +115,7 @@ Query the first 10 tokens:
116
115
117
116
To query for groups of entities in the middle of a collection, the `skip` parameter can be used in conjunction with the `first` parameter to skip a specified number of entities starting at the beginning of the collection.
118
117
119
-
#### Example using`first` and `skip`
118
+
#### Example Using`first` and `skip`
120
119
121
120
Query 10 `Token` entities, offset by 10 places from the beginning of the collection:
122
121
@@ -129,7 +128,7 @@ Query 10 `Token` entities, offset by 10 places from the beginning of the collect
129
128
}
130
129
```
131
130
132
-
#### Example using`first` and `id_ge`
131
+
#### Example Using`first` and `id_ge`
133
132
134
133
If a client needs to retrieve a large number of entities, it's more performant 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:
135
134
@@ -149,9 +148,9 @@ The first time, it would send the query with `lastID = ""`, and for subsequent r
149
148
- You can use the `where` parameter in your queries to filter for different properties.
150
149
- You can filter on multiple values within the `where` parameter.
151
150
152
-
#### Example using `where`
151
+
#### `where` Filtering
153
152
154
-
Query challenges with `failed` outcome:
153
+
Query challenges with `failed` outcome using 'where' filter:
155
154
156
155
```graphql
157
156
{
@@ -167,7 +166,7 @@ Query challenges with `failed` outcome:
167
166
168
167
You can use suffixes like `_gt`, `_lte` for value comparison:
169
168
170
-
#### Example for range filtering
169
+
#### Range Filtering
171
170
172
171
```graphql
173
172
{
@@ -179,7 +178,7 @@ You can use suffixes like `_gt`, `_lte` for value comparison:
179
178
}
180
179
```
181
180
182
-
#### Example for block filtering
181
+
#### Block Filtering
183
182
184
183
You can also filter entities that were updated in or after a specified block with `_change_block(number_gte: Int)`.
185
184
@@ -195,7 +194,7 @@ This can be useful if you are looking to fetch only entities which have changed,
195
194
}
196
195
```
197
196
198
-
#### Example for nested entity filtering
197
+
#### Nested Entity Filtering
199
198
200
199
Filtering on the basis of nested entities is possible in the fields with the `_` suffix.
201
200
@@ -213,7 +212,7 @@ This can be useful if you are looking to fetch only entities whose child-level e
213
212
}
214
213
```
215
214
216
-
####Logical operators
215
+
### Logical Operators
217
216
218
217
As of Graph Node [`v0.30.0`](https://github.com/graphprotocol/graph-node/releases/tag/v0.30.0) you can group multiple parameters in the same `where` argument using the `and` or the `or` operators to filter results based on more than one criteria.
219
218
@@ -263,7 +262,7 @@ The following example filters for challenges with `outcome` `succeeded` or `numb
263
262
}
264
263
```
265
264
266
-
> **Note**: When constructing queries, it is important to consider the performance impact of using the `or` operator. While `or` can be a useful tool for broadening search results, it can also have significant costs. One of the main issues with `or` is that it can cause queries to slow down. This is because `or` requires the database to scan through multiple indexes, which can be a time-consuming process. To avoid these issues, it is recommended that developers use and operators instead of or whenever possible. This allows for more precise filtering and can lead to faster, more accurate queries.
265
+
> **Note**: When writing queries, it is important to consider the performance impact of using the `or` operator. While `or` can be a useful tool for broadening search results, it can also have significant costs. One of the main issues with `or` is that it can cause queries to slow down. This is because `or` requires the database to scan through multiple indexes, which can be a time-consuming process. To avoid these issues, it is recommended that developers use and operators instead of or whenever possible. This allows for more precise filtering and can lead to faster, more accurate queries.
267
266
268
267
#### All Filters
269
268
@@ -300,15 +299,15 @@ In addition, the following global filters are available as part of `where` argum
300
299
_change_block(number_gte: Int)
301
300
```
302
301
303
-
### Time-travel queries
302
+
### Time-travel Queries
304
303
305
304
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.
306
305
307
306
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.
308
307
309
308
> 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.
310
309
311
-
#### Example
310
+
#### Example Time-travel Queries
312
311
313
312
```graphql
314
313
{
@@ -324,8 +323,6 @@ The result of such a query will not change over time, i.e., querying at a certai
324
323
325
324
This query will return `Challenge` entities, and their associated `Application` entities, as they existed directly after processing block number 8,000,000.
@@ -340,13 +337,13 @@ This query will return `Challenge` entities, and their associated `Application`
340
337
341
338
This query will return `Challenge` entities, and their associated `Application` entities, as they existed directly after processing the block with the given hash.
342
339
343
-
### Fulltext Search Queries
340
+
### Full-text Search Queries
344
341
345
-
Fulltext search query fields provide an expressive text search API that can be added to the Subgraph schema and customized. Refer to [Defining Fulltext Search Fields](/developing/creating-a-subgraph/#defining-fulltext-search-fields) to add fulltext search to your Subgraph.
342
+
Full-text search query fields provide an expressive text search API that can be added to the Subgraph schema and customized. Refer to [Defining Full-text Search Fields](/developing/creating-a-subgraph/#defining-fulltext-search-fields) to add full-text search to your Subgraph.
346
343
347
-
Fulltext search queries have one required field, `text`, for supplying search terms. Several special fulltext operators are available to be used in this `text` search field.
344
+
Full-text search queries have one required field, `text`, for supplying search terms. Several special full-text operators are available to be used in this `text` search field.
348
345
349
-
Fulltext search operators:
346
+
Full-text search operators:
350
347
351
348
| Symbol | Operator | Description |
352
349
| --- | --- | --- |
@@ -355,9 +352,9 @@ Fulltext search operators:
355
352
|`<->`|`Follow by`| Specify the distance between two words. |
356
353
|`:*`|`Prefix`| Use the prefix search term to find words whose prefix match (2 characters required.) |
357
354
358
-
#### Examples
355
+
#### Full-text Query Examples
359
356
360
-
Using the `or` operator, this query will filter to blog entities with variations of either "anarchism" or "crumpet" in their fulltext fields.
357
+
Using the `or` operator, this query will filter to blog entities with variations of either "anarchism" or "crumpet" in their full-text fields.
361
358
362
359
```graphql
363
360
{
@@ -370,7 +367,7 @@ Using the `or` operator, this query will filter to blog entities with variations
370
367
}
371
368
```
372
369
373
-
The `follow by` operator specifies a words a specific distance apart in the fulltext documents. The following query will return all blogs with variations of "decentralize" followed by "philosophy"
370
+
The `follow by` operator specifies that two words must appear a specific distance apart in full-text documents.. The following query will return all blogs with variations of "decentralize" followed by "philosophy"
374
371
375
372
```graphql
376
373
{
@@ -383,7 +380,7 @@ The `follow by` operator specifies a words a specific distance apart in the full
383
380
}
384
381
```
385
382
386
-
Combine fulltext operators to make more complex filters. With a pretext search operator combined with a follow by this example query will match all blog entities with words that start with "lou" followed by "music".
383
+
Combine full-text operators to make more complex filters. With a pretext search operator combined with a follow by this example query will match all blog entities with words that start with "lou" followed by "music".
387
384
388
385
```graphql
389
386
{
@@ -400,20 +397,6 @@ Combine fulltext operators to make more complex filters. With a pretext search o
400
397
401
398
Graph Node implements [specification-based](https://spec.graphql.org/October2021/#sec-Validation) validation of the GraphQL queries it receives using [graphql-tools-rs](https://github.com/dotansimha/graphql-tools-rs#validation-rules), which is based on the [graphql-js reference implementation](https://github.com/graphql/graphql-js/tree/main/src/validation). Queries which fail a validation rule do so with a standard error - visit the [GraphQL spec](https://spec.graphql.org/October2021/#sec-Validation) to learn more.
402
399
403
-
## Schema
404
-
405
-
The schema of your dataSources, i.e. the entity types, values, and relationships that are available to query, are defined through the [GraphQL Interface Definition Language (IDL)](https://facebook.github.io/graphql/draft/#sec-Type-System).
406
-
407
-
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).
408
-
409
-
> Note: Our API does not expose mutations because developers are expected to issue transactions directly against the underlying blockchain from their applications.
410
-
411
-
### Entities
412
-
413
-
All GraphQL types with `@entity` directives in your schema will be treated as entities and must have an `ID` field.
414
-
415
-
> **Note:** Currently, all types in your schema must have an `@entity` directive. In the future, we will treat types without an `@entity` directive as value objects, but this is not yet supported.
416
-
417
400
### Subgraph Metadata
418
401
419
402
All Subgraphs have an auto-generated `_Meta_` object, which provides access to Subgraph metadata. This can be queried as follows:
0 commit comments