A co-worker and are using GitHub’s GraphQL API for a side project. I am writing a series of posts around what we learned from creating the application entitled ‘GraghQL and Elm’. This post will detail how to communicate with the Github GraphQL API with Elm. All code related to these posts can be found here.
GitHub has extensive documentation on communicating with their GraphQL server. In this post we will write a simple query and display the JSON from Github. In the next post, we will use Elm Decoders in order to turn the JSON into Elm data.
The GitHub GraphQL Explorer is useful for crafting queries. Let’s write a query that fetches a user’s id:
1 2 3 4 5 |
|
With the aforementioned JSON, let’s contruct a request to GitHub in Elm!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
After posting the query to GitHub, your Elm update function will receive the following response:
1 2 3 4 5 6 7 |
|
I find it helpful to define this response as an Elm String
type and view it in the browser. If there is an error with the query, you can see the results in the browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
The code above will display a stringfied version of our query. In Part 2, we will transform the Elm String
data object into something more useful with Elm decoders.