3 Go-Based GraphQL Packages for Your Next API
GraphQL is an HTTP-based specification that solves most of the issues you’ll encounter while building RESTful APIs. It’s suitable for building complex APIs since it’s possible to use one endpoint to access data from many schemas.
GraphQL mitigates issues like over-fetching and under-fetching in REST. You can build a client that requests specific fields without having to make extra API calls.

There are several Go packages it’s possible to take advantage of to build GraphQL-based applications, from servers to APIs.
1. The gqlgen Package
gqlgen (GraphQL Generator)is a feature-rich, type-safe package for generating and building GraphQL servers and APIs.
The gqlgen package takes a schema-first approach, by which you use the GraphQL SDL to define your schema. It then generates the boilerplate code that you may adjust to set up your GraphQL server and API.

gqlgen is one of the more complete GraphQL packages inthe Go ecosystem. You can generate documentation and examples with the package and create queries, mutations, and subscriptions.
gqlgen ensures type bindings, embeddings, interfaces, generated inputs, and enums. The package also provides functionality for open tracing, hooks for error logging, data loading, concurrency, and increased query complexity.

After defining your GraphQL schema—as you would with any schema-first library—you’ll use the gqlgen command-line app to generate the boilerplate code from the schema in your project.
Create atools.gofile in your working directory and add these lines of code to include thegqlgenpackage:

Thetools.gofile specifies the build tools for thegqlgenpackage.
Run these commands in your working directory to install the gqlgen package and its dependencies:

You can initialize a new GraphQL project when you run the GraphQL package with theinitcommand as an argument:
You’ll need to have your schema in aschema.graphqlfile located in your working directory to initialize a project.
Run theserver.gofile to start your GraphQL server after adding functionality to your GraphQL app:
2. The graphql-go Package
The graphql-go packageis a popular GraphQL library that aims to provide the completeGraphQL draft specificationfor building GraphQL services in Go.
The graphql-go package takes the runtime types approach; you have the option to declare your schema in Go code, and the package checks on runtime.
You can implement queries, mutations, and subscriptions and generate examples with the package, but there’s no functionality for generated enums, inputs, or open tracing.
graphql-go features a minimal API with support for both built-in packages and popular third-party ones. It has support for theOpenTelemetryandOpenTracingstandards, schema type checking against resolvers, parallel execution of resolvers, and many other features.
If you’re familiar withbuilding RESTful services in Go with thehttppackage,you’ll find the graphql-go package easy to use.
Run these commands in your working directory to add the graphql-go package and its dependencies to your project:
Here’s an example of starting a simple GraphQL server:
TheHellomethod of thequerystruct is a resolver for the GraphQL endpoint that returns hello world. TheschemaExamplevariable is the schema definition, and the server will run on port 8080 with thehttppackage’sListenAndServemethod.
3. The Thunder Package
TheThunderframework takes the struct first approach; you declare a struct that models your GraphQL schema. It generates theGraphQL schemafrom the Go data to handle query batching, live queries, mutations, subscriptions, and example generation.
Thunder provides type safety with type bindings and other features, including reflection-based schema building, built-in parallel execution and batching, a built-in GraphiQL editor, and split schemas for larger GraphQL servers.
There’s no functionality for embedding, interfaces, generated enums or inputs, federation, open tracing, or custom errors in the Thunder package. However, it is one of the easiest to use compared to other popular packages and is an excellent starting package if you have no GraphQL experience.
You’ll need to run this command in the terminal of your working directory to install the Thunder package and its dependencies:
You’ll need to declare a struct model for the schema, write the resolvers, and instantiate the server to start a simple GraphQL server with the Thunder package.
Thepoststruct is the model for the GraphQL schema, and theserverstruct is the server instance. TheregisterQuery,registerMutation, andregisterPostmethods are resolver functions for queries, mutations, and data storage.
Themainfunction starts with the GraphQL server on port3030and the GraphQL editor.
You Can Query GraphQL APIs in Go With Built-In Packages
GraphQL is HTTP-based, and you could consume GraphQL APIs with the built-inhttppackage and other packages similar to RESTful APIs. There are also packages in Go’s ecosystem that will allow you to consume GraphQL APIs quickly.
Learn about this REST alternative and how to use it with Go.
Some subscriptions are worth the recurring cost, but not these ones.
Tor spoiled me forever.
This small feature makes a massive difference.
One casual AI chat exposed how vulnerable I was.
It’s not super flashy, but it can help to keep your computer up and running.