github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/plugin/resolvergen/resolver.go (about)

     1  package resolvergen
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	"github.com/99designs/gqlgen/codegen"
     8  	"github.com/99designs/gqlgen/codegen/templates"
     9  	"github.com/99designs/gqlgen/plugin"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func New() plugin.Plugin {
    14  	return &Plugin{}
    15  }
    16  
    17  type Plugin struct{}
    18  
    19  var _ plugin.CodeGenerator = &Plugin{}
    20  
    21  func (m *Plugin) Name() string {
    22  	// TODO: typo, should be resolvergen
    23  	return "resovlergen"
    24  }
    25  func (m *Plugin) GenerateCode(data *codegen.Data) error {
    26  	if !data.Config.Resolver.IsDefined() {
    27  		return nil
    28  	}
    29  
    30  	resolverBuild := &ResolverBuild{
    31  		Data:         data,
    32  		PackageName:  data.Config.Resolver.Package,
    33  		ResolverType: data.Config.Resolver.Type,
    34  	}
    35  	filename := data.Config.Resolver.Filename
    36  
    37  	if _, err := os.Stat(filename); os.IsNotExist(errors.Cause(err)) {
    38  		return templates.Render(templates.Options{
    39  			PackageName: data.Config.Resolver.Package,
    40  			Filename:    data.Config.Resolver.Filename,
    41  			Data:        resolverBuild,
    42  		})
    43  	}
    44  
    45  	log.Printf("Skipped resolver: %s already exists\n", filename)
    46  	return nil
    47  }
    48  
    49  type ResolverBuild struct {
    50  	*codegen.Data
    51  
    52  	PackageName  string
    53  	ResolverType string
    54  }