GraphQL in Go: Complete Guide to the gqlgen Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gqlgen Library for Go (Golang): Creating a GraphQL API



GraphQL has become the de facto standard for building modern APIs due to its flexibility and efficiency. In the Go (Golang) ecosystem, one of the most powerful and popular libraries for working with GraphQL is gqlgen. It provides developers with a convenient way to define schemas, automatic generation of boilerplate code, and high performance.



What is gqlgen and why choose it?



gqlgen (short for "graphql generator") is a Go library that builds a GraphQL server based on a schema. Its main feature is a code-first approach: you describe the schema in a .graphql file, and the library generates all the necessary Go code (structures, interfaces for resolvers). This eliminates the routine of writing boilerplate code and reduces the likelihood of errors.



Key advantages:

  • Automatic code generation: types, methods, and resolver interfaces are created from the schema.
  • Type safety: all types are checked at Go compile time.
  • Performance: the library is optimized for high-load systems.
  • Support for Subscriptions: ability to implement real-time functionality via WebSocket.
  • Flexibility: easily integrates with databases, caches, and other services.


Installation and Setup



To get started, you need to install gqlgen and initialize the project:



// Install the librarygo get github.com/99designs/gqlgen@latest

// Initialize the project skeletongo run github.com/99designs/gqlgen init


After running the init command, the project structure will be created:

  • graph/schema.graphqls — schema file.
  • graph/resolver.go — root resolver.
  • graph/schema.resolvers.go — generated stubs for resolvers.
  • server.go — entry point for starting the server.


Defining a GraphQL Schema



In the schema.graphqls file, types, queries, and mutations are described. Example schema for a blog system:



# Definition of the User typetype User {  id: ID!  name: String!  email: String!  posts: [Post!]!}

# Definition of the Post typetype Post { id: ID! title: String! content: String! author: User!}

# Root Query typetype Query { user(id: ID!): User users: [User!]! post(id: ID!): Post posts: [Post!]!}

# Root Mutation typetype Mutation { createUser(name: String!, email: String!): User! createPost(title: String!, content: String!, authorId: ID!): Post!}


Code Generation and Implementing Resolvers



After describing the schema, run code generation:



go run github.com/99designs/gqlgen generate


The library will create files with Go structures and interfaces. Now you need to implement resolvers — functions that return data. Example of implementing a resolver for fetching a user:



// graph/schema.resolvers.go (implementation example)func (r *queryResolver) User(ctx context.Context, id string) (*model.User, error) {    // Logic for fetching a user from the database    user, err := r.db.GetUserByID(ctx, id)    if err != nil {        return nil, err    }    return user, nil}

func (r *queryResolver) Users(ctx context.Context) ([]*model.User, error) { // Logic for fetching a list of all users return r.db.GetAllUsers(ctx)}

// Resolver for the posts field on User (resolving nested data)func (r *userResolver) Posts(ctx context.Context, obj *model.User) ([]*model.Post, error) { // obj is the User object for which posts are requested

Recommendations