Skip to content

Commit 48aafe5

Browse files
Tina92linhe0x0
authored andcommitted
Tutorial-GettingStarted.md (xitu#53)
1 parent 4eb7e75 commit 48aafe5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
---
2-
title: Getting Started With GraphQL.js
3-
sidebarTitle: Getting Started
2+
title: GraphQL.js 入门
3+
sidebarTitle: 入门
44
layout: ../_core/GraphQLJSLayout
5-
category: GraphQL.js Tutorial
5+
category: GraphQL.js 教程
66
permalink: /graphql-js/
77
next: /graphql-js/running-an-express-graphql-server/
88
---
99

10-
## Prerequisites
10+
## 学前准备
1111

12-
Before getting started, you should have Node v6 installed, although the examples should mostly work in previous versions of Node as well. For this guide, we won't use any language features that require transpilation, but we will use some ES6 features like [Promises](http://www.html5rocks.com/en/tutorials/es6/promises/), [classes](http://javascriptplayground.com/blog/2014/07/introduction-to-es6-classes-tutorial/), and [fat arrow functions](https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/), so if you aren't familiar with them you might want to read up on them first.
12+
即使这些示例大部分在以前版本的 Node 中也能正常运行,你也应该至少在开始学习之前安装了 v6 以上版本的 Node 环境。在本篇指南中,我们不会使用任何需要转换的语言特性,但是我们会使用 ES6 的部分新特性,比如 [Promises](http://www.html5rocks.com/en/tutorials/es6/promises/)[classes](http://javascriptplayground.com/blog/2014/07/introduction-to-es6-classes-tutorial/) [fat arrow functions](https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/)。所以如果你不熟悉它们,你应该先去了解一下。
1313

14-
To create a new project and install GraphQL.js in your current directory:
14+
创建一个新项目,在你当前目录去安装 GraphQL.js
1515

1616
```bash
1717
npm init
1818
npm install graphql --save
1919
```
2020

21-
## Writing Code
21+
## 编写代码
2222

23-
To handle GraphQL queries, we need a schema that defines the `Query` type, and we need an API root with a function called a “resolver” for each API endpoint. For an API that just returns “Hello world!”, we can put this code in a file named `server.js`:
23+
我们需要一个定义 `Query` 类型的 schema 来处理 GraphQL 查询。我们还需要一个 API 根节点,为每个 API 端点提供一个名为“resolver”的函数。对于只返回“Hello world!”的 API,我们可以将此代码放在名为 `server.js` 的文件中:
2424

2525
```javascript
2626
var { graphql, buildSchema } = require('graphql');
2727

28-
// Construct a schema, using GraphQL schema language
28+
// 使用 GraphQL schema language 构建一个 schema
2929
var schema = buildSchema(`
3030
type Query {
3131
hello: String
3232
}
3333
`);
3434

35-
// The root provides a resolver function for each API endpoint
35+
// 根节点为每个 API 入口端点提供一个 resolver 函数
3636
var root = {
3737
hello: () => {
3838
return 'Hello world!';
3939
},
4040
};
4141

42-
// Run the GraphQL query '{ hello }' and print out the response
42+
// 运行 GraphQL query '{ hello }' ,输出响应
4343
graphql(schema, '{ hello }', root).then((response) => {
4444
console.log(response);
4545
});
4646
```
4747

48-
If you run this with:
48+
如果你像这样运行代码:
4949

5050
```bash
5151
node server.js
5252
```
5353

54-
You should see the GraphQL response printed out:
54+
你会看到打印出的 GraphQL 响应:
5555

5656
```javascript
5757
{ data: { hello: 'Hello world!' } }
5858
```
5959

60-
Congratulations - you just executed a GraphQL query!
60+
恭喜 - 你刚刚执行了一个 GraphQL 的查询!
6161

62-
For practical applications, you'll probably want to run GraphQL queries from an API server, rather than executing GraphQL with a command line tool. To use GraphQL for an API server over HTTP, check out [Running an Express GraphQL Server](/graphql-js/running-an-express-graphql-server/).
62+
在实际应用中,你可能不会在命令行工具里执行 GraphQL,而是会想从一个 API 服务器运行 GraphQL 查询。如何在 HTTP API 服务器运行 GraphQL,请查看 [运行 GraphQL 服务器](/graphql-js/running-an-express-graphql-server/) 章节。

0 commit comments

Comments
 (0)