pokeapi/graphql/examples/node/pokemon.js

126 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-03-19 18:10:13 +00:00
/*
2021-04-10 19:19:55 +00:00
Get's many details about a pokemon passed as argument (starmie as default).
2021-03-21 20:31:22 +00:00
It gets:
- happiness
- if legendary/mythical
- generation
- habitat
- height
- weight
- ID
- abilities
- stats
- types
- learnable moves by leveling up
- in how many locations it can be found
- holdable items in Fire Red
- flavor text
2021-03-19 18:10:13 +00:00
*/
2021-04-10 19:19:11 +00:00
const fetch = require("node-fetch")
2021-03-19 18:10:13 +00:00
2021-04-10 19:19:11 +00:00
async function fetchGraphQL(query, variables, operationName) {
2021-03-19 18:10:13 +00:00
const result = await fetch(
2021-04-10 19:19:11 +00:00
"https://beta.pokeapi.co/graphql/v1beta",
2021-03-19 18:10:13 +00:00
{
method: "POST",
body: JSON.stringify({
2021-04-10 19:19:11 +00:00
query: query,
2021-03-19 18:10:13 +00:00
variables: variables,
operationName: operationName
})
}
2021-04-10 19:19:11 +00:00
)
2021-03-19 18:10:13 +00:00
2021-04-10 19:19:11 +00:00
return await result.json()
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
function fetchPokemon_details(name="starmie") {
const query = `
query pokemon_details($name: String) {
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
2021-03-19 18:10:13 +00:00
name
2021-04-10 19:19:11 +00:00
base_happiness
is_legendary
is_mythical
generation: pokemon_v2_generation {
2021-03-19 18:10:13 +00:00
name
2021-04-10 19:19:11 +00:00
}
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
}
2021-03-19 18:10:13 +00:00
}
}
2021-04-10 19:19:11 +00:00
stats: pokemon_v2_pokemonstats {
base_stat
stat: pokemon_v2_stat {
name
}
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
types: pokemon_v2_pokemontypes {
slot
type: pokemon_v2_type {
2021-03-19 18:10:13 +00:00
name
}
}
2021-04-10 19:19:11 +00:00
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
nodes {
move: pokemon_v2_move {
name
}
level
}
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
aggregate {
count
}
}
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
pokemon_v2_item {
name
cost
}
rarity
2021-03-19 18:10:13 +00:00
}
}
}
2021-04-10 19:19:11 +00:00
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
flavor_text
}
2021-03-19 18:10:13 +00:00
}
}
2021-04-10 19:19:11 +00:00
`
2021-03-19 18:10:13 +00:00
return fetchGraphQL(
2021-04-10 19:19:11 +00:00
query,
{"name": name},
"pokemon_details"
)
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
async function main() {
const pokemon = process.argv.slice(2)[0];
const { errors, data } = await fetchPokemon_details(pokemon)
2021-03-19 18:10:13 +00:00
if (errors) {
2021-04-10 19:19:11 +00:00
console.error(errors)
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
console.log(JSON.stringify(data, null, 2))
2021-03-19 18:10:13 +00:00
}
2021-04-10 19:19:11 +00:00
main()