Skip to content

Commit f95bf02

Browse files
committed
Add documentation about custom types, queries and mutations
1 parent 4557041 commit f95bf02

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

core/graphql.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ final class BookCollectionResolver implements QueryCollectionResolverInterface
129129
*/
130130
public function __invoke($collection, array $context)
131131
{
132+
// Query arguments are in $context['args'].
133+
132134
foreach ($collection as $book) {
133135
// Do something with the book.
134136
}
@@ -170,6 +172,8 @@ final class BookResolver implements QueryItemResolverInterface
170172
*/
171173
public function __invoke($item, array $context)
172174
{
175+
// Query arguments are in $context['args'].
176+
173177
// Do something with the book.
174178
// Or fetch the book if it has not been retrieved.
175179

@@ -235,6 +239,8 @@ Conversely, if you need to add custom arguments, make sure `id` is added among t
235239
- If you have added your [own custom types](#custom-types), you can use them directly for your arguments types (it's the case here for `DateTime`).
236240
- You can also add a custom description for your custom arguments. You can see the [field arguments documentation](https://webonyx.github.io/graphql-php/type-system/object-types/#field-arguments) for more options.
237241

242+
The arguments you have defined or the default ones and their value will be in `$context['args']` of your resolvers.
243+
238244
You custom queries will be available like this:
239245

240246
```graphql
@@ -284,6 +290,103 @@ mutation DeleteBook($id: ID!, $clientMutationId: String!) {
284290
}
285291
```
286292

293+
### Custom mutations
294+
295+
Creating custom mutations is like creating the [custom queries](#custom-queries).
296+
297+
Create your resolver:
298+
299+
```php
300+
<?php
301+
302+
namespace App\Resolver;
303+
304+
use ApiPlatform\Core\GraphQl\Resolver\MutationResolverInterface;
305+
use App\Model\Book;
306+
307+
final class BookMutationResolver implements MutationResolverInterface
308+
{
309+
/**
310+
* @param Book|null $item
311+
*
312+
* @return Book
313+
*/
314+
public function __invoke($item, array $context)
315+
{
316+
// Mutation input arguments are in $context['args']['input'].
317+
318+
// Do something with the book.
319+
// Or fetch the book if it has not been retrieved.
320+
321+
// The returned item will pe persisted.
322+
return $item;
323+
}
324+
}
325+
```
326+
327+
As you can see, depending on how you will define this resolver in the resource, the item is retrieved or not.
328+
Likewise, if you don't want your item to be persisted by API Platform, you can return `null` instead of the mutated item.
329+
Don't forget the resolver is a service and you can inject the dependencies you want.
330+
331+
If you don't use autoconfiguration, add the tag `api_platform.graphql.mutation_resolver` to the resolver service.
332+
333+
Now in your resource:
334+
335+
```php
336+
<?php
337+
338+
namespace App\Model;
339+
340+
use ApiPlatform\Core\Annotation\ApiResource;
341+
use App\Resolver\BookMutationResolver;
342+
343+
/**
344+
* @ApiResource(graphql={
345+
* "mutation"={
346+
* "mutation"=BookMutationResolver::class
347+
* },
348+
* "withCustomArgsMutation"={
349+
* "mutation"=BookMutationResolver::class,
350+
* "args"={
351+
* "sendMail"={"type"="Boolean!", "description"="Send a mail?"}
352+
* }
353+
* }
354+
* })
355+
*/
356+
class Book
357+
{
358+
// ...
359+
}
360+
```
361+
362+
As the custom queries, you can define your own arguments if you don't want to use the default ones (extracted from your resource).
363+
The only difference with them is that, even if you define your own arguments, the `clientMutationId` will always be set.
364+
365+
The arguments will be in `$context['args']['input']` of your resolvers.
366+
367+
You custom mutations will be available like this:
368+
369+
```graphql
370+
{
371+
mutation {
372+
mutationBook(input: {id: "/books/18", title: "The Fitz and the Fool"}) {
373+
book {
374+
title
375+
}
376+
}
377+
}
378+
379+
mutation {
380+
withCustomArgsMutationBook(input: {sendMail: true, clientMutationId: "myId}) {
381+
book {
382+
title
383+
}
384+
clientMutationId
385+
}
386+
}
387+
}
388+
```
389+
287390
## Filters
288391
289392
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)