Log in to GraphQL EditorGet started
Genqlient

Tomek Poniatowicz

11/2/2021

Genqlient: a type-safe GraphQL client written in Go

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!

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

Check out our other blogposts

Unlocking the Power of React 19
Tomasz Gajda
Tomasz Gajda
Unlocking the Power of React 19
1 min read
about 1 month ago
Zeus update - GraphQL spread operator
Michał Tyszkiewicz
Michał Tyszkiewicz
Zeus update - GraphQL spread operator
1 min read
2 months ago
What is GraphQL Zeus?
Michał Tyszkiewicz
Michał Tyszkiewicz
What is GraphQL Zeus?
1 min read
2 months ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor