Skip to content

Commit 954d9c3

Browse files
Kevin Lackerleebyron
Kevin Lacker
authored andcommitted
fix variables examples and add one for mutation (graphql#68)
1 parent ea91306 commit 954d9c3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

site/docs/Tutorial-GraphQLClients.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ xhr.setRequestHeader("Accept", "application/json");
6666
xhr.onload = function () {
6767
console.log('data returned:', xhr.response);
6868
}
69+
var query = `query RollDice($dice: Int!, $sides: Int) {
70+
rollDice(numDice: $dice, numSides: $sides)
71+
}`;
6972
xhr.send(JSON.stringify({
70-
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
73+
query: query,
7174
variables: { dice: dice, sides: sides },
7275
}));
7376
```

site/docs/Tutorial-Mutations.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,31 @@ mutation {
125125
}
126126
```
127127

128+
You can use variables to simplify mutation client logic just like you can with queries. For example, some JavaScript code that calls the server to execute this mutation is:
129+
130+
```javascript
131+
var author = 'andy';
132+
var content = 'hope is a good thing';
133+
var xhr = new XMLHttpRequest();
134+
xhr.responseType = 'json';
135+
xhr.open("POST", "/graphql");
136+
xhr.setRequestHeader("Content-Type", "application/json");
137+
xhr.setRequestHeader("Accept", "application/json");
138+
xhr.onload = function () {
139+
console.log('data returned:', xhr.response);
140+
}
141+
var query = `mutation CreateMessage($input: MessageInput) {
142+
createMessage(message: $input)
143+
}`;
144+
xhr.send(JSON.stringify({
145+
query: query,
146+
variables: {
147+
input: {
148+
author: author,
149+
content: content,
150+
}
151+
}
152+
}));
153+
```
154+
128155
One particular type of mutation is operations that change users, like signing up a new user. While you can implement this using GraphQL mutations, you can reuse many existing libraries if you learn about [GraphQL with authentication and Express middleware](/graphql-js/authentication-and-express-middleware/).

site/docs/Tutorial-PassingArguments.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ xhr.setRequestHeader("Accept", "application/json");
114114
xhr.onload = function () {
115115
console.log('data returned:', xhr.response);
116116
}
117+
var query = `query RollDice($dice: Int!, $sides: Int) {
118+
rollDice(numDice: $dice, numSides: $sides)
119+
}`;
117120
xhr.send(JSON.stringify({
118-
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
121+
query: query,
119122
variables: { dice: dice, sides: sides },
120123
}));
121124
```

0 commit comments

Comments
 (0)