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
*/
// 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(
"http://localhost:80/graphql/v1beta",
"https://beta.pokeapi.co/graphql/v1beta",
{
method: "POST",
body: JSON.stringify({
query: operationsDoc,
query: query,
variables: variables,
operationName: operationName
})
}
);
)
return await result.json();
return await result.json()
}
const operationsDoc = `
query pokemon_details {
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) {
name
base_happiness
is_legendary
is_mythical
generation: pokemon_v2_generation {
function fetchPokemon_details(name="starmie") {
const query = `
query pokemon_details($name: String) {
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
name
}
habitat: pokemon_v2_pokemonhabitat {
name
}
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
nodes {
height
base_happiness
is_legendary
is_mythical
generation: pokemon_v2_generation {
name
id
weight
abilities: pokemon_v2_pokemonabilities_aggregate {
nodes {
ability: pokemon_v2_ability {
}
habitat: pokemon_v2_pokemonhabitat {
name
}
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
}
}
}
stats: pokemon_v2_pokemonstats {
base_stat
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 {
types: pokemon_v2_pokemontypes {
slot
type: pokemon_v2_type {
name
}
level
}
}
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
aggregate {
count
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
nodes {
move: pokemon_v2_move {
name
}
level
}
}
}
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
pokemon_v2_item {
name
cost
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
aggregate {
count
}
}
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"}}}) {
flavor_text
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
flavor_text
}
}
}
}
`;
`
function fetchPokemon_details() {
return fetchGraphQL(
operationsDoc,
"pokemon_details",
{}
);
query,
{"name": name},
"pokemon_details"
)
}
async function startFetchPokemon_details() {
const { errors, data } = await fetchPokemon_details();
async function main() {
const pokemon = process.argv.slice(2)[0];
const { errors, data } = await fetchPokemon_details(pokemon)
if (errors) {
// handle those errors like a pro
console.error(errors);
console.error(errors)
}
// do something great with this precious data
console.log(data);
console.log(JSON.stringify(data, null, 2))
}
startFetchPokemon_details();
main()