Skip to content

Commit 97811b7

Browse files
hikerpiglinhe0x0
authored andcommitted
Guides-ConstructingTypes.md (xitu#50)
1 parent 02e64c6 commit 97811b7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

site/graphql-js/Guides-ConstructingTypes.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ permalink: /graphql-js/constructing-types/
66
next: /graphql-js/express-graphql/
77
---
88

9-
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.
9+
对多数应用来说,你可以在开始运行的时候使用 GraphQL Schema Languange 声明固有的 Schema。但有些情况下,使用程序构建 Schema 也很有用。你可使用 `GraphQLSchema` 构造函数来做这件事。
1010

11-
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.
11+
使用 `GraphQLSchema` 构造函数创建 Schema 时,你在定义 `Query` `Mutation` 类型时不用单纯的 Schema Language,而是像对象一样创建它们。
1212

13-
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` 这么写:
1414

1515
```javascript
1616
var express = require('express');
@@ -28,7 +28,7 @@ var schema = buildSchema(`
2828
}
2929
`);
3030

31-
// Maps id to User object
31+
// id 映射到 User 对象
3232
var fakeDatabase = {
3333
'a': {
3434
id: 'a',
@@ -56,7 +56,7 @@ app.listen(4000);
5656
console.log('Running a GraphQL API server at localhost:4000/graphql');
5757
```
5858

59-
We can implement this same API without using GraphQL schema language:
59+
也可以在不使用 GraphQL Schema Language 的情况下实现相同的 API:
6060

6161
```javascript
6262
var express = require('express');
@@ -75,7 +75,7 @@ var fakeDatabase = {
7575
},
7676
};
7777

78-
// Define the User type
78+
// 定义 User 类型
7979
var userType = new graphql.GraphQLObjectType({
8080
name: 'User',
8181
fields: {
@@ -84,13 +84,13 @@ var userType = new graphql.GraphQLObjectType({
8484
}
8585
});
8686

87-
// Define the Query type
87+
// 定义 Query 类型
8888
var queryType = new graphql.GraphQLObjectType({
8989
name: 'Query',
9090
fields: {
9191
user: {
9292
type: userType,
93-
// `args` describes the arguments that the `user` query accepts
93+
// `args` 描述了 `user` 查询接受的参数
9494
args: {
9595
id: { type: graphql.GraphQLString }
9696
},
@@ -112,6 +112,6 @@ app.listen(4000);
112112
console.log('Running a GraphQL API server at localhost:4000/graphql');
113113
```
114114

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` 对象上的。
116116

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

Comments
 (0)