openstates/_example/main.go

52 lines
911 B
Go

package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/machinebox/graphql"
)
func main() {
client := graphql.NewClient("https://openstates.org/graphql")
req := graphql.NewRequest(`
query Reps($lat: Float, $long: Float) {
people(latitude: $lat, longitude: $long, first: 100) {
edges {
node {
name
}
}
}
}
`)
req.Var("lat", 37.700824)
req.Var("long", -97.352906)
var res response
ctx := context.Background()
// This line enables logging... Probably don't want it for production :)
client.Log = func(s string) { log.Println(s) }
req.Header.Add("X-API-KEY", ApiKey)
if err := client.Run(ctx, req, &res); err != nil {
log.Fatal(err)
}
j, _ := json.Marshal(res)
fmt.Println(string(j))
}
type response struct {
People people
}
type people struct {
Edges []edge
}
type edge struct {
Node node
}
type node struct {
Name string
}