Skip to content

Commit a918d08

Browse files
committed
docs: improve Node example
1 parent d74edcc commit a918d08

File tree

1 file changed

+71
-73
lines changed

1 file changed

+71
-73
lines changed

graphql/examples/node/pokemon.js

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,110 +18,108 @@ It gets:
1818
- flavor text
1919
*/
2020

21-
// Node doesn't implement fetch so we have to import it
22-
const fetch =require("node-fetch");
21+
const fetch = require("node-fetch")
2322

24-
async function fetchGraphQL(operationsDoc, operationName, variables) {
23+
async function fetchGraphQL(query, variables, operationName) {
2524
const result = await fetch(
26-
"http://localhost:80/graphql/v1beta",
25+
"https://beta.pokeapi.co/graphql/v1beta",
2726
{
2827
method: "POST",
2928
body: JSON.stringify({
30-
query: operationsDoc,
29+
query: query,
3130
variables: variables,
3231
operationName: operationName
3332
})
3433
}
35-
);
34+
)
3635

37-
return await result.json();
36+
return await result.json()
3837
}
3938

40-
const operationsDoc = `
41-
query pokemon_details {
42-
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) {
43-
name
44-
base_happiness
45-
is_legendary
46-
is_mythical
47-
generation: pokemon_v2_generation {
48-
name
49-
}
50-
habitat: pokemon_v2_pokemonhabitat {
39+
40+
41+
function fetchPokemon_details(name="starmie") {
42+
const query = `
43+
query pokemon_details($name: String) {
44+
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
5145
name
52-
}
53-
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
54-
nodes {
55-
height
46+
base_happiness
47+
is_legendary
48+
is_mythical
49+
generation: pokemon_v2_generation {
5650
name
57-
id
58-
weight
59-
abilities: pokemon_v2_pokemonabilities_aggregate {
60-
nodes {
61-
ability: pokemon_v2_ability {
62-
name
51+
}
52+
habitat: pokemon_v2_pokemonhabitat {
53+
name
54+
}
55+
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
56+
nodes {
57+
height
58+
name
59+
id
60+
weight
61+
abilities: pokemon_v2_pokemonabilities_aggregate {
62+
nodes {
63+
ability: pokemon_v2_ability {
64+
name
65+
}
6366
}
6467
}
65-
}
66-
stats: pokemon_v2_pokemonstats {
67-
base_stat
68-
stat: pokemon_v2_stat {
69-
name
70-
}
71-
}
72-
types: pokemon_v2_pokemontypes {
73-
slot
74-
type: pokemon_v2_type {
75-
name
68+
stats: pokemon_v2_pokemonstats {
69+
base_stat
70+
stat: pokemon_v2_stat {
71+
name
72+
}
7673
}
77-
}
78-
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
79-
nodes {
80-
move: pokemon_v2_move {
74+
types: pokemon_v2_pokemontypes {
75+
slot
76+
type: pokemon_v2_type {
8177
name
8278
}
83-
level
8479
}
85-
}
86-
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
87-
aggregate {
88-
count
80+
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
81+
nodes {
82+
move: pokemon_v2_move {
83+
name
84+
}
85+
level
86+
}
8987
}
90-
}
91-
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
92-
pokemon_v2_item {
93-
name
94-
cost
88+
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
89+
aggregate {
90+
count
91+
}
92+
}
93+
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
94+
pokemon_v2_item {
95+
name
96+
cost
97+
}
98+
rarity
9599
}
96-
rarity
97100
}
98101
}
99-
}
100-
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
101-
flavor_text
102+
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
103+
flavor_text
104+
}
102105
}
103106
}
104-
}
105-
`;
107+
`
106108

107-
function fetchPokemon_details() {
108109
return fetchGraphQL(
109-
operationsDoc,
110-
"pokemon_details",
111-
{}
112-
);
110+
query,
111+
{"name": name},
112+
"pokemon_details"
113+
)
113114
}
114115

115-
async function startFetchPokemon_details() {
116-
const { errors, data } = await fetchPokemon_details();
117-
116+
async function main() {
117+
const pokemon = process.argv.slice(2)[0];
118+
const { errors, data } = await fetchPokemon_details(pokemon)
118119
if (errors) {
119-
// handle those errors like a pro
120-
console.error(errors);
120+
console.error(errors)
121121
}
122-
123-
// do something great with this precious data
124-
console.log(data);
122+
console.log(JSON.stringify(data, null, 2))
125123
}
126124

127-
startFetchPokemon_details();
125+
main()

0 commit comments

Comments
 (0)