github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/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  ```go
    14  // +build ignore
    15  
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"log"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/99designs/gqlgen/api"
    27  	"github.com/99designs/gqlgen/codegen/config"
    28  	"github.com/99designs/gqlgen/plugin/stubgen"
    29  )
    30  
    31  func main() {
    32  	cfg, err := config.LoadConfigFromDefaultLocations()
    33  	if err != nil {
    34  		fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
    35  		os.Exit(2)
    36  	}
    37  
    38  
    39  	err = api.Generate(cfg,
    40  		api.AddPlugin(yourplugin.New()), // This is the magic line
    41  	)
    42  	if err != nil {
    43  		fmt.Fprintln(os.Stderr, err.Error())
    44  		os.Exit(3)
    45  	}
    46  }
    47  
    48  ```
    49  
    50  ## Writing a plugin
    51  
    52  There are currently only two hooks:
    53  
    54  - MutateConfig: Allows a plugin to mutate the config before codegen starts. This allows plugins to add
    55    custom directives, define types, and implement resolvers. see
    56    [modelgen](https://github.com/99designs/gqlgen/tree/master/plugin/modelgen) for an example
    57  - GenerateCode: Allows a plugin to generate a new output file, see
    58    [stubgen](https://github.com/99designs/gqlgen/tree/master/plugin/stubgen) for an example
    59  
    60  Take a look at [plugin.go](https://github.com/99designs/gqlgen/blob/master/plugin/plugin.go) for the full list of
    61  available hooks. These are likely to change with each release.