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
If you have an API endpoint that alters data, like inserting data into a database or altering data already in a database, you should make this endpoint a `Mutation` rather than a `Query`. This is a simple as making the API endpoint part of the top-level `Mutation` type instead of the top-level `Query` type.
10
10
11
-
Let's say we have a “message of the day” server, where anyone can update the message of the day along with the author of the message, and anyone can read the current one. The GraphQL schema for this is simply:
11
+
Let's say we have a “message of the day” server, where anyone can update the message of the day, and anyone can read the current one. The GraphQL schema for this is simply:
12
12
13
13
```javascript
14
14
type Mutation {
15
-
setMessage(message:String)
15
+
setMessage(message:String):String
16
16
}
17
17
18
18
type Query {
19
19
getMessage:String
20
20
}
21
21
```
22
22
23
+
It's often convenient to have a mutation that maps to a database create or update operation, like `setMessage`, return the same thing that the server stored. That way, if you modify the data on the server, the client can learn about those modifications.
24
+
23
25
Both mutations and queries can be handled by root resolvers, so the root that implements this schema can simply be:
24
26
25
27
```javascript
26
28
var fakeDatabase = {};
27
29
var root = {
28
30
setMessage:function ({message}) {
29
31
fakeDatabase.message= message;
32
+
return message;
30
33
}
31
34
getMessage:function () {
32
35
returnfakeDatabase.message;
@@ -36,25 +39,33 @@ var root = {
36
39
37
40
You don't need anything more than this to implement mutations. But in many cases, you will find a number of different mutations that all accept the same input parameters. A common example is that creating an object in a database and updating an object in a database often take the same parameters. To make your schema simpler, you can use “input types” for this, by using the `input` keyword instead of the `type` keyword.
38
41
39
-
For example, instead of a single message of the day, let's say we have one message per author, where the author is just a unique username, and we want a mutation API both for creating a new message and for updating an old message. We could use the schema:
42
+
For example, instead of a single message of the day, let's say we have many messages, indexed in a database by the `id` field, and each message has both a `content` string and an `author` string. We want a mutation API both for creating a new message and for updating an old message. We could use the schema:
Input types can't have fields that are other objects, only basic scalar types and list types.
66
+
Here, the mutations return a `Message` type, so that the client can get more information about the newly-modified `Message` in the same request as the request that mutates it.
67
+
68
+
Input types can't have fields that are other objects, only basic scalar types, list types, and other input types.
58
69
59
70
Naming input types with `Input` on the end is a useful convention, because you will often want both an input type and an output type that are slightly different for a single conceptual object.
60
71
@@ -68,39 +79,59 @@ var { buildSchema } = require('graphql');
68
79
// Construct a schema, using GraphQL schema language
console.log('Running a GraphQL API server at localhost:4000/graphql');
144
+
app.listen(4000, () => {
145
+
console.log('Running a GraphQL API server at localhost:4000/graphql');
146
+
});
147
+
115
148
```
116
149
117
-
To call a mutation, you must use the keyword `mutation` before your GraphQL query. To pass an input type, provide the data written as if it's a JSON object. For example, to call `createMessage` as defined above you could use this GraphQL operation:
150
+
To call a mutation, you must use the keyword `mutation` before your GraphQL query. To pass an input type, provide the data written as if it's a JSON object. For example, with the server defined above, you can create a new message and return the `id` of the new message with this operation:
118
151
119
152
```javascript
120
153
mutation {
121
-
createMessage(message: {
154
+
createMessage(input: {
122
155
author:"andy",
123
156
content:"hope is a good thing",
124
-
})
157
+
}) {
158
+
id
159
+
}
125
160
}
126
161
```
127
162
@@ -139,7 +174,9 @@ xhr.onload = function () {
139
174
console.log('data returned:', xhr.response);
140
175
}
141
176
var query =`mutation CreateMessage($input: MessageInput) {
0 commit comments