Build a GraphQL API in Golang: Why? (Part 1)
Feb 16 2022|Written by Slimane Akalië|programming
Cover image by Gilberto Olimpio.
If you are part of the software engineering community, there is a high chance that you encountered GraphQL. You might have an idea about it or even build something with it, but the story doesn’t end there.
“Mankind invented a system to cope with the fact that we are so intrinsically lousy at manipulating numbers. It’s called the graph.” - Charlie Munger
Wait a minute, what is GraphQL?
This question is a philosophical one, it’s like asking what is SQL? is it the language or the runtime?
Put opinions aside, from the official website of GraphQL we see a “simple” definition of what this thing is:
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more.
If you don’t understand anything, you’re not alone. Let’s try to keep it simple and stupid: GraphQL is a query language that clients use to request read and write operations from a backend service with a clear description of data and an optimal way to expose it: you ask and get only what you need and nothing more.
All the tools that we use to create a GraphQL server are just middlewares to run an http server that offers an API for clients to manipulate/retrieve the data following the GraphQL language.
If we made the analogy to relational databases, in one hand, we have SQL which is the language that everybody agrees on (the equivalent to GraphQL), and in the other had, we have the RDBMS (relational database management system) which is the tool that offers clients an API to manipulate/retrieve the data from the physical files using SQL (the equivalent to a GraphQL API). The implementation of the runtime can differ from solution to another but the language is standard.
The most popular rival to GraphQL is REST, even though this comparison is not accurate 100% because REST is not really a query language, but there are some similarities between GraphQL APIs and RESTful web APIs.
For more information about the differences between GraphQL and REST, go to the home page of graphql.org and scroll down to understand the selling points of this technology.
In the remainder of this article, I will assume you’re familiar with GraphQL basic concepts.
For the ownership point, we will focus on specialty-based or interest-based teams (call them chapters, guilds, tribes, or whatever). Following this model, you would have for example backend team, web team, mobile team, QA team, and others.
This can indirectly push web developers to build and maintain GraphQL APIs (especially for small projects), or in the best cases, it will introduce a practice that some engineering managers hate which is “use whatever programming language in the backend based on the use case you’re facing”.
“Freeing yourself was one thing, claiming ownership of that freed self was another.” - Toni Morrison
Why did we choose Golang?
But the ownership point was the deal-breaker. Our backend team was trying to build expertise in Golang and Java but we got to a point where our biggest GraphQL API was maintained by the web team.
In the remainder of this series, I will assume you’re familiar with GraphQL basic concepts and Golang basic syntax.
“If Go had come out much earlier, I think it might have blown C++ out of the water.” - Bruce Eckel
Why gqlgen was the most convenient option?
gqlgen is a Go library for building GraphQL servers built by 99designs an Australian company that operates a freelancer platform for connecting graphic designers and clients.
This library was the most convenient option for our company, here is why
Talk is cheap. show me the code.
Now let’s write some code, check part 2 of this series to see how to code a GraphQL API using Golang.
“Talk is cheap. Show me the code.” - Linus Torvalds