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: README.md
+255Lines changed: 255 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,261 @@ function handleNameRegistered(event: NameRegistered) {
45
45
}
46
46
```
47
47
48
+
## GraphQL API
49
+
50
+
## 1 Queries
51
+
### 1.1 Basics
52
+
For each `Entity` type that you define in your schema, 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.
53
+
54
+
#### Example
55
+
Query for a single `Token` entity defined in your schema:
56
+
```graphql
57
+
{
58
+
token(id: "1") {
59
+
id
60
+
owner
61
+
}
62
+
}
63
+
```
64
+
When querying for a single entity, the `id` field is required.
65
+
66
+
#### Example
67
+
Collection query for `Token` entities:
68
+
```graphql
69
+
{
70
+
tokens(first: 100) {
71
+
id
72
+
owner
73
+
}
74
+
}
75
+
```
76
+
### 1.2 Sorting
77
+
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.
When querying a collection, the `first` parameter must be used to paginate from the beginning of the collection.
91
+
92
+
To query for groups of entities in the middle of a collection, the `skip` parameter may be used to skip a specified number of entities starting at the beginning of the collection.
93
+
94
+
#### Example
95
+
Query 10 `Token` entities, offset by 10:
96
+
```graphql
97
+
{
98
+
tokens(first: 10, skip: 10) {
99
+
id
100
+
owner
101
+
}
102
+
}
103
+
```
104
+
105
+
### 1.4 Filtering
106
+
107
+
You can use the `where` parameter in your queries to filter for different properties.
Please note that some suffixes are only supported for specific types. For example, `Boolean` only supports `_not`, `_in`, and `_not_in`.
155
+
156
+
## 2 Subscriptions
157
+
Graph Protocol subscriptions are GraphQL spec-compliant subscriptions. Unlike query operations, GraphQL subscriptions may only have a single top-level field at the root level for each subscription operation.
158
+
159
+
### 2.1 Basics
160
+
The root Subscription type for subscription operations mimics the root Query type used for query operations to minimize the cognitive overhead for writing subscriptions.
161
+
162
+
#### Example
163
+
Query the first 100 `Token` entities along with their `id` and `owner` attributes:
164
+
165
+
```graphql
166
+
query {
167
+
tokens(first: 100) {
168
+
id
169
+
owner
170
+
}
171
+
}
172
+
```
173
+
174
+
Subscribe to all `Token` entity changes and fetch the values of the `id` and `owner` attributes on the updated entity:
175
+
176
+
```graphql
177
+
subscription {
178
+
tokens(first: 100) {
179
+
id
180
+
owner
181
+
}
182
+
}
183
+
```
184
+
185
+
## 3 Schema
186
+
187
+
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)](http://facebook.github.io/graphql/draft/#sec-Type-System).
188
+
189
+
### 3.1 Basics
190
+
191
+
GraphQL requests consist of three basic operations: `query`, `subscription`, and `mutation`. Each of these has a corresponding root level `Query`, `Subscription`, and `Mutation` type in the schema of a GraphQL endpoint.
192
+
193
+
> **Note:** Our API does not expose mutations because developers are expected to issue transactions directly against the underlying blockchain from their applications.
194
+
195
+
It is typical for developers to define their own root `Query` and `Subscription` types when building a GraphQL API server, but with The Graph, we generate these top-level types based on the entities that you define in your schema as well as several other types for exploring blockchain data, which we describe in depth in the [Query API](#Basics).
196
+
197
+
### 3.2 Entities
198
+
199
+
All GraphQL types with `@entity` directives in your schema will be treated as entities and must have an `ID` field.
200
+
201
+
> **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.
202
+
203
+
#### Example
204
+
Define a `Token` entity:
205
+
206
+
```graphql
207
+
typeToken@entity {
208
+
# The unique ID of this entity
209
+
id: ID!
210
+
name: String!
211
+
symbol: String!
212
+
decimals: Int!
213
+
}
214
+
```
215
+
216
+
### 3.3 Built-In Types
217
+
218
+
#### 3.3.1 GraphQL Built-In Scalars
219
+
AllthescalarsdefinedintheGraphQLspecaresupported: `Int`, `Float`, `String`, `Boolean`, and `ID`.
Youcanalsocreate `enums` withinaschema. Enums have the following syntax:
232
+
233
+
```graphql
234
+
enum TokenStatus {
235
+
OriginalOwner,
236
+
SecondOwner,
237
+
ThirdOwner,
238
+
}
239
+
```
240
+
241
+
To set a store value with an enum, use the name of the enum value as a string. In the example above, you can set the `TokenStatus` to the second owner with `SecondOwner`.
242
+
More detail on writing enums can be found in the [GraphQL documentation](https://graphql.org/learn/schema/).
243
+
244
+
### 3.5 Entity Relationships
245
+
An entity may have a relationship to one or more other entities in your schema. These relationships may be traversed in your queries and subscriptions.
246
+
247
+
Relationships in The Graph are unidirectional. Despite this, relationships may be traversed in *either* direction by defining reverse lookups on an entity.
248
+
249
+
#### 3.5.1 Basics
250
+
251
+
Relationships are defined on entities just like any other scalar type except that the type specified is that of another entity.
252
+
253
+
#### Example
254
+
Define a `Transaction` entity type with an optional one-to-one relationship with a `TransactionReceipt` entity type:
0 commit comments