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: doc/mutations/README.md
+49-38Lines changed: 49 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,43 @@
1
1
# Mutations
2
2
3
-
In version 4 of Drupal GraphQL `Mutations` work a lot more similar to queries than they do in 3.x. Mutations are also technically Data producers which we already looked at.
3
+
In version 4 of Drupal GraphQL `Mutations` work a lot more similar to queries than they do in 3.x. Mutations are called using also Data producers which we already looked at.
4
4
5
-
Lets create a mutation that creates a new article. In this case it takes a data parameter that can have a `title` and a `creator` in order to set these fields when creating the new article if they have been provided.
5
+
Let's make a mutation that creates a new article. In this case it takes a data parameter that can have a `title` and a `description` in order to set these fields when creating the new article if they have been provided.
6
6
7
7
Similar to queries we can start by adding the necessary schema information, not only to register our new mutation but also provide type safety on all parameters as well. This mutation will return the newly created "Article".
8
8
9
-
The code with all the demo queries and mutations in these docs can be found in [this repository](https://github.com/joaogarin/mydrupalgql).
9
+
The code with all the demo queries and mutations in these docs can be found in the same `graphql_composable` example module.
10
10
11
11
## Add the schema declaration
12
12
13
-
```
14
-
schema {
15
-
mutation: Mutation
16
-
}
13
+
Adapt your base schema file to something like this where we include a new type called `Mutation` and we also create a new input called `ArticleInput` which we will use as the type for our mutation argument.
17
14
18
-
type Mutation {
19
-
createArticle(data: ArticleInput): Article
20
-
}
15
+
```
16
+
type Mutation
21
17
22
-
type Article implements NodeInterface {
23
-
id: Int!
24
-
title: String!
25
-
creator: String
26
-
}
18
+
scalar Violation
27
19
28
-
interface NodeInterface {
29
-
id: Int!
20
+
type Article {
21
+
id: Int!
22
+
title: String!
23
+
author: String
30
24
}
31
25
32
26
input ArticleInput {
33
-
title: String!
34
-
description: String
27
+
title: String!
28
+
description: String
35
29
}
30
+
```
31
+
32
+
And now in our `.exntends.graphqls` file we will extend the Mutation type to add our new mutation. This is so that in the future other modules can also themselves extend this type with new mutations keeping things organized.
36
33
34
+
```
35
+
extend type Mutation {
36
+
createArticle(data: ArticleInput): Article
37
+
}
37
38
```
38
39
39
-
We can now see we have a Mutation called `createArticle` which takes a data parameter, but because GraphQL is heavily typed we know everything we can and must include in the new Article like the title which is mandatory in this case.
40
+
We can now see we have a Mutation called `createArticle` which takes a data parameter, and because GraphQL is heavily typed we know everything we can and must include in the new Article (`ArticleInput`) like the title which is mandatory in this case.
40
41
41
42
## Implement the custom data producer (mutation)
42
43
@@ -45,7 +46,7 @@ We now need to implement the actual mutation, in the file `src/Plugin/GraphQL/Da
if ($this->currentUser->hasPermission("create article content")) {
124
-
$values = [
125
-
'title' => $data['title'],
126
-
'field_article_creator' => $data['creator'],
127
-
];
128
-
$node = Node::create($values);
129
-
$node->save();
130
-
return $node;
125
+
$values = [
126
+
'type' => 'article',
127
+
'title' => $data['title'],
128
+
'body' => $data['description'],
129
+
];
130
+
$node = Node::create($values);
131
+
$node->save();
132
+
return $node;
131
133
}
132
134
return NULL;
133
135
}
134
136
135
137
}
136
-
137
138
```
138
139
139
-
## Adding resolvers
140
+
### Important note
141
+
142
+
One thing to notice when creating mutations like this is that Access checking needs to be done in the mutation, for queries this usually is done in the
143
+
data producer directly (e.g. `entity_load` has access checking built-in) but because we are programatically creating
144
+
things we need to check the user actually has access to do the operation.
145
+
146
+
## Calling the mutation
140
147
141
-
To add the resolvers we go to our schema implementation and call the created data producer `create_article` inside the `getResolverRegistry` method.
148
+
To add the resolvers for the `createArticle` mutation we go to our schema implementation and call the created data producer `create_article` inside the `registerResolvers` method.
142
149
143
150
```php
144
151
/**
145
152
* {@inheritdoc}
146
153
*/
147
-
protected function getResolverRegistry() {
148
-
154
+
public function registerResolvers(ResolverRegistryInterface $registry) {
@@ -159,10 +166,10 @@ protected function getResolverRegistry() {
159
166
```
160
167
161
168
This mutation can now be called like this :
162
-
169
+
163
170
```graphql
164
171
mutation {
165
-
createArticle(data: {title: "Hello GraphQl 2"}) {
172
+
createArticle(data: {title: "Hello GraphQl 2"}) {
166
173
...onArticle {
167
174
id
168
175
title
@@ -171,7 +178,7 @@ mutation {
171
178
}
172
179
```
173
180
174
-
and should return something like :
181
+
and should return something like :
175
182
176
183
```json
177
184
{
@@ -183,3 +190,7 @@ and should return something like :
183
190
}
184
191
}
185
192
```
193
+
194
+
## Validating mutations
195
+
196
+
Now that we have our mutation in place one way we can improve this is by adding some validation so that if someone is not to create an article they get a nice error back (technically in Drupal these are called Violations) so that it can be printed to the user in whichever app this is called. In the next chapter we will look at how we can improve this code to add some validation to it.
0 commit comments