mirror of
https://github.com/PokeAPI/pokeapi
synced 2025-02-17 21:18:27 +00:00
docs: improve Node example
This commit is contained in:
parent
d74edcc0b6
commit
a918d08da1
1 changed files with 71 additions and 73 deletions
|
@ -18,28 +18,30 @@ 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"}}) {
|
||||
|
||||
|
||||
function fetchPokemon_details(name="starmie") {
|
||||
const query = `
|
||||
query pokemon_details($name: String) {
|
||||
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
|
||||
name
|
||||
base_happiness
|
||||
is_legendary
|
||||
|
@ -102,26 +104,22 @@ const operationsDoc = `
|
|||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
`
|
||||
|
||||
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)
|
||||
}
|
||||
console.log(JSON.stringify(data, null, 2))
|
||||
}
|
||||
|
||||
// do something great with this precious data
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
startFetchPokemon_details();
|
||||
main()
|
||||
|
|
Loading…
Add table
Reference in a new issue