by Kyriakos Chatzidimitriou | Oct 15, 2019 05:30 | tutorials
gogolanggraphqljsonmarshalencodegithub v4
Background: I wanted to test quickly, different, complex GraphQL queries to the GitHub v4 API especially wrt to error messages produced. At the same time I didn't want to create complex Go types to match the GitHub schema using a library like shurcooL/githubv4, it seemed like a lot of hassle for my purpose, especially since I didn't want to decode the response and use it.
Prerequisites: Create a personal access token with the scopes related to the queries you want to do and put it in the environmental variable GITHUB_TOKEN.
The string value of "query" must escape newline characters or the schema will not parse it correctly. For the POST body, use outer double quotes and escaped inner double quotes.
So in order not to do the encoding myself, I will use the go json library to take care of the json encoding/marshaling.
In addition one must take care of putting extra braces like explained in this StackOverflow answer.
The complete gist that includes a query with variables can be found below:
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
Comments