Get your GraphQL server ready using GoLang in 5 minutes

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 this 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.

Highlights

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.

Code

As it’s schema-first approach so let’s quickly define a schema. We are taking job application forum as an example.

https://medium.com/media/e14ac2b00e46a0854ddc357e16c223bd/href

Now library provide codegen command gqlgen init which will generate all the required files for project files

  • gqlgen.yml — The gqlgen config file, knobs for controlling the generated code.
  • generated.go — The graphql execution runtime, the bulk of the generated code
  • models_gen.go — Generated models required to build the graph. Often you will override these with models you write yourself. Still very useful for input types.
  • resolver.go — This is where your application code lives. generated.go will call into this to get the data the user has requested.

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:

https://medium.com/media/1bbb8a86f5c1f390ce78ffd18e98d35b/href

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:

https://medium.com/media/9c8839d81b80b1a6afe26bf773a46f29/href

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.

https://medium.com/media/a5f6d370ba109dc83c68470ae89c719b/href

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.

https://medium.com/media/389de4893784ba4c0aedbaa4f73ec50f/href

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)

https://medium.com/media/03b4bc85bbff2ec31bbca47dc0eb3442/href

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.

https://medium.com/media/a1589145533facb4b5f023a58dc2d37b/href

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”) .

https://medium.com/media/235a635b4237fe94b97dbebe9bb5862a/href

So now hit the query again:

And it’s all working.

Now What?

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. 😀)


Get your GraphQL server ready using GoLang in 5 minutes was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: