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

     1  package servergen
     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(filename string) plugin.Plugin {
    14  	return &Plugin{filename}
    15  }
    16  
    17  type Plugin struct {
    18  	filename string
    19  }
    20  
    21  var _ plugin.CodeGenerator = &Plugin{}
    22  
    23  func (m *Plugin) Name() string {
    24  	return "servergen"
    25  }
    26  func (m *Plugin) GenerateCode(data *codegen.Data) error {
    27  	serverBuild := &ServerBuild{
    28  		ExecPackageName:     data.Config.Exec.ImportPath(),
    29  		ResolverPackageName: data.Config.Resolver.ImportPath(),
    30  	}
    31  
    32  	if _, err := os.Stat(m.filename); os.IsNotExist(errors.Cause(err)) {
    33  		return templates.Render(templates.Options{
    34  			PackageName: "main",
    35  			Filename:    m.filename,
    36  			Data:        serverBuild,
    37  		})
    38  	}
    39  
    40  	log.Printf("Skipped server: %s already exists\n", m.filename)
    41  	return nil
    42  }
    43  
    44  type ServerBuild struct {
    45  	codegen.Data
    46  
    47  	ExecPackageName     string
    48  	ResolverPackageName string
    49  }