Skip to content

Instantly share code, notes, and snippets.

@yohfee
Created April 7, 2020 03:42
Show Gist options
  • Save yohfee/01da4541813c259aff059fae6c6faaf8 to your computer and use it in GitHub Desktop.
Save yohfee/01da4541813c259aff059fae6c6faaf8 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
var message = `{ "query": "{ pokemon(name: \"%s\") %s }" }`
type Response struct {
Data Data `json:"data"`
}
type Data struct {
Pokemon Pokemon `json:"pokemon"`
}
type Attack struct {
Name string `json:"name"`
Type string `json:"type"`
Damage int `json:"damage"`
}
type Pokemon struct {
ID string `json:"id"`
Number string `json:"number"`
Name string `json:"name"`
Weight Dimension `json:"weight"`
Height Dimension `json:"height"`
Classification string `json:"classification"`
Types []string `json:"types"`
Resistant []string `json:"resistant"`
Attacks Attacks `json:"attacks"`
Weaknesses []string `json:"weaknesses"`
FleeRate float32 `json:"fleeRate"`
MaxCP int `json:"maxCP"`
Evolutions []Pokemon `json:"evolutions"`
EvolutionRequirements EvolutionRequirement `json:"evolutionRequirements"`
MaxHP int `json:"maxHP"`
Image string `json:"image"`
}
type Attacks struct {
Fast []Attack `json:"fast"`
Special []Attack `json:"special"`
}
type Dimension struct {
Minimum string `json:"minimum"`
Maximum string `json:"maximum"`
}
type EvolutionRequirement struct {
Amount int `json:"amount"`
Name string `json:"name"`
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: Pokemon [Query]\n")
return
}
pokemon := os.Args[1]
query := "{ id number name types attacks { special { name type damage } } evolutions { id number name weight { minimum maximum } attacks { fast { name type damage } } }"
if len(os.Args) == 3 {
query = os.Args[2]
}
q := fmt.Sprintf(message, pokemon, query)
fmt.Println(q)
res, err := http.Post("https://graphql-pokemon.now.sh/", "application/json", strings.NewReader(q))
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var response Response
if err := json.Unmarshal(body, &response); err != nil {
panic(err)
}
fmt.Printf("%+v", response.Data.Pokemon)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment