Skip to content

Commit 92c39cb

Browse files
caubleebyron
authored andcommitted
Convert XHR to fetch in examples (graphql#455)
1 parent a2f10d2 commit 92c39cb

File tree

3 files changed

+56
-49
lines changed

3 files changed

+56
-49
lines changed

site/graphql-js/Tutorial-GraphQLClients.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ If you prefer to use a graphical user interface to send a test query, you can us
2828
It's also simple to send GraphQL from the browser. Open up http://localhost:4000, open a developer console, and paste in:
2929

3030
```javascript
31-
var xhr = new XMLHttpRequest();
32-
xhr.responseType = 'json';
33-
xhr.open("POST", "/graphql");
34-
xhr.setRequestHeader("Content-Type", "application/json");
35-
xhr.setRequestHeader("Accept", "application/json");
36-
xhr.onload = function () {
37-
console.log('data returned:', xhr.response);
38-
}
39-
xhr.send(JSON.stringify({query: "{ hello }"}));
31+
fetch('/graphql', {
32+
method: 'POST',
33+
headers: {
34+
'Content-Type': 'application/json',
35+
'Accept': 'application/json',
36+
},
37+
body: JSON.stringify({query: "{ hello }"})
38+
})
39+
.then(r => r.json())
40+
.then(data => console.log('data returned:', data));
4041
```
4142

4243
You should see the data returned, logged in the console:
@@ -60,21 +61,23 @@ You could access this from JavaScript with the code:
6061
```javascript
6162
var dice = 3;
6263
var sides = 6;
63-
var xhr = new XMLHttpRequest();
64-
xhr.responseType = 'json';
65-
xhr.open("POST", "/graphql");
66-
xhr.setRequestHeader("Content-Type", "application/json");
67-
xhr.setRequestHeader("Accept", "application/json");
68-
xhr.onload = function () {
69-
console.log('data returned:', xhr.response);
70-
}
7164
var query = `query RollDice($dice: Int!, $sides: Int) {
7265
rollDice(numDice: $dice, numSides: $sides)
7366
}`;
74-
xhr.send(JSON.stringify({
75-
query: query,
76-
variables: { dice: dice, sides: sides },
77-
}));
67+
68+
fetch('/graphql', {
69+
method: 'POST',
70+
headers: {
71+
'Content-Type': 'application/json',
72+
'Accept': 'application/json',
73+
},
74+
body: JSON.stringify({
75+
query,
76+
variables: { dice, sides },
77+
})
78+
})
79+
.then(r => r.json())
80+
.then(data => console.log('data returned:', data));
7881
```
7982

8083
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.

site/graphql-js/Tutorial-Mutations.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,30 @@ You can use variables to simplify mutation client logic just like you can with q
165165
```javascript
166166
var author = 'andy';
167167
var content = 'hope is a good thing';
168-
var xhr = new XMLHttpRequest();
169-
xhr.responseType = 'json';
170-
xhr.open("POST", "/graphql");
171-
xhr.setRequestHeader("Content-Type", "application/json");
172-
xhr.setRequestHeader("Accept", "application/json");
173-
xhr.onload = function () {
174-
console.log('data returned:', xhr.response);
175-
}
176168
var query = `mutation CreateMessage($input: MessageInput) {
177169
createMessage(input: $input) {
178170
id
179171
}
180172
}`;
181-
xhr.send(JSON.stringify({
182-
query: query,
183-
variables: {
184-
input: {
185-
author: author,
186-
content: content,
173+
174+
fetch('/graphql', {
175+
method: 'POST',
176+
headers: {
177+
'Content-Type': 'application/json',
178+
'Accept': 'application/json',
179+
},
180+
body: JSON.stringify({
181+
query,
182+
variables: {
183+
input: {
184+
author,
185+
content,
186+
}
187187
}
188-
}
189-
}));
188+
})
189+
})
190+
.then(r => r.json())
191+
.then(data => console.log('data returned:', data));
190192
```
191193

192194
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/graphql-js/Tutorial-PassingArguments.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,23 @@ For example, some JavaScript code that calls our server above is:
106106
```javascript
107107
var dice = 3;
108108
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-
}
117109
var query = `query RollDice($dice: Int!, $sides: Int) {
118110
rollDice(numDice: $dice, numSides: $sides)
119111
}`;
120-
xhr.send(JSON.stringify({
121-
query: query,
122-
variables: { dice: dice, sides: sides },
123-
}));
112+
113+
fetch('/graphql', {
114+
method: 'POST',
115+
headers: {
116+
'Content-Type': 'application/json',
117+
'Accept': 'application/json',
118+
},
119+
body: JSON.stringify({
120+
query,
121+
variables: { dice, sides },
122+
})
123+
})
124+
.then(r => r.json())
125+
.then(data => console.log('data returned:', data));
124126
```
125127

126128
Using `$dice` and `$sides` as variables in GraphQL means we don't have to worry about escaping on the client side.

0 commit comments

Comments
 (0)