Skip to content

Commit 2c1bb0d

Browse files
committed
tell people to use variables
1 parent c760a37 commit 2c1bb0d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

site/docs/Tutorial-GraphQLClients.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ xhr.setRequestHeader("Accept", "application/json");
3434
xhr.onload = function () {
3535
console.log('data returned:', xhr.response);
3636
}
37-
xhr.send(JSON.stringify({query:"{ hello }"}));
37+
xhr.send(JSON.stringify({query: "{ hello }"}));
3838
```
3939

4040
You should see the data returned, logged in the console:
@@ -43,6 +43,37 @@ You should see the data returned, logged in the console:
4343
data returned: Object { hello: "Hello world!" }
4444
```
4545

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.
47+
48+
For example, let's say you're running the example server from [Passing Arguments](/graphql-js/passing-arguments/) that has a schema of
49+
50+
```javascript
51+
type Query {
52+
rollDice(numDice: Int!, numSides: Int): [Int]
53+
}
54+
```
55+
56+
You could access this from JavaScript with the code:
57+
58+
```javascript
59+
var dice = 3;
60+
var sides = 6;
61+
var xhr = new XMLHttpRequest();
62+
xhr.responseType = 'json';
63+
xhr.open("POST", "/graphql");
64+
xhr.setRequestHeader("Content-Type", "application/json");
65+
xhr.setRequestHeader("Accept", "application/json");
66+
xhr.onload = function () {
67+
console.log('data returned:', xhr.response);
68+
}
69+
xhr.send(JSON.stringify({
70+
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
71+
variables: { dice: dice, sides: sides },
72+
}));
73+
```
74+
75+
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.
76+
4677
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.
4778

4879
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/).

site/docs/Tutorial-PassingArguments.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,27 @@ When you call this API, you have to pass each argument by name. So for the serve
9999

100100
If you run this code with `node server.js` and browse to [http://localhost:4000/graphql](http://localhost:4000/graphql) you can try out this API.
101101

102+
When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use `$` syntax to define variables in your query, and pass the variables as a separate map.
103+
104+
For example, some JavaScript code that calls our server above is:
105+
106+
```javascript
107+
var dice = 3;
108+
var sides = 6;
109+
var xhr = new XMLHttpRequest();
110+
xhr.responseType = 'json';
111+
xhr.open("POST", "/graphql");
112+
xhr.setRequestHeader("Content-Type", "application/json");
113+
xhr.setRequestHeader("Accept", "application/json");
114+
xhr.onload = function () {
115+
console.log('data returned:', xhr.response);
116+
}
117+
xhr.send(JSON.stringify({
118+
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
119+
variables: { dice: dice, sides: sides },
120+
}));
121+
```
122+
123+
Using `$dice` and `$sides` as variables in GraphQL means we don't have to worry about escaping on the client side.
124+
102125
With basic types and argument passing, you can implement anything you can implement in a REST API. But GraphQL supports even more powerful queries. You can replace multiple API calls with a single API call if you learn how to [define your own object types](/graphql-js/object-types/).

0 commit comments

Comments
 (0)