master
master v0.17.56 v0.17.55 v0.17.54 v0.17.53 v0.17.52 v0.17.51 v0.17.50 v0.17.49 v0.17.48 v0.17.47 v0.17.46 v0.17.45 v0.17.44 v0.17.43 v0.17.42 v0.17.41 v0.17.40 v0.17.39 v0.17.38 v0.17.37

Plugins

How to write plugins for gqlgen
[edit]
You are looking at the docs for the unreleased master branch. The latest version is v0.17.56.

Plugins provide a way to hook into the gqlgen code generation lifecycle. In order to use anything other than the default plugins you will need to create your own entrypoint:

Using a plugin

To use a plugin during code generation, you need to create a new entry point. Create generate.go in the same folder as resolver.go with the following code:

// go:build ignore

package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"time"

	"github.com/99designs/gqlgen/api"
	"github.com/99designs/gqlgen/codegen/config"
	"github.com/99designs/gqlgen/plugin/stubgen"
)

func main() {
	cfg, err := config.LoadConfigFromDefaultLocations()
	if err != nil {
		fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
		os.Exit(2)
	}


	err = api.Generate(cfg,
		api.AddPlugin(yourplugin.New()), // This is the magic line
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(3)
	}
}

In resolver.go, add //go:generate go run generate.go. Now you can run go generate ./... instead of go run github.com/99designs/gqlgen generate to generate the code.

Writing a plugin

There are currently only two hooks:

Take a look at plugin.go for the full list of available hooks. These are likely to change with each release.