-
-
Save vvakame/aaaa632bae97ee06c701f339fbae4dbc to your computer and use it in GitHub Desktop.
GraphQL client by Go+stdlib demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"bytes" | |
"io/ioutil" | |
"fmt" | |
"encoding/json" | |
"net/url" | |
"os" | |
) | |
func main() { | |
githubToken := os.Getenv("GITHUB_TOKEN") | |
query := ` | |
{ | |
viewer { | |
id | |
name | |
} | |
} | |
` | |
b, err := json.Marshal(struct { | |
Query string `json:"query"` | |
}{ | |
Query: query, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
endpointURL, err := url.Parse("https://api.github.com/graphql") | |
if err != nil { | |
panic(err) | |
} | |
buf := bytes.NewBuffer(b) | |
resp, err := http.DefaultClient.Do(&http.Request{ | |
URL: endpointURL, | |
Method: "POST", | |
Header: http.Header{ | |
"Content-Type": {"application/json"}, | |
"Authorization": {"bearer " + githubToken}, | |
}, | |
Body: ioutil.NopCloser(buf), | |
}) | |
if err != nil { | |
panic(err) | |
} | |
b, err = ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You MUST exec with
GITHUB_TOKE=<your personal access token place here>
.