Skip to content
Santekno.com | Tech Tutorials and Trends
EN
📖 0%
11 Oct 2025 · 5 min read ·Article 103 / 125
Go

103 Initializing gqlgen with `go run github.com/99designs/gqlgen init`

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

103 Initializing gqlgen with go run github.com/99designs/gqlgen init

If you’re exploring GraphQL in the Go ecosystem, you’ll almost certainly come across gqlgen . Built by the 99designs team, this library has become the de facto standard in modern codebases that want to build GraphQL APIs with Go. One of the reasons gqlgen is so well-liked is its ability to generate code using a schema-first approach, which helps maintain consistency and speeds up prototyping.

In this article, I’ll walk through the steps to initialize gqlgen from scratch using a single command:
go run github.com/99designs/gqlgen init
We’ll look at the generated code, run an end-to-end simulation, and even see how to understand its flow using a diagram. This is the essential foundation you need to grasp before moving on to more advanced topics like database integration or authentication.

Why Do You Need gqlgen?

Let’s compare it with the traditional REST API approach:

REST API (net/http)GraphQL with gqlgen
Many endpointsSingle endpoint
Over/Under-fetching of dataFetch exactly what you need
Manual handler & routingAutomatic via schema
Documentation often manualAuto via Playground/Tools

GraphQL with gqlgen reduces the boilerplate burden and provides out-of-the-box tooling, such as automatic schema binding and interactive documentation.

Steps to Initialize gqlgen

Let’s start from an empty project:

bash
1$ mkdir gqlgen-demo && cd gqlgen-demo
2$ go mod init github.com/username/gqlgen-demo

The next step—which many beginners skip because there’s an npm/yarn/git submodule shortcut—is this super powerful command:

bash
1go run github.com/99designs/gqlgen init

The output looks something like this:

text
1go: downloading github.com/99designs/gqlgen ...
2Execing "go run github.com/99designs/gqlgen init"
3Generated server

Your directory now has a structure like this:

text
 1.
 2├── go.mod
 3├── gqlgen.yml
 4├── schema.graphqls
 5├── graph/
 6│   ├── generated/
 7│   ├── model/
 8│   ├── resolver.go
 9│   └── schema.resolvers.go
10└── server.go

Let’s go over a few of the important files.

Explaining the File Structure

File/FolderPurpose
gqlgen.ymlMain configuration: paths, packages, hooks
schema.graphqlsWhere you define the GraphQL schema, based on SDL
graph/All code related to logic, resolvers, models, etc.
graph/generated/(AUTO) Internal code generated by gqlgen
graph/model/Models that correspond to the schema
server.goBootstraps the HTTP server using gqlgen

schema.graphqls

The generated schema template:

graphql
 1type Todo {
 2    id: ID!
 3    text: String!
 4    done: Boolean!
 5}
 6
 7type Query {
 8    todos: [Todo!]!
 9}
10type Mutation {
11    createTodo(text: String!): Todo!
12}
This is a small example so you can run and test it right away.

server.go

The server entrypoint code will look like this:

go
 1package main
 2
 3import (
 4    "log"
 5    "net/http"
 6    "github.com/99designs/gqlgen/graphql/handler"
 7    "github.com/99designs/gqlgen/graphql/playground"
 8    "gqlgen-demo/graph"
 9    "gqlgen-demo/graph/generated"
10)
11
12func main() {
13    srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
14    http.Handle("/", playground.Handler("GraphQL playground", "/query"))
15    http.Handle("/query", srv)
16
17    log.Printf("connect to http://localhost:8080/ for GraphQL playground")
18    log.Fatal(http.ListenAndServe(":8080", nil))
19}
Notice how playground is attached at the root path as a helper for testing the API interactively.


Simulation: Starting the Server & Testing

Just run:

bash
1go run server.go

Then open your browser to http://localhost:8080/ , and the GraphQL Playground interface will appear.

Sample Queries

Adding a Todo:

graphql
1mutation {
2    createTodo(text: "Belajar gqlgen") {
3        id
4        text
5        done
6    }
7}

Retrieving All Todos:

graphql
1query {
2    todos {
3        id
4        text
5        done
6    }
7}

Since the backend is still in memory, the data is lost on every restart—but that’s good enough for a POC.


Explaining gqlgen’s Automation Flow

To make it easier to understand, I’ve created a diagram of this library’s initialization flow using Mermaid syntax:

MERMAID
flowchart TD
    A[Mulai di root project] --> B[Eksekusi perintah 'go run github.com/99designs/gqlgen init']
    B --> C[Download dependensi utama]
    C --> D[Generate:
        - gqlgen.yml
        - schema.graphqls
        - graph folder beserta kode boilerplate
        - server.go]
    D --> E[Developer modifikasi schema.graphqls, lalu jalanin 'go generate ./...']
    E --> F[Auto regenerate GraphQL types & resolvers stub]
    F --> G[Implementasi business logic]
The diagram above shows that init is the very first setup step that gets you developing right away. After that, the typical cycle just involves modifying the schema and running go generate ./... to keep your type and resolver files in sync.


A Unique Point: go run ... init vs. Manual Install

Usually, people install a library via:

bash
1go get github.com/99designs/gqlgen

But with
go run github.com/99designs/gqlgen init,
you immediately:

  • download the package temporarily without adding bloated dependencies
  • generate the project structure
  • minimize manual errors during the setup step

The advantage is that you can do a one-liner setup without any clutter.


Professional Tips for Using gqlgen

1. Separate Models and Resolvers

Rather than letting the logic in *_resolver.go grow increasingly complex, it’s better to modularize it into its own package.

2. Library Version

Pin the version in go.mod to keep your code stable:

go
1require (
2    github.com/99designs/gqlgen v0.18.0 // example of a stable version
3)

3. Database Integration

The generated resolvers receive a context, making it easy to inject a DB/ORM dependency:

go
1type Resolver struct {
2    DB *gorm.DB
3}

4. Custom Scalars and Middlewares

With custom scalars (Upload, DateTime, etc.) and directives, you can set up constraints/authorization just like REST middleware.


FAQ

Q: What if the schema changes?
A: Just update schema.graphqls, then run go generate ./... again – the new resolvers are automatically stubbed.

Q: Can it be multi-module?
A: Yes, edit gqlgen.yml to set the graph path and output module as needed.


Conclusion

Initializing gqlgen with
go run github.com/99designs/gqlgen init
is the fast track for setting up a modern, Go-based GraphQL server. This single-line process shortens the tooling setup, generates schema-first code, and at the same time delivers an interactive, production-ready playground. The next steps simply involve fleshing out your models, business logic, and documentation—this tooling will keep the foundation of your codebase maintainable.

So, if you need a production-ready Go API stack with GraphQL without the hassle, this is definitely where you start here!

Related Articles

💬 Comments