mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-22 11:23:13 +00:00
Merge pull request #611 from PokeAPI/more-graphql-exmples
This commit is contained in:
commit
b35fdf3616
11 changed files with 220 additions and 132 deletions
5
graphql/examples/README.md
Normal file
5
graphql/examples/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# GraphQL examples
|
||||
|
||||
You can use all the `.gql` examples in our console at https://beta.pokeapi.co/graphql/console/.
|
||||
|
||||
Inside the folders you find GraphQL queries implemented in different languages, frameworks and libraries.
|
|
@ -3,7 +3,7 @@ Finds Pokemons in Alola that evolve when you are in a particular location.
|
|||
|
||||
Variables:
|
||||
{
|
||||
"region": "alola"
|
||||
"region": "alola"
|
||||
}
|
||||
"""
|
||||
|
||||
|
|
9
graphql/examples/go/README.md
Normal file
9
graphql/examples/go/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Go examples
|
||||
|
||||
## `pokemon.go`
|
||||
|
||||
Fetches details about a Pokémon and prints an unformatted JSON to the `stdout`. The name of the Pokémon is passed as a variable.
|
||||
|
||||
```sh
|
||||
go run pokemon.go # | jq
|
||||
```
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -17,10 +18,12 @@ type Operation struct {
|
|||
var (
|
||||
pokemonDetails = Operation{
|
||||
OperationName: "pokemon_details",
|
||||
Variables: map[string]interface{}{},
|
||||
Variables: map[string]interface{}{
|
||||
"name": "staryu",
|
||||
},
|
||||
Query: `
|
||||
query pokemon_details {
|
||||
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) {
|
||||
query pokemon_details($name: String) {
|
||||
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
|
||||
name
|
||||
base_happiness
|
||||
is_legendary
|
||||
|
@ -103,5 +106,5 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println(string(body))
|
||||
fmt.Println(string(body))
|
||||
}
|
27
graphql/examples/item_translations.gql
Normal file
27
graphql/examples/item_translations.gql
Normal file
|
@ -0,0 +1,27 @@
|
|||
# for each language, list all items and the relative English translation
|
||||
query getItemsTranslation1 {
|
||||
pokemon_v2_language {
|
||||
name
|
||||
iso639
|
||||
iso3166
|
||||
items: pokemon_v2_itemnames {
|
||||
name
|
||||
englishName: pokemon_v2_item {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# for each item, show the English name and get all its translations
|
||||
query getItemsTranslation2 {
|
||||
items: pokemon_v2_item {
|
||||
name
|
||||
translations: pokemon_v2_itemnames {
|
||||
foreignName: name
|
||||
language: pokemon_v2_language {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
graphql/examples/node/README.md
Normal file
10
graphql/examples/node/README.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Node examples
|
||||
|
||||
## `pokemon.js`
|
||||
|
||||
Fetches info about Staryu using `node-fetch`.
|
||||
|
||||
```sh
|
||||
npm i
|
||||
node pokemon.js
|
||||
```
|
13
graphql/examples/node/package-lock.json
generated
Normal file
13
graphql/examples/node/package-lock.json
generated
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "examples",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"node-fetch": {
|
||||
"version": "2.6.1",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
|
||||
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
|
||||
}
|
||||
}
|
||||
}
|
125
graphql/examples/node/pokemon.js
Normal file
125
graphql/examples/node/pokemon.js
Normal file
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
Get's many details about a pokemon passed as argument (starmie as default).
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
const fetch = require("node-fetch")
|
||||
|
||||
async function fetchGraphQL(query, variables, operationName) {
|
||||
const result = await fetch(
|
||||
"https://beta.pokeapi.co/graphql/v1beta",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
query: query,
|
||||
variables: variables,
|
||||
operationName: operationName
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
return await result.json()
|
||||
}
|
||||
|
||||
|
||||
|
||||
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
|
||||
is_mythical
|
||||
generation: pokemon_v2_generation {
|
||||
name
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
level
|
||||
}
|
||||
}
|
||||
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
|
||||
aggregate {
|
||||
count
|
||||
}
|
||||
}
|
||||
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
|
||||
pokemon_v2_item {
|
||||
name
|
||||
cost
|
||||
}
|
||||
rarity
|
||||
}
|
||||
}
|
||||
}
|
||||
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
|
||||
flavor_text
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
return fetchGraphQL(
|
||||
query,
|
||||
{"name": name},
|
||||
"pokemon_details"
|
||||
)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const pokemon = process.argv.slice(2)[0];
|
||||
const { errors, data } = await fetchPokemon_details(pokemon)
|
||||
if (errors) {
|
||||
console.error(errors)
|
||||
}
|
||||
console.log(JSON.stringify(data, null, 2))
|
||||
}
|
||||
|
||||
main()
|
|
@ -1,127 +0,0 @@
|
|||
/*
|
||||
Get's many details about Staryu.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// Node doesn't implement fetch so we have to import it
|
||||
const fetch =require("node-fetch");
|
||||
|
||||
async function fetchGraphQL(operationsDoc, operationName, variables) {
|
||||
const result = await fetch(
|
||||
"http://localhost:80/graphql/v1beta",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
query: operationsDoc,
|
||||
variables: variables,
|
||||
operationName: operationName
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
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 {
|
||||
name
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
level
|
||||
}
|
||||
}
|
||||
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
|
||||
aggregate {
|
||||
count
|
||||
}
|
||||
}
|
||||
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
|
||||
pokemon_v2_item {
|
||||
name
|
||||
cost
|
||||
}
|
||||
rarity
|
||||
}
|
||||
}
|
||||
}
|
||||
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",
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
async function startFetchPokemon_details() {
|
||||
const { errors, data } = await fetchPokemon_details();
|
||||
|
||||
if (errors) {
|
||||
// handle those errors like a pro
|
||||
console.error(errors);
|
||||
}
|
||||
|
||||
// do something great with this precious data
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
startFetchPokemon_details();
|
23
graphql/examples/pokemon_stats.gql
Normal file
23
graphql/examples/pokemon_stats.gql
Normal file
|
@ -0,0 +1,23 @@
|
|||
query tallest {
|
||||
pokemon: pokemon_v2_pokemon(order_by: {height: desc}, limit: 3, where: {is_default: {_eq: true}}) {
|
||||
name
|
||||
height
|
||||
}
|
||||
}
|
||||
|
||||
query fattest {
|
||||
pokemon: pokemon_v2_pokemon(order_by: {weight: desc}, limit: 3, where: {is_default: {_eq: true}}) {
|
||||
name
|
||||
weight
|
||||
}
|
||||
}
|
||||
|
||||
query avgHappiness {
|
||||
species: pokemon_v2_pokemonspecies_aggregate {
|
||||
aggregate {
|
||||
avg {
|
||||
base_happiness
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue