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
You should see the data returned, logged in the console:
@@ -43,6 +43,37 @@ You should see the data returned, logged in the console:
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.
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:
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
+
46
77
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.
47
78
48
79
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/).
Copy file name to clipboardExpand all lines: site/docs/Tutorial-PassingArguments.md
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -99,4 +99,27 @@ When you call this API, you have to pass each argument by name. So for the serve
99
99
100
100
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.
101
101
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:
Using `$dice` and `$sides` as variables in GraphQL means we don't have to worry about escaping on the client side.
124
+
102
125
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