|
1 | 1 | ---
|
2 |
| -title: Getting Started With GraphQL.js |
3 |
| -sidebarTitle: Getting Started |
| 2 | +title: GraphQL.js 入门 |
| 3 | +sidebarTitle: 入门 |
4 | 4 | layout: ../_core/GraphQLJSLayout
|
5 |
| -category: GraphQL.js Tutorial |
| 5 | +category: GraphQL.js 教程 |
6 | 6 | permalink: /graphql-js/
|
7 | 7 | next: /graphql-js/running-an-express-graphql-server/
|
8 | 8 | ---
|
9 | 9 |
|
10 |
| -## Prerequisites |
| 10 | +## 学前准备 |
11 | 11 |
|
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/)。所以如果你不熟悉它们,你应该先去了解一下。 |
13 | 13 |
|
14 |
| -To create a new project and install GraphQL.js in your current directory: |
| 14 | +创建一个新项目,在你当前目录去安装 GraphQL.js: |
15 | 15 |
|
16 | 16 | ```bash
|
17 | 17 | npm init
|
18 | 18 | npm install graphql --save
|
19 | 19 | ```
|
20 | 20 |
|
21 |
| -## Writing Code |
| 21 | +## 编写代码 |
22 | 22 |
|
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` 的文件中: |
24 | 24 |
|
25 | 25 | ```javascript
|
26 | 26 | var { graphql, buildSchema } = require('graphql');
|
27 | 27 |
|
28 |
| -// Construct a schema, using GraphQL schema language |
| 28 | +// 使用 GraphQL schema language 构建一个 schema |
29 | 29 | var schema = buildSchema(`
|
30 | 30 | type Query {
|
31 | 31 | hello: String
|
32 | 32 | }
|
33 | 33 | `);
|
34 | 34 |
|
35 |
| -// The root provides a resolver function for each API endpoint |
| 35 | +// 根节点为每个 API 入口端点提供一个 resolver 函数 |
36 | 36 | var root = {
|
37 | 37 | hello: () => {
|
38 | 38 | return 'Hello world!';
|
39 | 39 | },
|
40 | 40 | };
|
41 | 41 |
|
42 |
| -// Run the GraphQL query '{ hello }' and print out the response |
| 42 | +// 运行 GraphQL query '{ hello }' ,输出响应 |
43 | 43 | graphql(schema, '{ hello }', root).then((response) => {
|
44 | 44 | console.log(response);
|
45 | 45 | });
|
46 | 46 | ```
|
47 | 47 |
|
48 |
| -If you run this with: |
| 48 | +如果你像这样运行代码: |
49 | 49 |
|
50 | 50 | ```bash
|
51 | 51 | node server.js
|
52 | 52 | ```
|
53 | 53 |
|
54 |
| -You should see the GraphQL response printed out: |
| 54 | +你会看到打印出的 GraphQL 响应: |
55 | 55 |
|
56 | 56 | ```javascript
|
57 | 57 | { data: { hello: 'Hello world!' } }
|
58 | 58 | ```
|
59 | 59 |
|
60 |
| -Congratulations - you just executed a GraphQL query! |
| 60 | +恭喜 - 你刚刚执行了一个 GraphQL 的查询! |
61 | 61 |
|
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