github.com/99designs/gqlgen@v0.17.45/docs/content/reference/plugins.md (about) 1 --- 2 linkTitle: Plugins 3 title: How to write plugins for gqlgen 4 description: Use plugins to customize code generation and integrate with other libraries 5 menu: { main: { parent: "reference", weight: 10 } } 6 --- 7 8 Plugins provide a way to hook into the gqlgen code generation lifecycle. In order to use anything other than the 9 default plugins you will need to create your own entrypoint: 10 11 ## Using a plugin 12 13 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: 14 15 ```go 16 // go:build ignore 17 18 package main 19 20 import ( 21 "flag" 22 "fmt" 23 "io" 24 "log" 25 "os" 26 "time" 27 28 "github.com/99designs/gqlgen/api" 29 "github.com/99designs/gqlgen/codegen/config" 30 "github.com/99designs/gqlgen/plugin/stubgen" 31 ) 32 33 func main() { 34 cfg, err := config.LoadConfigFromDefaultLocations() 35 if err != nil { 36 fmt.Fprintln(os.Stderr, "failed to load config", err.Error()) 37 os.Exit(2) 38 } 39 40 41 err = api.Generate(cfg, 42 api.AddPlugin(yourplugin.New()), // This is the magic line 43 ) 44 if err != nil { 45 fmt.Fprintln(os.Stderr, err.Error()) 46 os.Exit(3) 47 } 48 } 49 50 ``` 51 52 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. 53 54 ## Writing a plugin 55 56 There are currently only two hooks: 57 58 - MutateConfig: Allows a plugin to mutate the config before codegen starts. This allows plugins to add 59 custom directives, define types, and implement resolvers. see 60 [modelgen](https://github.com/99designs/gqlgen/tree/master/plugin/modelgen) for an example 61 - GenerateCode: Allows a plugin to generate a new output file, see 62 [stubgen](https://github.com/99designs/gqlgen/tree/master/plugin/stubgen) for an example 63 64 Take a look at [plugin.go](https://github.com/99designs/gqlgen/blob/master/plugin/plugin.go) for the full list of 65 available hooks. These are likely to change with each release.