mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-25 04:40:21 +00:00
Add graphql go consumption example
Signed-off-by: Quoc Trung Hoang <hoangquo@etu.utc.fr>
This commit is contained in:
parent
ad73257649
commit
408600f7ed
1 changed files with 107 additions and 0 deletions
107
graphql/examples/pokemon.go
Normal file
107
graphql/examples/pokemon.go
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Operation struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Variables map[string]interface{} `json:"variables"`
|
||||||
|
OperationName string `json:"operationName"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
pokemonDetails = Operation{
|
||||||
|
OperationName: "pokemon_details",
|
||||||
|
Variables: map[string]interface{}{},
|
||||||
|
Query: `
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
url := "https://beta.pokeapi.co/graphql/v1beta"
|
||||||
|
body, err := json.Marshal(pokemonDetails)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(url, "", bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err = ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println(string(body))
|
||||||
|
}
|
Loading…
Reference in a new issue