Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (47 sloc) 1.58 KB
linkTitle title description menu
Plugins
How to write plugins for gqlgen
Use plugins to customize code generation and integrate with other libraries
main
parent
reference

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

// +build ignore

package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"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)
	}
}

Writing a plugin

There are currently only two hooks:

  • MutateConfig: Allows a plugin to mutate the config before codegen starts. This allows plugins to add custom directives, define types, and implement resolvers. see modelgen for an example
  • GenerateCode: Allows a plugin to generate a new output file, see stubgen for an example

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

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.