Skip to content

Commit 9dcae99

Browse files
authored
Merge pull request #610 from ichbinfrog/graphql-go-example
Add graphql go consumption example
2 parents ad73257 + 408600f commit 9dcae99

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

graphql/examples/pokemon.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
)
10+
11+
type Operation struct {
12+
Query string `json:"query"`
13+
Variables map[string]interface{} `json:"variables"`
14+
OperationName string `json:"operationName"`
15+
}
16+
17+
var (
18+
pokemonDetails = Operation{
19+
OperationName: "pokemon_details",
20+
Variables: map[string]interface{}{},
21+
Query: `
22+
query pokemon_details {
23+
species: pokemon_v2_pokemonspecies(where: {name: {_eq: "staryu"}}) {
24+
name
25+
base_happiness
26+
is_legendary
27+
is_mythical
28+
generation: pokemon_v2_generation {
29+
name
30+
}
31+
habitat: pokemon_v2_pokemonhabitat {
32+
name
33+
}
34+
pokemon: pokemon_v2_pokemons_aggregate(limit: 1) {
35+
nodes {
36+
height
37+
name
38+
id
39+
weight
40+
abilities: pokemon_v2_pokemonabilities_aggregate {
41+
nodes {
42+
ability: pokemon_v2_ability {
43+
name
44+
}
45+
}
46+
}
47+
stats: pokemon_v2_pokemonstats {
48+
base_stat
49+
stat: pokemon_v2_stat {
50+
name
51+
}
52+
}
53+
types: pokemon_v2_pokemontypes {
54+
slot
55+
type: pokemon_v2_type {
56+
name
57+
}
58+
}
59+
levelUpMoves: pokemon_v2_pokemonmoves_aggregate(where: {pokemon_v2_movelearnmethod: {name: {_eq: "level-up"}}}, distinct_on: move_id) {
60+
nodes {
61+
move: pokemon_v2_move {
62+
name
63+
}
64+
level
65+
}
66+
}
67+
foundInAsManyPlaces: pokemon_v2_encounters_aggregate {
68+
aggregate {
69+
count
70+
}
71+
}
72+
fireRedItems: pokemon_v2_pokemonitems(where: {pokemon_v2_version: {name: {_eq: "firered"}}}) {
73+
pokemon_v2_item {
74+
name
75+
cost
76+
}
77+
rarity
78+
}
79+
}
80+
}
81+
flavorText: pokemon_v2_pokemonspeciesflavortexts(where: {pokemon_v2_language: {name: {_eq: "en"}}, pokemon_v2_version: {name: {_eq: "firered"}}}) {
82+
flavor_text
83+
}
84+
}
85+
}
86+
`,
87+
}
88+
)
89+
90+
func main() {
91+
url := "https://beta.pokeapi.co/graphql/v1beta"
92+
body, err := json.Marshal(pokemonDetails)
93+
if err != nil {
94+
log.Fatal(err)
95+
}
96+
97+
resp, err := http.Post(url, "", bytes.NewReader(body))
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
defer resp.Body.Close()
102+
body, err = ioutil.ReadAll(resp.Body)
103+
if err != nil {
104+
log.Fatal(err)
105+
}
106+
log.Println(string(body))
107+
}

0 commit comments

Comments
 (0)