Initial Commit
This commit is contained in:
4
_example/api_key.go.example
Normal file
4
_example/api_key.go.example
Normal file
@@ -0,0 +1,4 @@
|
||||
package main
|
||||
// Copy this file to "api_key.go" and then populate the line below in that file with your api key
|
||||
// ("api_key.go" is in the .gitignore)
|
||||
var ApiKey = ""
|
51
_example/main.go
Normal file
51
_example/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user