Skip to content

Instantly share code, notes, and snippets.

@kyrcha
Last active October 14, 2019 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrcha/76fdcabfbdb4c746fdc8d20761262212 to your computer and use it in GitHub Desktop.
Save kyrcha/76fdcabfbdb4c746fdc8d20761262212 to your computer and use it in GitHub Desktop.
Performing GraphQL queries (to GiHub) using the barebones http.Client in Go
package main
import (
"context"
"encoding/json"
"fmt"
"net/http/httputil"
"os"
"strings"
"golang.org/x/oauth2"
)
type graphQLRequest struct {
Query string `json:"query"`
Variables string `json:"variables"`
}
func main() {
client := oauth2.NewClient(
context.TODO(),
oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
))
query := `query {
repository(owner:"octocat", name:"Hello-World") {
issues(last:20, states:CLOSED) {
edges {
node {
title
url
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}`
gqlMarshalled, err := json.Marshal(graphQLRequest{Query: query})
if err != nil {
panic(err)
}
resp, err := client.Post("https://api.github.com/graphql", "application/json", strings.NewReader(string(gqlMarshalled)))
if err != nil {
panic(err)
}
b, _ := httputil.DumpResponse(resp, true)
fmt.Println(string(b))
query = `query($number_of_repos:Int!) {
viewer {
name
repositories(last: $number_of_repos) {
nodes {
name
}
}
}
}`
variables := `variables {
"number_of_repos": 3
}`
gqlMarshalled, err = json.Marshal(graphQLRequest{Query: query, Variables: variables})
if err != nil {
panic(err)
}
b, _ = httputil.DumpResponse(resp, true)
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment