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 as simple as making the API endpoint part of the top-level `Mutation`type instead of the top-level `Query` type.
9
+
假设你有一个 API 入口端点用于修改数据,像是向数据库中插入数据或修改已有数据,在 GraphQL 中,你应该将这个入口端点做为 `Mutation`而不是 `Query`。这十分简单,只需要将这个入口端点做成 `Mutation`类型顶层的一部份即可。
10
10
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:
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.
Both mutations and queries can be handled by root resolvers, so the root that implements this schema can simply be:
25
+
不论是变更还是查询,根级解析器都能够处理,因此实现 schema 的 root 可以如下:
26
26
27
27
```javascript
28
28
var fakeDatabase = {};
@@ -37,11 +37,11 @@ var root = {
37
37
};
38
38
```
39
39
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.
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:
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.
Input types can't have fields that are other objects, only basic scalar types, list types, and other input types.
68
+
输入类型的字段不能是其他对象类型,只能是基础标量类型、列表类型或者其他输入类型。
69
69
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.
Here's some runnable code that implements this schema, keeping the data in memory:
72
+
下面的可运行代码实现了上述 schema,数据保存在内存中:
73
73
74
74
```javascript
75
75
var express = require('express');
76
76
var graphqlHTTP = require('express-graphql');
77
77
var { buildSchema } = require('graphql');
78
78
79
-
//Construct a schema, using GraphQL schema language
79
+
// 使用 GraphQLschema language 构建 schema
80
80
var schema = buildSchema(`
81
81
input MessageInput {
82
82
content: String
@@ -99,7 +99,7 @@ var schema = buildSchema(`
99
99
}
100
100
`);
101
101
102
-
//If Message had any complex fields, we'd put them on this object.
102
+
// 如果Message拥有复杂字段,我们把它们放在这个对象里面。
103
103
classMessage {
104
104
constructor(id, {content, author}) {
105
105
this.id = id;
@@ -108,7 +108,7 @@ class Message {
108
108
}
109
109
}
110
110
111
-
//Maps username to content
111
+
// 映射username到content
112
112
varfakeDatabase = {};
113
113
114
114
varroot = {
@@ -147,9 +147,9 @@ app.listen(4000, () => {
147
147
148
148
```
149
149
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:
You can use variables to simplify mutation client logic just like you can with queries. For example, some JavaScript code that calls the server to execute this mutation is:
One particular type of mutation is operations that change users, like signing up a new user. While you can implement this using GraphQL mutations, you can reuse many existing libraries if you learn about [GraphQL with authentication and Express middleware](/graphql-js/authentication-and-express-middleware/).
0 commit comments