Skip to content

Commit 0f2ee61

Browse files
author
sam bacha
authored
docs(readme): subgraph api
1 parent c4852ce commit 0f2ee61

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

README.md

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,261 @@ function handleNameRegistered(event: NameRegistered) {
4545
}
4646
```
4747

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.
78+
79+
#### Example
80+
```graphql
81+
{
82+
tokens(first: 100, orderBy: price, orderDirection: asc) {
83+
id
84+
owner
85+
}
86+
}
87+
```
88+
89+
### 1.3 Pagination
90+
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.
108+
109+
#### Example
110+
Query challenges with `failed` outcome:
111+
112+
```graphql
113+
{
114+
challenges(first: 100, where: {outcome: "failed"}) {
115+
challenger
116+
outcome
117+
application(first: 100) {
118+
id
119+
}
120+
}
121+
}
122+
```
123+
124+
You can use suffixes like `_gt`, `_lte` for value comparison:
125+
126+
#### Example
127+
```graphql
128+
{
129+
applications(first: 100, where: {deposit_gt:"10000000000"}) {
130+
id
131+
whitelisted
132+
deposit
133+
}
134+
}
135+
```
136+
137+
Full list of parameter suffixes:
138+
```
139+
_not
140+
_gt
141+
_lt
142+
_gte
143+
_lte
144+
_in
145+
_not_in
146+
_contains
147+
_not_contains
148+
_starts_with
149+
_ends_with
150+
_not_starts_with
151+
_not_ends_with
152+
```
153+
154+
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+
type Token @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+
All the scalars defined in the GraphQL spec are supported: `Int`, `Float`, `String`, `Boolean`, and `ID`.
220+
221+
#### 3.3.2 Bytes
222+
There is a `Bytes` scalar for variable-length byte arrays.
223+
224+
#### 3.3.2 Numbers
225+
The GraphQL spec defines `Int` and `Float` to have sizes of 32 bytes.
226+
227+
This API additionally includes a `BigInt` number type to represent arbitrarily large integer numbers.
228+
229+
### 3.4 Enums
230+
231+
You can also create `enums` within a schema. 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:
255+
```graphql
256+
type Transaction @entity {
257+
id: ID!
258+
transactionReceipt: TransactionReceipt
259+
}
260+
261+
type TransactionReceipt @entity {
262+
id: ID!
263+
transaction: Transaction
264+
}
265+
```
266+
267+
#### Example
268+
Define a `Token` entity type with a required one-to-many relationship with a `TokenBalance` entity type:
269+
```graphql
270+
type Token @entity {
271+
id: ID!
272+
tokenBalances: [TokenBalance!]!
273+
}
274+
275+
type TokenBalance @entity {
276+
id: ID!
277+
amount: Int!
278+
}
279+
```
280+
281+
#### 3.5.2 Reverse Lookups
282+
Defining reverse lookups can be defined on an entity through the `@derivedFrom` field. This creates a virtual field on the entity that may be queried but cannot be set manually through the mappings API. Rather, it is derived from the relationship defined on the other entity.
283+
284+
The type of an `@derivedFrom` field must be a collection since multiple entities may specify relationships to a single entity.
285+
286+
#### Example
287+
Define a reverse lookup from a `User` entity type to an `Organization` entity type:
288+
```graphql
289+
type Organization @entity {
290+
id: ID!
291+
name: String!
292+
members: [User]!
293+
}
294+
295+
type User @entity {
296+
id: ID!
297+
name: String!
298+
organizations: [Organization!] @derivedFrom(field: "members")
299+
}
300+
```
301+
302+
48303
## License
49304

50305
Apache-2.0 or MIT

0 commit comments

Comments
 (0)