Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guides-ConstructingTypes.md #50

Merged
merged 3 commits into from Aug 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions site/graphql-js/Guides-ConstructingTypes.md
Expand Up @@ -6,11 +6,11 @@ permalink: /graphql-js/constructing-types/
next: /graphql-js/express-graphql/
---

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

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

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:
例如,假设我们要实现个简单的 API,根据 id 在一些硬编码数据中查询某个用户数据。我们可以用 `buildSchema` 这么写:

```javascript
var express = require('express');
Expand All @@ -28,7 +28,7 @@ var schema = buildSchema(`
}
`);

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

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

```javascript
var express = require('express');
Expand All @@ -75,7 +75,7 @@ var fakeDatabase = {
},
};

// Define the User type
// 定义 User 类型
var userType = new graphql.GraphQLObjectType({
name: 'User',
fields: {
Expand All @@ -84,13 +84,13 @@ var userType = new graphql.GraphQLObjectType({
}
});

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

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.
当我们使用这种方式构建 API 时,根解析器是构建在 `Query` `Mutation` 类型, 而不是 `root` 对象上的。

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.
这种方法在你想要通过一些手段(例如数据库 Schema)自动创建 GraphQL Schema 时很有用。如此一来你就可以拥有一些类似于创建和更改数据库记录的通用模板。还有,在实现类似集合类型(union types)这种没法轻易映射为 ES6 Class 或者纯 Schema Language 实现的功能时,此方法也很有用。