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
Copy file name to clipboardExpand all lines: site/graphql-js/Tutorial-GraphQLClients.md
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
2
-
title: GraphQL Clients
2
+
title: GraphQL 客户端
3
3
layout: ../_core/GraphQLJSLayout
4
-
category: GraphQL.js Tutorial
4
+
category: GraphQL.js 教程
5
5
permalink: /graphql-js/graphql-clients/
6
6
next: /graphql-js/basic-types/
7
7
---
8
8
9
-
Since a GraphQL API has more underlying structure than a REST API, there are more powerful clients like [Relay](https://facebook.github.io/relay/) which can automatically handle batching, caching, and other features. But you don't need a complex client to call a GraphQL server. With `express-graphql`, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the `query`field in a JSON payload.
9
+
由于 GraphQL API 拥有比 REST API 更多的下层结构,它也有更强大的客户端。譬如 [Relay](https://facebook.github.io/relay/),它能自动操作批处理、缓存等等。但你并不需要一个复杂的客户端才能调用 GraphQL 服务器,在有了 `express-graphql` 的情况下,你可以向 GraphQL 服务器上的入口端点发送一个 HTTP POST 请求,其中将 GraphQL 查询作为 JSON 载荷的 `query`字段,就能调用 GraphQL 服务器。
10
10
11
-
For example, let's say we mounted a GraphQL server on http://localhost:4000/graphql as in the example code for [running an Express GraphQL server](/graphql-js/running-an-express-graphql-server/), and we want to send the GraphQL query`{ hello }`. We can do this from the command line with `curl`. If you paste this into a terminal:
You should see the data returned, logged in the console:
40
+
你会在控制台中见到返回的数据:
41
41
42
42
```
43
43
data returned: Object { hello: "Hello world!" }
44
44
```
45
45
46
-
In this example, the query was just a hardcoded string. As your application becomes more complex, and you add GraphQL endpoints that take arguments as described in [Passing Arguments](/graphql-js/passing-arguments/), you will want to construct GraphQL queries using variables in client code. You can do this by including a keyword prefixed with a dollar sign in the query, and passing an extra `variables`field on the payload.
You could access this from JavaScript with the code:
56
+
你可以使用下列 JavaScript代码来接入:
57
57
58
58
```javascript
59
59
vardice = 3;
@@ -75,8 +75,8 @@ xhr.send(JSON.stringify({
75
75
}));
76
76
```
77
77
78
-
Using this syntax for variables is a good idea because it automatically prevents bugs due to escaping, and it makes it easier to monitor your server.
78
+
这种语法的变量有助于自动避免转义 bug,也更容易监控服务器。
79
79
80
-
In general, it will take a bit more time to set up a GraphQL client like Relay, but it's worth it to get more features as your application grows. You might want to start out just using HTTP requests as the underlying transport layer, and switching to a more complex client as your application gets more complex.
At this point you can write a client and server in GraphQL for an API that receives a single string. To do more, you will want to [learn how to use the other basic data types](/graphql-js/basic-types/).
82
+
本文中你学会了如何编写只返回一条字符串的 API 的 GraphQL 客户端和服务器。如果你不想止步于此,你可以学习使用如何使用其他 [基本类型](/graphql-js/basic-types/)。
0 commit comments