GraphQL has been a buzzword for last few years after Facebook has made it open-source and so I tried GraphQL with the Node.js and I completely agree with all the buzz about the GraphQL, its advantages, and simplicity.
I recently switched to Go-lang for the new project from the Node.js and I decided to try GraphQL with the Go-lang. There are not many library options with the Go-lang but I have tried it with these 4 libraries. Thunder, graphql, graphql-go, and gqlgen. And I have to say that gqlgen is winning all the ground in all libraries I have tried.
gqlgen is still in the beta with latest version v0.4.4 at the time of writing this article and it’s rapidly evolving. You can find their road-map here. And as now 99designs are officially sponsoring them so we will see even better development speed for this awesome opensource project. vektah and neelance are major contributors of this and neelance also wrote the graphql-go before.
So let’s dive into the library semantic assuming you have basic graphql knowledge.
As their headline states
This is a library for quickly creating strictly typed graphql servers in golang.
I think this is the most promising thing about a library that you will never see map[string]interface{}
here as it uses strictly typed approach.
Apart from that, it uses Schema first Approach: So you define your API using the graphql Schema Definition Language and has its own powerful code generation tools which will auto-generate all of your GraphQL code and you will just need to implement the core logic of that interface methods.
This version has now been deprecated. Please follow this articles for latest updates.
https://medium.freecodecamp.org/deep-dive-into-graphql-with-golang-d3e02a429ac3
As it’s schema-first approach so let’s quickly define a schema. We are taking job application forum as an example.
Now library provide codegen command gqlgen init
which will generate all the required files for project files
As the docs stats if you don’t provide the models definition it will auto-generate the models but in most of the cases you will write your own model as you will have more fine-grained control on data types and you can use third-party structs when needed(Like you can use AWS or any other library’s Struct for like credential, config, etc).
So let’s override generate gqlgen.yml
file:
Here we have given the path of all the models. In schema.graphql
file we have defined one custom scalar Timestamp
so we need to tell graphql about how to Marshal and Unmarshal them.
Here is our models.go
file where all above structs are defined:
Now if you have noticed we haven’t defined any struct for Timestamp
but only Marshal and Unmarshal methods for Timestamp. So the library handles this for you just need to write these two methods.
Another important thing to notice is we haven’t defined a struct for inputs(NewJob
and NewApplication
) too. So as promised by the library it will be auto-generated in models/inputs.go
as per the given config in gqlgen.yml
file with a notice that you should not edit this file.
Now, let’s come to the most important file and that is generated.go
, which has all the magic code written its very long file obviously but let’s have a look at needed code from it.
Another file generated by gqlgen is resolver.go
which is the file where you will put all of your logic. Following is auto-generated stub file(Little edited for comments, formatting and package change)
Now all you need to do is write your logic in these methods.
I am writing one sample method to create and get all jobs with the help of Firebase real-time database. and cutting down the database code for the sake of he length of this article which you can find here.
Now let’s try this out and hit the servers and run first mutation
$ go run server/server.go
As we can see one job is created under Position’s node(as configured in database layer) and we get the same response in graphql also with newly created id.
Now let’s test query:
But wait, WHAATT! We got an error. Don’t panic its because graphql don’t understand how to fetch CreatedBy
User object from just a CreatedBy
userId so we need to implement resolver method for that which currently states that panic(**“not implemented”**)
.
So now hit the query again:
And it’s all working.
This article just gives a basic demo of how graphql server can be implemented in the golang(I know there can be many changes in the code and structure). This code is on Github. I will write article soon to add CORS, Validation, Authentication, and Configuration with Golang gqlgen server. Till then happy coding.
Part-2 is on the way!
Thanks for reading! (A few 👏 are always appreciated. 😀)