Running Mutations
Mutations modify data in your Doctrine ORM. They are defined as such:
1<?php
2
3$schema = new Schema([
4 'mutation' => new ObjectType([
5 'name' => 'mutation',
6 'fields' => [
7 'mutationName' => [
8 'type' => $driver->type(Artist::class),
9 'args' => [
10 'id' => Type::nonNull(Type::id()),
11 'input' => Type::nonNull($driver->input(Artist::class, ['name'])),
12 ],
13 'resolve' => function ($root, $args) use ($driver): User {
14 $artist = $driver->get(EntityManager::class)
15 ->getRepository(Artist::class)
16 ->find($args['id']);
17
18 $artist->setName($args['input']['name']);
19 $driver->get(EntityManager::class)->flush();
20
21 return $artist;
22 },
23 ],
24 ],
25 ]),
26]);
You can define multiple mutations under the fields
array. The type
is
the GraphQL type of the entity you’re processing. The args
array in this
example has a traditional argument and an input
argument. The input
argument is created using the driver $driver->input(entityclass)
method and
has two optional arguments. The resolve
method passes the args
to
a function that will do the work. In this example that function returns an
Artist
entity thereby allowing a query on the result.
Calling Mutations
1<?php
2
3$query = 'mutation MutationName($id: Int!, $name: String!) {
4 mutationName(id: $id, input: { name: $name }) {
5 id
6 name
7 }
8}';
To call a mutation you must prefix the request with mutation
. The mutation
will then take input from the args
array. The id
and name
in this
mutation will return the new values from the mutated entity.
Input Argument
The driver function $driver->input(Entity::class)
will return an
InputObjectType
with all the fields set to nonNull, thereby making them
required. Since this is rarely what is intended, there are two optional
parameters to specify required and optional fields.
1<?php
2
3$driver->input(Entity::class, ['requiredField'], ['optionalField'])
In the above mutation example the name
field is required and there are no
optional fields, so the only field in the input
args will be name
.
The name
input field will be typed according to its metadata configuration.
You may use a wildcard ['*']
for required and optional fields.
Identifiers are excluded from the input field list because they should not be changed or added by a user.
This is documentation for API-Skeletons/doctrine-graphql. Please add your ★ star to the project.
Authored by API Skeletons.