diff --git a/graphql/examples/README.md b/graphql/examples/README.md new file mode 100644 index 00000000..59ca62cd --- /dev/null +++ b/graphql/examples/README.md @@ -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. diff --git a/graphql/examples/alola_road_encounters.gql b/graphql/examples/alola_road_encounters.gql index 625197c5..b3dcafb9 100644 --- a/graphql/examples/alola_road_encounters.gql +++ b/graphql/examples/alola_road_encounters.gql @@ -3,7 +3,7 @@ Finds Pokemons in Alola that evolve when you are in a particular location. Variables: { - "region": "alola" + "region": "alola" } """ diff --git a/graphql/examples/go/README.md b/graphql/examples/go/README.md new file mode 100644 index 00000000..ed240eb8 --- /dev/null +++ b/graphql/examples/go/README.md @@ -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 +``` diff --git a/graphql/examples/pokemon.go b/graphql/examples/go/pokemon.go similarity index 90% rename from graphql/examples/pokemon.go rename to graphql/examples/go/pokemon.go index 4d739a0c..7a1abf1f 100644 --- a/graphql/examples/pokemon.go +++ b/graphql/examples/go/pokemon.go @@ -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)) } diff --git a/graphql/examples/item_translations.gql b/graphql/examples/item_translations.gql new file mode 100644 index 00000000..3948495d --- /dev/null +++ b/graphql/examples/item_translations.gql @@ -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 + } + } + } +} \ No newline at end of file diff --git a/graphql/examples/node/README.md b/graphql/examples/node/README.md new file mode 100644 index 00000000..075c5388 --- /dev/null +++ b/graphql/examples/node/README.md @@ -0,0 +1,10 @@ +# Node examples + +## `pokemon.js` + +Fetches info about Staryu using `node-fetch`. + +```sh +npm i +node pokemon.js +``` diff --git a/graphql/examples/node/package-lock.json b/graphql/examples/node/package-lock.json new file mode 100644 index 00000000..ff4a6872 --- /dev/null +++ b/graphql/examples/node/package-lock.json @@ -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==" + } + } +} diff --git a/graphql/examples/package.json b/graphql/examples/node/package.json similarity index 100% rename from graphql/examples/package.json rename to graphql/examples/node/package.json diff --git a/graphql/examples/node/pokemon.js b/graphql/examples/node/pokemon.js new file mode 100644 index 00000000..b11a8f59 --- /dev/null +++ b/graphql/examples/node/pokemon.js @@ -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() diff --git a/graphql/examples/pokemon.js b/graphql/examples/pokemon.js deleted file mode 100644 index 11850ae0..00000000 --- a/graphql/examples/pokemon.js +++ /dev/null @@ -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(); \ No newline at end of file diff --git a/graphql/examples/pokemon_stats.gql b/graphql/examples/pokemon_stats.gql new file mode 100644 index 00000000..01990293 --- /dev/null +++ b/graphql/examples/pokemon_stats.gql @@ -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 + } + } + } +}