Skip to content

Commit 983c544

Browse files
authored
Add some documentation about queries and mutations (#761)
1 parent 1441d2c commit 983c544

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

core/graphql.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,97 @@ api_platform:
3535
# ...
3636
```
3737

38+
## Queries
39+
40+
If you don't know what queries are yet, the documentation about them is [here](https://graphql.org/learn/queries/).
41+
42+
For each resource, two queries are available: one for retrieving an item and the other one for the collection.
43+
For example, if you have a `Book` resource, the queries `book` and `books` can be used.
44+
45+
### Global Object Identifier
46+
47+
When querying an item, you need to pass an identifier as argument. Following the [Relay Global Object Identification Specification](https://facebook.github.io/relay/graphql/objectidentification.htm),
48+
the identifier needs to be globally unique. In API Platform, this argument is represented as an [IRI (Internationalized Resource Identifier)](https://www.w3.org/TR/ld-glossary/#internationalized-resource-identifier).
49+
50+
For example, to query a book having as identifier `89`, you have to run the following:
51+
52+
```graphql
53+
{
54+
book(id: "/books/89") {
55+
title
56+
isbn
57+
}
58+
}
59+
```
60+
61+
Note that in this example, we're retrieving two fields: `title` and `isbn`.
62+
63+
### Pagination
64+
65+
API Platform natively enables a cursor-based pagination for collections.
66+
It supports [GraphQL's Complete Connection Model](https://graphql.org/learn/pagination/#complete-connection-model) and is compatible with [Relay's Cursor Connections Specification](https://facebook.github.io/relay/graphql/connections.htm).
67+
68+
Here is an example query leveraging the pagination system:
69+
70+
```graphql
71+
{
72+
offers(first: 10, after: "cursor") {
73+
totalCount
74+
pageInfo {
75+
endCursor
76+
hasNextPage
77+
}
78+
edges {
79+
cursor
80+
node {
81+
id
82+
}
83+
}
84+
}
85+
}
86+
```
87+
88+
The two parameters `first` and `after` are necessary to make the paginated query work, more precisely:
89+
90+
* `first` corresponds to the items per page starting from the beginning;
91+
* `after` corresponds to the `cursor` from which the items are returned.
92+
93+
The current page always has an `endCursor` present in the `pageInfo` field.
94+
To get the next page, you would add the `endCursor` from the current page as the `after` parameter.
95+
96+
```graphql
97+
{
98+
offers(first: 10, after: "endCursor") {
99+
}
100+
}
101+
```
102+
103+
When the property `hasNextPage` of the `pageInfo` field is false, you've reached the last page.
104+
If you move forward, you'll end up having an empty result.
105+
106+
## Mutations
107+
108+
If you don't know what mutations are yet, the documentation about them is [here](https://graphql.org/learn/queries/#mutations).
109+
110+
For each resource, three mutations are available: one for creating it (`create`), one for updating it (`update`) and one for deleting it (`delete`).
111+
112+
When updating or deleting a resource, you need to pass the **IRI** of the resource as argument. See [Global Object Identifier](#global-object-identifier) for more information.
113+
114+
### Client Mutation Id
115+
116+
Following the [Relay Input Object Mutations Specification](https://facebook.github.io/relay/graphql/mutations.htm),
117+
you can pass a `clientMutationId` as argument and can ask its value as a field.
118+
119+
For example, if you delete a book:
120+
121+
```graphql
122+
mutation DeleteBook($id: ID!, $clientMutationId: String!) {
123+
deleteBook(input: {id: $id, clientMutationId: $clientMutationId}) {
124+
clientMutationId
125+
}
126+
}
127+
```
128+
38129
## Filters
39130

40131
Filters are supported out-of-the-box. Follow the [filters](filters.md) documentation and your filters will be available as arguments of queries.

0 commit comments

Comments
 (0)