-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
01-use-fetch-get.js
41 lines (36 loc) · 811 Bytes
/
01-use-fetch-get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
const gqlQuery = `query pokemons($limit: Int, $offset: Int) {
pokemons(limit: $limit, offset: $offset) {
count
next
previous
status
message
results {
url
name
image
}
}
}`;
const gqlVariables = {
limit: 2,
offset: 1,
};
const params = {
query: gqlQuery,
variables: gqlVariables,
};
const queryString = Object.keys(params)
.map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
})
.join('&');
fetch(`https://graphql-pokeapi.graphcdn.app/?${queryString}`, {
credentials: 'omit',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
})
.then((res) => res.json())
.then((res) => console.log('Response from server', res));