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
For many apps, you can define a fixed schema when the application starts, and define it using GraphQL schema language. In some cases, it's useful to construct a schema programmatically. You can do this using the `GraphQLSchema`constructor.
When you are using the `GraphQLSchema`constructor to create a schema, instead of defining `Query`and`Mutation`types solely using schema language, you create them as separate object types.
For example, let's say we are building a simple API that lets you fetch user data for a few hardcoded users based on an id. Using`buildSchema`we could write a server with:
13
+
例如,假设我们要实现个简单的 API,根据 id 在一些硬编码数据中查询某个用户数据。我们可以用`buildSchema`这么写:
14
14
15
15
```javascript
16
16
var express =require('express');
@@ -28,7 +28,7 @@ var schema = buildSchema(`
28
28
}
29
29
`);
30
30
31
-
//Maps id to User object
31
+
//从 id 映射到 User 对象
32
32
var fakeDatabase = {
33
33
'a': {
34
34
id:'a',
@@ -56,7 +56,7 @@ app.listen(4000);
56
56
console.log('Running a GraphQL API server at localhost:4000/graphql');
57
57
```
58
58
59
-
We can implement this same API without using GraphQL schema language:
59
+
也可以在不使用 GraphQL Schema Language 的情况下实现相同的 API:
60
60
61
61
```javascript
62
62
var express =require('express');
@@ -75,7 +75,7 @@ var fakeDatabase = {
75
75
},
76
76
};
77
77
78
-
//Define the User type
78
+
//定义 User 类型
79
79
var userType =newgraphql.GraphQLObjectType({
80
80
name:'User',
81
81
fields: {
@@ -84,13 +84,13 @@ var userType = new graphql.GraphQLObjectType({
84
84
}
85
85
});
86
86
87
-
//Define the Query type
87
+
//定义 Query 类型
88
88
var queryType =newgraphql.GraphQLObjectType({
89
89
name:'Query',
90
90
fields: {
91
91
user: {
92
92
type: userType,
93
-
// `args` describes the arguments that the `user` query accepts
93
+
// `args` 描述了 `user` 查询接受的参数
94
94
args: {
95
95
id: { type:graphql.GraphQLString }
96
96
},
@@ -112,6 +112,6 @@ app.listen(4000);
112
112
console.log('Running a GraphQL API server at localhost:4000/graphql');
113
113
```
114
114
115
-
When we use this method of creating the API, the root level resolvers are implemented on the `Query`and`Mutation`types rather than on a `root`object.
115
+
当我们使用这种方式构建 API 时,根解析器是构建在 `Query`和`Mutation`类型, 而不是 `root`对象上的。
116
116
117
-
This is particularly useful if you want to create a GraphQL schema automatically from something else, like a database schema. You might have a common format for something like creating and updating database records. This is also useful for implementing features like union types which don't map cleanly to ES6 classes and schema language.
117
+
这种方法在你想要通过一些手段(例如数据库 Schema)自动创建 GraphQL Schema 时很有用。如此一来你就可以拥有一些类似于创建和更改数据库记录的通用模板。还有,在实现类似集合类型(union types)这种没法轻易映射为 ES6 Class 或者纯 Schema Language 实现的功能时,此方法也很有用。
0 commit comments