Skip to content

Commit b35fdf3

Browse files
authored
Merge pull request #611 from PokeAPI/more-graphql-exmples
2 parents 9dcae99 + 30e531d commit b35fdf3

File tree

11 files changed

+220
-132
lines changed

11 files changed

+220
-132
lines changed

graphql/examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GraphQL examples
2+
3+
You can use all the `.gql` examples in our console at https://beta.pokeapi.co/graphql/console/.
4+
5+
Inside the folders you find GraphQL queries implemented in different languages, frameworks and libraries.

graphql/examples/alola_road_encounters.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Finds Pokemons in Alola that evolve when you are in a particular location.
33
44
Variables:
55
{
6-
"region": "alola"
6+
"region": "alola"
77
}
88
"""
99

graphql/examples/go/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Go examples
2+
3+
## `pokemon.go`
4+
5+
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.
6+
7+
```sh
8+
go run pokemon.go # | jq
9+
```
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"log"
89
"net/http"
@@ -17,10 +18,12 @@ type Operation struct {
1718
var (
1819
pokemonDetails = Operation{
1920
OperationName: "pokemon_details",
20-
Variables: map[string]interface{}{},
21+
Variables: map[string]interface{}{
22+
"name": "staryu",
23+
},
2124
Query: `
22-
query pokemon_details {
23-
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) {
25+
query pokemon_details($name: String) {
26+
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
2427
name
2528
base_happiness
2629
is_legendary
@@ -103,5 +106,5 @@ func main() {
103106
if err != nil {
104107
log.Fatal(err)
105108
}
106-
log.Println(string(body))
109+
fmt.Println(string(body))
107110
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# for each language, list all items and the relative English translation
2+
query getItemsTranslation1 {
3+
pokemon_v2_language {
4+
name
5+
iso639
6+
iso3166
7+
items: pokemon_v2_itemnames {
8+
name
9+
englishName: pokemon_v2_item {
10+
name
11+
}
12+
}
13+
}
14+
}
15+
16+
# for each item, show the English name and get all its translations
17+
query getItemsTranslation2 {
18+
items: pokemon_v2_item {
19+
name
20+
translations: pokemon_v2_itemnames {
21+
foreignName: name
22+
language: pokemon_v2_language {
23+
name
24+
}
25+
}
26+
}
27+
}

graphql/examples/node/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Node examples
2+
3+
## `pokemon.js`
4+
5+
Fetches info about Staryu using `node-fetch`.
6+
7+
```sh
8+
npm i
9+
node pokemon.js
10+
```

graphql/examples/node/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphql/examples/node/pokemon.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Get's many details about a pokemon passed as argument (starmie as default).
3+
4+
It gets:
5+
- happiness
6+
- if legendary/mythical
7+
- generation
8+
- habitat
9+
- height
10+
- weight
11+
- ID
12+
- abilities
13+
- stats
14+
- types
15+
- learnable moves by leveling up
16+
- in how many locations it can be found
17+
- holdable items in Fire Red
18+
- flavor text
19+
*/
20+
21+
const fetch = require("node-fetch")
22+
23+
async function fetchGraphQL(query, variables, operationName) {
24+
const result = await fetch(
25+
"https://beta.pokeapi.co/graphql/v1beta",
26+
{
27+
method: "POST",
28+
body: JSON.stringify({
29+
query: query,
30+
variables: variables,
31+
operationName: operationName
32+
})
33+
}
34+
)
35+
36+
return await result.json()
37+
}
38+
39+
40+
41+
function fetchPokemon_details(name="starmie") {
42+
const query = `
43+
query pokemon_details($name: String) {
44+
species: pokemon_v2_pokemonspecies(where: {name: {_eq: $name}}) {
45+
name
46+
base_happiness
47+
is_legendary
48+
is_mythical
49+
generation: pokemon_v2_generation {
50+
name
51+
}
52+
habitat: pokemon_v2_pokemonhabitat {
53+
name
54+
}
55+
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
56+
nodes {
57+
height
58+
name
59+
id
60+
weight
61+
abilities: pokemon_v2_pokemonabilities_aggregate {
62+
nodes {
63+
ability: pokemon_v2_ability {
64+
name
65+
}
66+
}
67+
}
68+
stats: pokemon_v2_pokemonstats {
69+
base_stat
70+
stat: pokemon_v2_stat {
71+
name
72+
}
73+
}
74+
types: pokemon_v2_pokemontypes {
75+
slot
76+
type: pokemon_v2_type {
77+
name
78+
}
79+
}
80+
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
81+
nodes {
82+
move: pokemon_v2_move {
83+
name
84+
}
85+
level
86+
}
87+
}
88+
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
89+
aggregate {
90+
count
91+
}
92+
}
93+
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
94+
pokemon_v2_item {
95+
name
96+
cost
97+
}
98+
rarity
99+
}
100+
}
101+
}
102+
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
103+
flavor_text
104+
}
105+
}
106+
}
107+
`
108+
109+
return fetchGraphQL(
110+
query,
111+
{"name": name},
112+
"pokemon_details"
113+
)
114+
}
115+
116+
async function main() {
117+
const pokemon = process.argv.slice(2)[0];
118+
const { errors, data } = await fetchPokemon_details(pokemon)
119+
if (errors) {
120+
console.error(errors)
121+
}
122+
console.log(JSON.stringify(data, null, 2))
123+
}
124+
125+
main()

graphql/examples/pokemon.js

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)