Genqlient: a type-safe GraphQL client written in Go

Tomek PoniatowiczTomek Poniatowicz

GraphQL is a new query language for APIs that was developed by Facebook. It's similar to REST but provides clients with more control over what data they get back from the server and how itโ€™s formatted. The main benefit of GraphQL is increased performance: you only fetch the data you need, and not everything. This library will help programmers write type-safe GraphQL queries using Go as it takes advantage of both "languages" being type-safe so there won't be any errors at compile time!

genqlient is a GraphQL client written in Go aiming to provide even more type-safety for your GraphQL APIs. It takes advantage of the fact that both Go & GraphQL are strongly typed and impose a rigorous set of rules thus guaranteeing a certain consistency of results.

Genqlient vs other

So in general what's the difference between genqlient and other GraphQL clients or what it has to offer that others don't? It guarantees no errors at compile-time or risks of executing your queries incorrectly because they can always be checked for validity beforehand.

As mentioned by genqlient's author, most popular Go GraphQL clients will make you write your code more or less like this:

Source: https://github.com/Khan/genqlient

The above code works, but there is definitely space for improvements as:

  • The response structure is type-safe at the Go level, but there's nothing that could validate if your schema looks like it's expected i.e maybe a field is called different or it has the wrong capitalization - we won't know until runtime,
  • The only way to know if your variable was passed correctly is by checking at runtime, which can be a pain as you could have passed {"id": true} and you won't notice until runtime,

So what genqlient has to offer that others don't?

Compile-time validation

Say goodbye to runtime errors as Genqlient provides a compile-time validation of your GraphQL queries making sure you won't ship an invalid GraphQL query again which means no more runtime errors!

To achieve this, Genqlient uses Go's powerful type system to ensure that any invalid query would be caught at compile time.

Type-safe response objects

Genqlient generates the right type for each query, so you know the response will unmarshal correctly. GenQLient takes advantage of Go is a statically-typed language and compiles both GraphQL schema declaration and queries to types which makes it easy for us to generate result type for any query.

โšก Speed up your GraphQL API development

The GraphQL Editor is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster thanks to dozens of built-in micro features. Its graphical interface will also fix communication within your product team. Visualization is the key!

Try it for free