docs: improve Node example

This commit is contained in:
Alessandro Pezzè 2021-04-10 21:19:11 +02:00
parent d74edcc0b6
commit a918d08da1

View file

@ -18,110 +18,108 @@ It gets:
- flavor text - flavor text
*/ */
// Node doesn't implement fetch so we have to import it const fetch = require("node-fetch")
const fetch =require("node-fetch");
async function fetchGraphQL(operationsDoc, operationName, variables) { async function fetchGraphQL(query, variables, operationName) {
const result = await fetch( const result = await fetch(
"http://localhost:80/graphql/v1beta", "https://beta.pokeapi.co/graphql/v1beta",
{ {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
query: operationsDoc, query: query,
variables: variables, variables: variables,
operationName: operationName operationName: operationName
}) })
} }
); )
return await result.json(); return await result.json()
} }
const operationsDoc = `
query pokemon_details {
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) { function fetchPokemon_details(name="starmie") {
name const query = `
base_happiness query pokemon_details($name: String) {
is_legendary species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
is_mythical
generation: pokemon_v2_generation {
name name
} base_happiness
habitat: pokemon_v2_pokemonhabitat { is_legendary
name is_mythical
} generation: pokemon_v2_generation {
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
nodes {
height
name name
id }
weight habitat: pokemon_v2_pokemonhabitat {
abilities: pokemon_v2_pokemonabilities_aggregate { name
nodes { }
ability: pokemon_v2_ability { pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
nodes {
height
name
id
weight
abilities: pokemon_v2_pokemonabilities_aggregate {
nodes {
ability: pokemon_v2_ability {
name
}
}
}
stats: pokemon_v2_pokemonstats {
base_stat
stat: pokemon_v2_stat {
name name
} }
} }
} types: pokemon_v2_pokemontypes {
stats: pokemon_v2_pokemonstats { slot
base_stat type: pokemon_v2_type {
stat: pokemon_v2_stat {
name
}
}
types: pokemon_v2_pokemontypes {
slot
type: pokemon_v2_type {
name
}
}
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
nodes {
move: pokemon_v2_move {
name name
} }
level
} }
} levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
foundInAsManyPlaces: pokemon_v2_encounters_aggregate { nodes {
aggregate { move: pokemon_v2_move {
count name
}
level
}
} }
} foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) { aggregate {
pokemon_v2_item { count
name }
cost }
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
pokemon_v2_item {
name
cost
}
rarity
} }
rarity
} }
} }
} flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) { flavor_text
flavor_text }
} }
} }
} `
`;
function fetchPokemon_details() {
return fetchGraphQL( return fetchGraphQL(
operationsDoc, query,
"pokemon_details", {"name": name},
{} "pokemon_details"
); )
} }
async function startFetchPokemon_details() { async function main() {
const { errors, data } = await fetchPokemon_details(); const pokemon = process.argv.slice(2)[0];
const { errors, data } = await fetchPokemon_details(pokemon)
if (errors) { if (errors) {
// handle those errors like a pro console.error(errors)
console.error(errors);
} }
console.log(JSON.stringify(data, null, 2))
// do something great with this precious data
console.log(data);
} }
startFetchPokemon_details(); main()