diff options
Diffstat (limited to 'vendor/github.com')
26 files changed, 2531 insertions, 0 deletions
diff --git a/vendor/github.com/vektah/gqlgen/codegen/build.go b/vendor/github.com/vektah/gqlgen/codegen/build.go new file mode 100644 index 00000000..d56fc06f --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/build.go @@ -0,0 +1,165 @@ +package codegen + +import ( + "fmt" + "go/build" + "go/types" + "os" + + "github.com/pkg/errors" + "golang.org/x/tools/go/loader" +) + +type Build struct { + PackageName string + Objects Objects + Inputs Objects + Interfaces []*Interface + Imports []*Import + QueryRoot *Object + MutationRoot *Object + SubscriptionRoot *Object + SchemaRaw string +} + +type ModelBuild struct { + PackageName string + Imports []*Import + Models []Model + Enums []Enum +} + +// Create a list of models that need to be generated +func (cfg *Config) models() (*ModelBuild, error) { + namedTypes := cfg.buildNamedTypes() + + prog, err := cfg.loadProgram(namedTypes, true) + if err != nil { + return nil, errors.Wrap(err, "loading failed") + } + imports := buildImports(namedTypes, cfg.Model.Dir()) + + cfg.bindTypes(imports, namedTypes, cfg.Model.Dir(), prog) + + models, err := cfg.buildModels(namedTypes, prog) + if err != nil { + return nil, err + } + return &ModelBuild{ + PackageName: cfg.Model.Package, + Models: models, + Enums: cfg.buildEnums(namedTypes), + Imports: imports.finalize(), + }, nil +} + +// bind a schema together with some code to generate a Build +func (cfg *Config) bind() (*Build, error) { + namedTypes := cfg.buildNamedTypes() + + prog, err := cfg.loadProgram(namedTypes, true) + if err != nil { + return nil, errors.Wrap(err, "loading failed") + } + + imports := buildImports(namedTypes, cfg.Exec.Dir()) + cfg.bindTypes(imports, namedTypes, cfg.Exec.Dir(), prog) + + objects, err := cfg.buildObjects(namedTypes, prog, imports) + if err != nil { + return nil, err + } + + inputs, err := cfg.buildInputs(namedTypes, prog, imports) + if err != nil { + return nil, err + } + + b := &Build{ + PackageName: cfg.Exec.Package, + Objects: objects, + Interfaces: cfg.buildInterfaces(namedTypes, prog), + Inputs: inputs, + Imports: imports.finalize(), + SchemaRaw: cfg.SchemaStr, + } + + if qr, ok := cfg.schema.EntryPoints["query"]; ok { + b.QueryRoot = b.Objects.ByName(qr.TypeName()) + } + + if mr, ok := cfg.schema.EntryPoints["mutation"]; ok { + b.MutationRoot = b.Objects.ByName(mr.TypeName()) + } + + if sr, ok := cfg.schema.EntryPoints["subscription"]; ok { + b.SubscriptionRoot = b.Objects.ByName(sr.TypeName()) + } + + if b.QueryRoot == nil { + return b, fmt.Errorf("query entry point missing") + } + + // Poke a few magic methods into query + q := b.Objects.ByName(b.QueryRoot.GQLType) + q.Fields = append(q.Fields, Field{ + Type: &Type{namedTypes["__Schema"], []string{modPtr}, nil}, + GQLName: "__schema", + NoErr: true, + GoMethodName: "ec.introspectSchema", + Object: q, + }) + q.Fields = append(q.Fields, Field{ + Type: &Type{namedTypes["__Type"], []string{modPtr}, nil}, + GQLName: "__type", + NoErr: true, + GoMethodName: "ec.introspectType", + Args: []FieldArgument{ + {GQLName: "name", Type: &Type{namedTypes["String"], []string{}, nil}, Object: &Object{}}, + }, + Object: q, + }) + + return b, nil +} + +func (cfg *Config) validate() error { + namedTypes := cfg.buildNamedTypes() + + _, err := cfg.loadProgram(namedTypes, false) + return err +} + +func (cfg *Config) loadProgram(namedTypes NamedTypes, allowErrors bool) (*loader.Program, error) { + conf := loader.Config{} + if allowErrors { + conf = loader.Config{ + AllowErrors: true, + TypeChecker: types.Config{ + Error: func(e error) {}, + }, + } + } + for _, imp := range ambientImports { + conf.Import(imp) + } + + for _, imp := range namedTypes { + if imp.Package != "" { + conf.Import(imp.Package) + } + } + + return conf.Load() +} + +func resolvePkg(pkgName string) (string, error) { + cwd, _ := os.Getwd() + + pkg, err := build.Default.Import(pkgName, cwd, build.FindOnly) + if err != nil { + return "", err + } + + return pkg.ImportPath, nil +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/codegen.go b/vendor/github.com/vektah/gqlgen/codegen/codegen.go new file mode 100644 index 00000000..789ef2ec --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/codegen.go @@ -0,0 +1,153 @@ +package codegen + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "regexp" + "syscall" + + "github.com/pkg/errors" + "github.com/vektah/gqlgen/codegen/templates" + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/imports" +) + +func Generate(cfg Config) error { + if err := cfg.normalize(); err != nil { + return err + } + + _ = syscall.Unlink(cfg.Exec.Filename) + _ = syscall.Unlink(cfg.Model.Filename) + + modelsBuild, err := cfg.models() + if err != nil { + return errors.Wrap(err, "model plan failed") + } + if len(modelsBuild.Models) > 0 || len(modelsBuild.Enums) > 0 { + var buf *bytes.Buffer + buf, err = templates.Run("models.gotpl", modelsBuild) + if err != nil { + return errors.Wrap(err, "model generation failed") + } + + if err = write(cfg.Model.Filename, buf.Bytes()); err != nil { + return err + } + for _, model := range modelsBuild.Models { + modelCfg := cfg.Models[model.GQLType] + modelCfg.Model = cfg.Model.ImportPath() + "." + model.GoType + cfg.Models[model.GQLType] = modelCfg + } + + for _, enum := range modelsBuild.Enums { + modelCfg := cfg.Models[enum.GQLType] + modelCfg.Model = cfg.Model.ImportPath() + "." + enum.GoType + cfg.Models[enum.GQLType] = modelCfg + } + } + + build, err := cfg.bind() + if err != nil { + return errors.Wrap(err, "exec plan failed") + } + + var buf *bytes.Buffer + buf, err = templates.Run("generated.gotpl", build) + if err != nil { + return errors.Wrap(err, "exec codegen failed") + } + + if err = write(cfg.Exec.Filename, buf.Bytes()); err != nil { + return err + } + + if err = cfg.validate(); err != nil { + return errors.Wrap(err, "validation failed") + } + + return nil +} + +func (cfg *Config) normalize() error { + if err := cfg.Model.normalize(); err != nil { + return errors.Wrap(err, "model") + } + + if err := cfg.Exec.normalize(); err != nil { + return errors.Wrap(err, "exec") + } + + builtins := TypeMap{ + "__Directive": {Model: "github.com/vektah/gqlgen/neelance/introspection.Directive"}, + "__Type": {Model: "github.com/vektah/gqlgen/neelance/introspection.Type"}, + "__Field": {Model: "github.com/vektah/gqlgen/neelance/introspection.Field"}, + "__EnumValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.EnumValue"}, + "__InputValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.InputValue"}, + "__Schema": {Model: "github.com/vektah/gqlgen/neelance/introspection.Schema"}, + "Int": {Model: "github.com/vektah/gqlgen/graphql.Int"}, + "Float": {Model: "github.com/vektah/gqlgen/graphql.Float"}, + "String": {Model: "github.com/vektah/gqlgen/graphql.String"}, + "Boolean": {Model: "github.com/vektah/gqlgen/graphql.Boolean"}, + "ID": {Model: "github.com/vektah/gqlgen/graphql.ID"}, + "Time": {Model: "github.com/vektah/gqlgen/graphql.Time"}, + "Map": {Model: "github.com/vektah/gqlgen/graphql.Map"}, + } + + if cfg.Models == nil { + cfg.Models = TypeMap{} + } + for typeName, entry := range builtins { + if !cfg.Models.Exists(typeName) { + cfg.Models[typeName] = entry + } + } + + cfg.schema = schema.New() + return cfg.schema.Parse(cfg.SchemaStr) +} + +var invalidPackageNameChar = regexp.MustCompile(`[^\w]`) + +func sanitizePackageName(pkg string) string { + return invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_") +} + +func abs(path string) string { + absPath, err := filepath.Abs(path) + if err != nil { + panic(err) + } + return filepath.ToSlash(absPath) +} + +func gofmt(filename string, b []byte) ([]byte, error) { + out, err := imports.Process(filename, b, nil) + if err != nil { + return b, errors.Wrap(err, "unable to gofmt") + } + return out, nil +} + +func write(filename string, b []byte) error { + err := os.MkdirAll(filepath.Dir(filename), 0755) + if err != nil { + return errors.Wrap(err, "failed to create directory") + } + + formatted, err := gofmt(filename, b) + if err != nil { + fmt.Fprintf(os.Stderr, "gofmt failed: %s\n", err.Error()) + formatted = b + } + + err = ioutil.WriteFile(filename, formatted, 0644) + if err != nil { + return errors.Wrapf(err, "failed to write %s", filename) + } + + return nil +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/config.go b/vendor/github.com/vektah/gqlgen/codegen/config.go new file mode 100644 index 00000000..cd42ae6b --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/config.go @@ -0,0 +1,184 @@ +package codegen + +import ( + "fmt" + "go/build" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/pkg/errors" + "github.com/vektah/gqlgen/neelance/schema" + "gopkg.in/yaml.v2" +) + +var defaults = Config{ + SchemaFilename: "schema.graphql", + Model: PackageConfig{Filename: "models_gen.go"}, + Exec: PackageConfig{Filename: "generated.go"}, +} + +var cfgFilenames = []string{".gqlgen.yml", "gqlgen.yml", "gqlgen.yaml"} + +// LoadDefaultConfig looks for a config file in the current directory, and all parent directories +// walking up the tree. The closest config file will be returned. +func LoadDefaultConfig() (*Config, error) { + cfgFile, err := findCfg() + if err != nil || cfgFile == "" { + cpy := defaults + return &cpy, err + } + + err = os.Chdir(filepath.Dir(cfgFile)) + if err != nil { + return nil, errors.Wrap(err, "unable to enter config dir") + } + return LoadConfig(cfgFile) +} + +// LoadConfig reads the gqlgen.yml config file +func LoadConfig(filename string) (*Config, error) { + config := defaults + + b, err := ioutil.ReadFile(filename) + if err != nil { + return nil, errors.Wrap(err, "unable to read config") + } + + if err := yaml.UnmarshalStrict(b, &config); err != nil { + return nil, errors.Wrap(err, "unable to parse config") + } + + return &config, nil +} + +type Config struct { + SchemaFilename string `yaml:"schema,omitempty"` + SchemaStr string `yaml:"-"` + Exec PackageConfig `yaml:"exec"` + Model PackageConfig `yaml:"model"` + Models TypeMap `yaml:"models,omitempty"` + + schema *schema.Schema `yaml:"-"` +} + +type PackageConfig struct { + Filename string `yaml:"filename,omitempty"` + Package string `yaml:"package,omitempty"` +} + +type TypeMapEntry struct { + Model string `yaml:"model"` + Fields map[string]TypeMapField `yaml:"fields,omitempty"` +} + +type TypeMapField struct { + Resolver bool `yaml:"resolver"` +} + +func (c *PackageConfig) normalize() error { + if c.Filename == "" { + return errors.New("Filename is required") + } + c.Filename = abs(c.Filename) + // If Package is not set, first attempt to load the package at the output dir. If that fails + // fallback to just the base dir name of the output filename. + if c.Package == "" { + cwd, _ := os.Getwd() + pkg, _ := build.Default.Import(c.ImportPath(), cwd, 0) + if pkg.Name != "" { + c.Package = pkg.Name + } else { + c.Package = filepath.Base(c.Dir()) + } + } + c.Package = sanitizePackageName(c.Package) + return nil +} + +func (c *PackageConfig) ImportPath() string { + dir := filepath.ToSlash(c.Dir()) + for _, gopath := range filepath.SplitList(build.Default.GOPATH) { + gopath = filepath.ToSlash(gopath) + "/src/" + if len(gopath) > len(dir) { + continue + } + if strings.EqualFold(gopath, dir[0:len(gopath)]) { + dir = dir[len(gopath):] + break + } + } + return dir +} + +func (c *PackageConfig) Dir() string { + return filepath.ToSlash(filepath.Dir(c.Filename)) +} + +func (c *PackageConfig) Check() error { + if strings.ContainsAny(c.Package, "./\\") { + return fmt.Errorf("package should be the output package name only, do not include the output filename") + } + if c.Filename != "" && !strings.HasSuffix(c.Filename, ".go") { + return fmt.Errorf("filename should be path to a go source file") + } + return nil +} + +func (cfg *Config) Check() error { + if err := cfg.Models.Check(); err != nil { + return errors.Wrap(err, "config.models") + } + if err := cfg.Exec.Check(); err != nil { + return errors.Wrap(err, "config.exec") + } + if err := cfg.Model.Check(); err != nil { + return errors.Wrap(err, "config.model") + } + return nil +} + +type TypeMap map[string]TypeMapEntry + +func (tm TypeMap) Exists(typeName string) bool { + _, ok := tm[typeName] + return ok +} + +func (tm TypeMap) Check() error { + for typeName, entry := range tm { + if strings.LastIndex(entry.Model, ".") < strings.LastIndex(entry.Model, "/") { + return fmt.Errorf("model %s: invalid type specifier \"%s\" - you need to specify a struct to map to", typeName, entry.Model) + } + } + return nil +} + +// findCfg searches for the config file in this directory and all parents up the tree +// looking for the closest match +func findCfg() (string, error) { + dir, err := os.Getwd() + if err != nil { + return "", errors.Wrap(err, "unable to get working dir to findCfg") + } + + cfg := findCfgInDir(dir) + + for cfg == "" && dir != filepath.Dir(dir) { + dir = filepath.Dir(dir) + cfg = findCfgInDir(dir) + } + + return cfg, nil +} + +func findCfgInDir(dir string) string { + for _, cfgName := range cfgFilenames { + path := filepath.Join(dir, cfgName) + if _, err := os.Stat(path); err == nil { + return path + } + } + return "" +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/enum.go b/vendor/github.com/vektah/gqlgen/codegen/enum.go new file mode 100644 index 00000000..e62fd2b1 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/enum.go @@ -0,0 +1,12 @@ +package codegen + +type Enum struct { + *NamedType + + Values []EnumValue +} + +type EnumValue struct { + Name string + Description string +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/enum_build.go b/vendor/github.com/vektah/gqlgen/codegen/enum_build.go new file mode 100644 index 00000000..f2e6f63c --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/enum_build.go @@ -0,0 +1,39 @@ +package codegen + +import ( + "sort" + "strings" + + "github.com/vektah/gqlgen/codegen/templates" + "github.com/vektah/gqlgen/neelance/schema" +) + +func (cfg *Config) buildEnums(types NamedTypes) []Enum { + var enums []Enum + + for _, typ := range cfg.schema.Types { + namedType := types[typ.TypeName()] + e, isEnum := typ.(*schema.Enum) + if !isEnum || strings.HasPrefix(typ.TypeName(), "__") || namedType.IsUserDefined { + continue + } + + var values []EnumValue + for _, v := range e.Values { + values = append(values, EnumValue{v.Name, v.Desc}) + } + + enum := Enum{ + NamedType: namedType, + Values: values, + } + enum.GoType = templates.ToCamel(enum.GQLType) + enums = append(enums, enum) + } + + sort.Slice(enums, func(i, j int) bool { + return strings.Compare(enums[i].GQLType, enums[j].GQLType) == -1 + }) + + return enums +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/import.go b/vendor/github.com/vektah/gqlgen/codegen/import.go new file mode 100644 index 00000000..b511e8f6 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/import.go @@ -0,0 +1,29 @@ +package codegen + +import ( + "strconv" +) + +type Import struct { + Name string + Path string + + alias string +} + +type Imports struct { + imports []*Import + destDir string +} + +func (i *Import) Write() string { + return i.Alias() + " " + strconv.Quote(i.Path) +} + +func (i *Import) Alias() string { + if i.alias == "" { + panic("alias called before imports are finalized") + } + + return i.alias +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/import_build.go b/vendor/github.com/vektah/gqlgen/codegen/import_build.go new file mode 100644 index 00000000..f0877ed3 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/import_build.go @@ -0,0 +1,116 @@ +package codegen + +import ( + "fmt" + "go/build" + "sort" + "strconv" + "strings" +) + +// These imports are referenced by the generated code, and are assumed to have the +// default alias. So lets make sure they get added first, and any later collisions get +// renamed. +var ambientImports = []string{ + "context", + "fmt", + "io", + "strconv", + "time", + "sync", + "github.com/vektah/gqlgen/neelance/introspection", + "github.com/vektah/gqlgen/neelance/errors", + "github.com/vektah/gqlgen/neelance/query", + "github.com/vektah/gqlgen/neelance/schema", + "github.com/vektah/gqlgen/neelance/validation", + "github.com/vektah/gqlgen/graphql", +} + +func buildImports(types NamedTypes, destDir string) *Imports { + imports := Imports{ + destDir: destDir, + } + + for _, ambient := range ambientImports { + imports.add(ambient) + } + + // Imports from top level user types + for _, t := range types { + t.Import = imports.add(t.Package) + } + + return &imports +} + +func (s *Imports) add(path string) *Import { + if path == "" { + return nil + } + + if stringHasSuffixFold(s.destDir, path) { + return nil + } + + if existing := s.findByPath(path); existing != nil { + return existing + } + + pkg, err := build.Default.Import(path, s.destDir, 0) + if err != nil { + panic(err) + } + + imp := &Import{ + Name: pkg.Name, + Path: path, + } + s.imports = append(s.imports, imp) + + return imp +} + +func stringHasSuffixFold(s, suffix string) bool { + return len(s) >= len(suffix) && strings.EqualFold(s[len(s)-len(suffix):], suffix) +} + +func (s Imports) finalize() []*Import { + // ensure stable ordering by sorting + sort.Slice(s.imports, func(i, j int) bool { + return s.imports[i].Path > s.imports[j].Path + }) + + for _, imp := range s.imports { + alias := imp.Name + + i := 1 + for s.findByAlias(alias) != nil { + alias = imp.Name + strconv.Itoa(i) + i++ + if i > 10 { + panic(fmt.Errorf("too many collisions, last attempt was %s", alias)) + } + } + imp.alias = alias + } + + return s.imports +} + +func (s Imports) findByPath(importPath string) *Import { + for _, imp := range s.imports { + if imp.Path == importPath { + return imp + } + } + return nil +} + +func (s Imports) findByAlias(alias string) *Import { + for _, imp := range s.imports { + if imp.alias == alias { + return imp + } + } + return nil +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/input_build.go b/vendor/github.com/vektah/gqlgen/codegen/input_build.go new file mode 100644 index 00000000..98b25b8b --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/input_build.go @@ -0,0 +1,86 @@ +package codegen + +import ( + "go/types" + "sort" + "strings" + + "github.com/pkg/errors" + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, imports *Imports) (Objects, error) { + var inputs Objects + + for _, typ := range cfg.schema.Types { + switch typ := typ.(type) { + case *schema.InputObject: + input, err := buildInput(namedTypes, typ) + if err != nil { + return nil, err + } + + def, err := findGoType(prog, input.Package, input.GoType) + if err != nil { + return nil, errors.Wrap(err, "cannot find type") + } + if def != nil { + input.Marshaler = buildInputMarshaler(typ, def) + bindErrs := bindObject(def.Type(), input, imports) + if len(bindErrs) > 0 { + return nil, bindErrs + } + } + + inputs = append(inputs, input) + } + } + + sort.Slice(inputs, func(i, j int) bool { + return strings.Compare(inputs[i].GQLType, inputs[j].GQLType) == -1 + }) + + return inputs, nil +} + +func buildInput(types NamedTypes, typ *schema.InputObject) (*Object, error) { + obj := &Object{NamedType: types[typ.TypeName()]} + + for _, field := range typ.Values { + newField := Field{ + GQLName: field.Name.Name, + Type: types.getType(field.Type), + Object: obj, + } + + if field.Default != nil { + newField.Default = field.Default.Value(nil) + } + + if !newField.Type.IsInput && !newField.Type.IsScalar { + return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.GQLType, obj.GQLType) + } + + obj.Fields = append(obj.Fields, newField) + + } + return obj, nil +} + +// if user has implemented an UnmarshalGQL method on the input type manually, use it +// otherwise we will generate one. +func buildInputMarshaler(typ *schema.InputObject, def types.Object) *Ref { + switch def := def.(type) { + case *types.TypeName: + namedType := def.Type().(*types.Named) + for i := 0; i < namedType.NumMethods(); i++ { + method := namedType.Method(i) + if method.Name() == "UnmarshalGQL" { + return nil + } + } + } + + return &Ref{GoType: typ.Name} +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/interface.go b/vendor/github.com/vektah/gqlgen/codegen/interface.go new file mode 100644 index 00000000..2de0c88a --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/interface.go @@ -0,0 +1,13 @@ +package codegen + +type Interface struct { + *NamedType + + Implementors []InterfaceImplementor +} + +type InterfaceImplementor struct { + ValueReceiver bool + + *NamedType +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/interface_build.go b/vendor/github.com/vektah/gqlgen/codegen/interface_build.go new file mode 100644 index 00000000..cdf0f597 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/interface_build.go @@ -0,0 +1,94 @@ +package codegen + +import ( + "fmt" + "go/types" + "os" + "sort" + "strings" + + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +func (cfg *Config) buildInterfaces(types NamedTypes, prog *loader.Program) []*Interface { + var interfaces []*Interface + for _, typ := range cfg.schema.Types { + switch typ := typ.(type) { + case *schema.Union, *schema.Interface: + interfaces = append(interfaces, cfg.buildInterface(types, typ, prog)) + default: + continue + } + } + + sort.Slice(interfaces, func(i, j int) bool { + return strings.Compare(interfaces[i].GQLType, interfaces[j].GQLType) == -1 + }) + + return interfaces +} + +func (cfg *Config) buildInterface(types NamedTypes, typ schema.NamedType, prog *loader.Program) *Interface { + switch typ := typ.(type) { + + case *schema.Union: + i := &Interface{NamedType: types[typ.TypeName()]} + + for _, implementor := range typ.PossibleTypes { + t := types[implementor.TypeName()] + + i.Implementors = append(i.Implementors, InterfaceImplementor{ + NamedType: t, + ValueReceiver: cfg.isValueReceiver(types[typ.Name], t, prog), + }) + } + + return i + + case *schema.Interface: + i := &Interface{NamedType: types[typ.TypeName()]} + + for _, implementor := range typ.PossibleTypes { + t := types[implementor.TypeName()] + + i.Implementors = append(i.Implementors, InterfaceImplementor{ + NamedType: t, + ValueReceiver: cfg.isValueReceiver(types[typ.Name], t, prog), + }) + } + + return i + default: + panic(fmt.Errorf("unknown interface %#v", typ)) + } +} + +func (cfg *Config) isValueReceiver(intf *NamedType, implementor *NamedType, prog *loader.Program) bool { + interfaceType, err := findGoInterface(prog, intf.Package, intf.GoType) + if interfaceType == nil || err != nil { + return true + } + + implementorType, err := findGoNamedType(prog, implementor.Package, implementor.GoType) + if implementorType == nil || err != nil { + return true + } + + for i := 0; i < interfaceType.NumMethods(); i++ { + intfMethod := interfaceType.Method(i) + + implMethod := findMethod(implementorType, intfMethod.Name()) + if implMethod == nil { + fmt.Fprintf(os.Stderr, "missing method %s on %s\n", intfMethod.Name(), implementor.GoType) + return false + } + + sig := implMethod.Type().(*types.Signature) + if _, isPtr := sig.Recv().Type().(*types.Pointer); isPtr { + return false + } + } + + return true +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/model.go b/vendor/github.com/vektah/gqlgen/codegen/model.go new file mode 100644 index 00000000..164a04d5 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/model.go @@ -0,0 +1,15 @@ +package codegen + +type Model struct { + *NamedType + + Fields []ModelField +} + +type ModelField struct { + *Type + GQLName string + GoVarName string + GoFKName string + GoFKType string +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/models_build.go b/vendor/github.com/vektah/gqlgen/codegen/models_build.go new file mode 100644 index 00000000..211d4bd4 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/models_build.go @@ -0,0 +1,91 @@ +package codegen + +import ( + "sort" + "strings" + + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) ([]Model, error) { + var models []Model + + for _, typ := range cfg.schema.Types { + var model Model + switch typ := typ.(type) { + case *schema.Object: + obj, err := cfg.buildObject(types, typ) + if err != nil { + return nil, err + } + if obj.Root || obj.IsUserDefined { + continue + } + model = cfg.obj2Model(obj) + case *schema.InputObject: + obj, err := buildInput(types, typ) + if err != nil { + return nil, err + } + if obj.IsUserDefined { + continue + } + model = cfg.obj2Model(obj) + case *schema.Interface, *schema.Union: + intf := cfg.buildInterface(types, typ, prog) + if intf.IsUserDefined { + continue + } + model = int2Model(intf) + default: + continue + } + + models = append(models, model) + } + + sort.Slice(models, func(i, j int) bool { + return strings.Compare(models[i].GQLType, models[j].GQLType) == -1 + }) + + return models, nil +} + +func (cfg *Config) obj2Model(obj *Object) Model { + model := Model{ + NamedType: obj.NamedType, + Fields: []ModelField{}, + } + + model.GoType = ucFirst(obj.GQLType) + model.Marshaler = &Ref{GoType: obj.GoType} + + for i := range obj.Fields { + field := &obj.Fields[i] + mf := ModelField{Type: field.Type, GQLName: field.GQLName} + + mf.GoVarName = ucFirst(field.GQLName) + if mf.IsScalar { + if mf.GoVarName == "Id" { + mf.GoVarName = "ID" + } + } + + model.Fields = append(model.Fields, mf) + } + + return model +} + +func int2Model(obj *Interface) Model { + model := Model{ + NamedType: obj.NamedType, + Fields: []ModelField{}, + } + + model.GoType = ucFirst(obj.GQLType) + model.Marshaler = &Ref{GoType: obj.GoType} + + return model +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/object.go b/vendor/github.com/vektah/gqlgen/codegen/object.go new file mode 100644 index 00000000..1c03c0ba --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/object.go @@ -0,0 +1,206 @@ +package codegen + +import ( + "bytes" + "fmt" + "strconv" + "strings" + "text/template" + "unicode" +) + +type Object struct { + *NamedType + + Fields []Field + Satisfies []string + Root bool + DisableConcurrency bool + Stream bool +} + +type Field struct { + *Type + + GQLName string // The name of the field in graphql + GoMethodName string // The name of the method in go, if any + GoVarName string // The name of the var in go, if any + Args []FieldArgument // A list of arguments to be passed to this field + ForceResolver bool // Should be emit Resolver method + NoErr bool // If this is bound to a go method, does that method have an error as the second argument + Object *Object // A link back to the parent object + Default interface{} // The default value +} + +type FieldArgument struct { + *Type + + GQLName string // The name of the argument in graphql + GoVarName string // The name of the var in go + Object *Object // A link back to the parent object + Default interface{} // The default value +} + +type Objects []*Object + +func (o *Object) Implementors() string { + satisfiedBy := strconv.Quote(o.GQLType) + for _, s := range o.Satisfies { + satisfiedBy += ", " + strconv.Quote(s) + } + return "[]string{" + satisfiedBy + "}" +} + +func (o *Object) HasResolvers() bool { + for _, f := range o.Fields { + if f.IsResolver() { + return true + } + } + return false +} + +func (f *Field) IsResolver() bool { + return f.ForceResolver || f.GoMethodName == "" && f.GoVarName == "" +} + +func (f *Field) IsConcurrent() bool { + return f.IsResolver() && !f.Object.DisableConcurrency +} +func (f *Field) ShortInvocation() string { + if !f.IsResolver() { + return "" + } + shortName := strings.ToUpper(f.GQLName[:1]) + f.GQLName[1:] + res := fmt.Sprintf("%s().%s(ctx", f.Object.GQLType, shortName) + if !f.Object.Root { + res += fmt.Sprintf(", obj") + } + for _, arg := range f.Args { + res += fmt.Sprintf(", %s", arg.GoVarName) + } + res += ")" + return res +} +func (f *Field) ShortResolverDeclaration() string { + if !f.IsResolver() { + return "" + } + decl := strings.TrimPrefix(f.ResolverDeclaration(), f.Object.GQLType+"_") + return strings.ToUpper(decl[:1]) + decl[1:] +} + +func (f *Field) ResolverDeclaration() string { + if !f.IsResolver() { + return "" + } + res := fmt.Sprintf("%s_%s(ctx context.Context", f.Object.GQLType, f.GQLName) + + if !f.Object.Root { + res += fmt.Sprintf(", obj *%s", f.Object.FullName()) + } + for _, arg := range f.Args { + res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature()) + } + + result := f.Signature() + if f.Object.Stream { + result = "<-chan " + result + } + + res += fmt.Sprintf(") (%s, error)", result) + return res +} + +func (f *Field) CallArgs() string { + var args []string + + if f.GoMethodName == "" { + args = append(args, "ctx") + + if !f.Object.Root { + args = append(args, "obj") + } + } + + for _, arg := range f.Args { + args = append(args, "args["+strconv.Quote(arg.GQLName)+"].("+arg.Signature()+")") + } + + return strings.Join(args, ", ") +} + +// should be in the template, but its recursive and has a bunch of args +func (f *Field) WriteJson() string { + return f.doWriteJson("res", f.Type.Modifiers, false, 1) +} + +func (f *Field) doWriteJson(val string, remainingMods []string, isPtr bool, depth int) string { + switch { + case len(remainingMods) > 0 && remainingMods[0] == modPtr: + return fmt.Sprintf("if %s == nil { return graphql.Null }\n%s", val, f.doWriteJson(val, remainingMods[1:], true, depth+1)) + + case len(remainingMods) > 0 && remainingMods[0] == modList: + if isPtr { + val = "*" + val + } + var arr = "arr" + strconv.Itoa(depth) + var index = "idx" + strconv.Itoa(depth) + + return tpl(`{{.arr}} := graphql.Array{} + for {{.index}} := range {{.val}} { + {{.arr}} = append({{.arr}}, func() graphql.Marshaler { + rctx := graphql.GetResolverContext(ctx) + rctx.PushIndex({{.index}}) + defer rctx.Pop() + {{ .next }} + }()) + } + return {{.arr}}`, map[string]interface{}{ + "val": val, + "arr": arr, + "index": index, + "next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], false, depth+1), + }) + + case f.IsScalar: + if isPtr { + val = "*" + val + } + return f.Marshal(val) + + default: + if !isPtr { + val = "&" + val + } + return fmt.Sprintf("return ec._%s(ctx, field.Selections, %s)", f.GQLType, val) + } +} + +func (os Objects) ByName(name string) *Object { + for i, o := range os { + if strings.EqualFold(o.GQLType, name) { + return os[i] + } + } + return nil +} + +func tpl(tpl string, vars map[string]interface{}) string { + b := &bytes.Buffer{} + err := template.Must(template.New("inline").Parse(tpl)).Execute(b, vars) + if err != nil { + panic(err) + } + return b.String() +} + +func ucFirst(s string) string { + if s == "" { + return "" + } + + r := []rune(s) + r[0] = unicode.ToUpper(r[0]) + return string(r) +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/object_build.go b/vendor/github.com/vektah/gqlgen/codegen/object_build.go new file mode 100644 index 00000000..0ef40fef --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/object_build.go @@ -0,0 +1,144 @@ +package codegen + +import ( + "log" + "sort" + "strings" + + "github.com/pkg/errors" + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports *Imports) (Objects, error) { + var objects Objects + + for _, typ := range cfg.schema.Types { + switch typ := typ.(type) { + case *schema.Object: + obj, err := cfg.buildObject(types, typ) + if err != nil { + return nil, err + } + + def, err := findGoType(prog, obj.Package, obj.GoType) + if err != nil { + return nil, err + } + if def != nil { + for _, bindErr := range bindObject(def.Type(), obj, imports) { + log.Println(bindErr.Error()) + log.Println(" Adding resolver method") + } + } + + objects = append(objects, obj) + } + } + + sort.Slice(objects, func(i, j int) bool { + return strings.Compare(objects[i].GQLType, objects[j].GQLType) == -1 + }) + + return objects, nil +} + +var keywords = []string{ + "break", + "default", + "func", + "interface", + "select", + "case", + "defer", + "go", + "map", + "struct", + "chan", + "else", + "goto", + "package", + "switch", + "const", + "fallthrough", + "if", + "range", + "type", + "continue", + "for", + "import", + "return", + "var", +} + +func sanitizeGoName(name string) string { + for _, k := range keywords { + if name == k { + return name + "_" + } + } + return name +} + +func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, error) { + obj := &Object{NamedType: types[typ.TypeName()]} + typeEntry, entryExists := cfg.Models[typ.TypeName()] + + for _, i := range typ.Interfaces { + obj.Satisfies = append(obj.Satisfies, i.Name) + } + + for _, field := range typ.Fields { + + var forceResolver bool + if entryExists { + if typeField, ok := typeEntry.Fields[field.Name]; ok { + forceResolver = typeField.Resolver + } + } + + var args []FieldArgument + for _, arg := range field.Args { + newArg := FieldArgument{ + GQLName: arg.Name.Name, + Type: types.getType(arg.Type), + Object: obj, + GoVarName: sanitizeGoName(arg.Name.Name), + } + + if !newArg.Type.IsInput && !newArg.Type.IsScalar { + return nil, errors.Errorf("%s cannot be used as argument of %s.%s. only input and scalar types are allowed", arg.Type, obj.GQLType, field.Name) + } + + if arg.Default != nil { + newArg.Default = arg.Default.Value(nil) + newArg.StripPtr() + } + args = append(args, newArg) + } + + obj.Fields = append(obj.Fields, Field{ + GQLName: field.Name, + Type: types.getType(field.Type), + Args: args, + Object: obj, + ForceResolver: forceResolver, + }) + } + + for name, typ := range cfg.schema.EntryPoints { + schemaObj := typ.(*schema.Object) + if schemaObj.TypeName() != obj.GQLType { + continue + } + + obj.Root = true + if name == "mutation" { + obj.DisableConcurrency = true + } + if name == "subscription" { + obj.Stream = true + } + } + return obj, nil +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/args.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/args.gotpl new file mode 100644 index 00000000..f53aceec --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/args.gotpl @@ -0,0 +1,30 @@ + {{- if . }}args := map[string]interface{}{} {{end}} + {{- range $i, $arg := . }} + var arg{{$i}} {{$arg.Signature }} + if tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok { + var err error + {{$arg.Unmarshal (print "arg" $i) "tmp" }} + if err != nil { + ec.Error(ctx, err) + {{- if $arg.Object.Stream }} + return nil + {{- else }} + return graphql.Null + {{- end }} + } + } {{ if $arg.Default }} else { + var tmp interface{} = {{ $arg.Default | dump }} + var err error + {{$arg.Unmarshal (print "arg" $i) "tmp" }} + if err != nil { + ec.Error(ctx, err) + {{- if $arg.Object.Stream }} + return nil + {{- else }} + return graphql.Null + {{- end }} + } + } + {{end }} + args[{{$arg.GQLName|quote}}] = arg{{$i}} + {{- end -}} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/data.go b/vendor/github.com/vektah/gqlgen/codegen/templates/data.go new file mode 100644 index 00000000..d6da4807 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/data.go @@ -0,0 +1,11 @@ +package templates + +var data = map[string]string{ + "args.gotpl": "\t{{- if . }}args := map[string]interface{}{} {{end}}\n\t{{- range $i, $arg := . }}\n\t\tvar arg{{$i}} {{$arg.Signature }}\n\t\tif tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok {\n\t\t\tvar err error\n\t\t\t{{$arg.Unmarshal (print \"arg\" $i) \"tmp\" }}\n\t\t\tif err != nil {\n\t\t\t\tec.Error(ctx, err)\n\t\t\t\t{{- if $arg.Object.Stream }}\n\t\t\t\t\treturn nil\n\t\t\t\t{{- else }}\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t{{- end }}\n\t\t\t}\n\t\t} {{ if $arg.Default }} else {\n\t\t\tvar tmp interface{} = {{ $arg.Default | dump }}\n\t\t\tvar err error\n\t\t\t{{$arg.Unmarshal (print \"arg\" $i) \"tmp\" }}\n\t\t\tif err != nil {\n\t\t\t\tec.Error(ctx, err)\n\t\t\t\t{{- if $arg.Object.Stream }}\n\t\t\t\t\treturn nil\n\t\t\t\t{{- else }}\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\t\t{{end }}\n\t\targs[{{$arg.GQLName|quote}}] = arg{{$i}}\n\t{{- end -}}\n", + "field.gotpl": "{{ $field := . }}\n{{ $object := $field.Object }}\n\n{{- if $object.Stream }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})\n\t\tresults, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\tif err != nil {\n\t\t\tec.Error(ctx, err)\n\t\t\treturn nil\n\t\t}\n\t\treturn func() graphql.Marshaler {\n\t\t\tres, ok := <-results\n\t\t\tif !ok {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tvar out graphql.OrderedMap\n\t\t\tout.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }())\n\t\t\treturn &out\n\t\t}\n\t}\n{{ else }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\n\t\t{{- if $field.IsConcurrent }}\n\t\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\t\tObject: {{$object.GQLType|quote}},\n\t\t\t\tArgs: {{if $field.Args }}args{{else}}nil{{end}},\n\t\t\t\tField: field,\n\t\t\t})\n\t\t\treturn graphql.Defer(func() (ret graphql.Marshaler) {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tuserErr := ec.Recover(ctx, r)\n\t\t\t\t\t\tec.Error(ctx, userErr)\n\t\t\t\t\t\tret = graphql.Null\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t{{ else }}\n\t\t\trctx := graphql.GetResolverContext(ctx)\n\t\t\trctx.Object = {{$object.GQLType|quote}}\n\t\t\trctx.Args = {{if $field.Args }}args{{else}}nil{{end}}\n\t\t\trctx.Field = field\n\t\t\trctx.PushField(field.Alias)\n\t\t\tdefer rctx.Pop()\n\t\t{{- end }}\n\n\t\t\t{{- if $field.IsResolver }}\n\t\t\t\tresTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {\n\t\t\t\t\treturn ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\t\t\t})\n\t\t\t\tif err != nil {\n\t\t\t\t\tec.Error(ctx, err)\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t}\n\t\t\t\tif resTmp == nil {\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t}\n\t\t\t\tres := resTmp.({{$field.Signature}})\n\t\t\t{{- else if $field.GoVarName }}\n\t\t\t\tres := obj.{{$field.GoVarName}}\n\t\t\t{{- else if $field.GoMethodName }}\n\t\t\t\t{{- if $field.NoErr }}\n\t\t\t\t\tres := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t{{- else }}\n\t\t\t\t\tres, err := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tec.Error(ctx, err)\n\t\t\t\t\t\treturn graphql.Null\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t\t{{ $field.WriteJson }}\n\t\t{{- if $field.IsConcurrent }}\n\t\t\t})\n\t\t{{- end }}\n\t}\n{{ end }}\n", + "generated.gotpl": "// Code generated by github.com/vektah/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface.\nfunc MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {\n\treturn &executableSchema{resolvers: resolvers}\n}\n\n// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.\nfunc NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema {\n\treturn MakeExecutableSchema(shortMapper{r: resolvers})\n}\n\ntype Resolvers interface {\n{{- range $object := .Objects -}}\n\t{{ range $field := $object.Fields -}}\n\t\t{{ $field.ResolverDeclaration }}\n\t{{ end }}\n{{- end }}\n}\n\ntype ResolverRoot interface {\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers -}}\n\t\t{{$object.GQLType}}() {{$object.GQLType}}Resolver\n\t{{ end }}\n{{- end }}\n}\n\n{{- range $object := .Objects -}}\n\t{{ if $object.HasResolvers }}\n\t\ttype {{$object.GQLType}}Resolver interface {\n\t\t{{ range $field := $object.Fields -}}\n\t\t\t{{ $field.ShortResolverDeclaration }}\n\t\t{{ end }}\n\t\t}\n\t{{- end }}\n{{- end }}\n\ntype shortMapper struct {\n\tr ResolverRoot\n}\n\n{{- range $object := .Objects -}}\n\t{{ range $field := $object.Fields -}}\n\t\t{{- if $field.IsResolver }}\n\t\t\tfunc (s shortMapper) {{ $field.ResolverDeclaration }} {\n\t\t\t\treturn s.r.{{$field.ShortInvocation}}\n\t\t\t}\n\t\t{{- end }}\n\t{{ end }}\n{{- end }}\n\ntype executableSchema struct {\n\tresolvers Resolvers\n}\n\nfunc (e *executableSchema) Schema() *schema.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, op *query.Operation) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.QueryRoot.GQLType}}(ctx, op.Selections)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"queries are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}\n\n\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\tdata := ec._{{.MutationRoot.GQLType}}(ctx, op.Selections)\n\t\t\tvar buf bytes.Buffer\n\t\t\tdata.MarshalGQL(&buf)\n\t\t\treturn buf.Bytes()\n\t\t})\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn graphql.ErrorResponse(ctx, \"mutations are not supported\")\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, op *query.Operation) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.Selections)\n\t\tif ec.Errors != nil {\n\t\t\treturn graphql.OneShot(&graphql.Response{Data: []byte(\"null\"), Errors: ec.Errors})\n\t\t}\n\n\t\tvar buf bytes.Buffer\n\t\treturn func() *graphql.Response {\n\t\t\tbuf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {\n\t\t\t\tbuf.Reset()\n\t\t\t\tdata := next()\n\n\t\t\t\tif data == nil {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tdata.MarshalGQL(&buf)\n\t\t\t\treturn buf.Bytes()\n\t\t\t})\n\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(graphql.ErrorResponse(ctx, \"subscriptions are not supported\"))\n\t{{- end }}\n}\n\ntype executionContext struct {\n\t*graphql.RequestContext\n\n\tresolvers Resolvers\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n\n\t{{- range $field := $object.Fields }}\n\t\t{{ template \"field.gotpl\" $field }}\n\t{{ end }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\tt := parsedSchema.Resolve(name)\n\tif t == nil {\n\t\treturn nil\n\t}\n\treturn introspection.WrapType(t)\n}\n\nvar parsedSchema = schema.MustParse({{.SchemaRaw|rawQuote}})\n", + "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\t\tvar asMap = v.(map[string]interface{})\n\t\t{{ range $field := .Fields}}\n\t\t\t{{- if $field.Default}}\n\t\t\t\tif _, present := asMap[{{$field.GQLName|quote}}] ; !present {\n\t\t\t\t\tasMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}\n\t\t\t\t}\n\t\t\t{{- end}}\n\t\t{{- end }}\n\n\t\tfor k, v := range asMap {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\tvar err error\n\t\t\t\t{{ $field.Unmarshal (print \"it.\" $field.GoVarName) \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", + "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel []query.Selection, obj *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch obj := (*obj).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\t\t{{- if $implementor.ValueReceiver }}\n\t\t\tcase {{$implementor.FullName}}:\n\t\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, &obj)\n\t\t{{- end}}\n\t\tcase *{{$implementor.FullName}}:\n\t\t\treturn ec._{{$implementor.GQLType}}(ctx, sel, obj)\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", obj))\n\t}\n}\n", + "models.gotpl": "// Code generated by github.com/vektah/gqlgen, DO NOT EDIT.\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\n{{ range $model := .Models }}\n\t{{- if .IsInterface }}\n\t\ttype {{.GoType}} interface {}\n\t{{- else }}\n\t\ttype {{.GoType}} struct {\n\t\t\t{{- range $field := .Fields }}\n\t\t\t\t{{- if $field.GoVarName }}\n\t\t\t\t\t{{ $field.GoVarName }} {{$field.Signature}} `json:\"{{$field.GQLName}}\"`\n\t\t\t\t{{- else }}\n\t\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t\t{{- end }}\n\t\t\t{{- end }}\n\t\t}\n\t{{- end }}\n{{- end}}\n\n{{ range $enum := .Enums }}\n\ttype {{.GoType}} string\n\tconst (\n\t{{ range $value := .Values -}}\n\t\t{{with .Description}} {{.|prefixLines \"// \"}} {{end}}\n\t\t{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}\n\t{{- end }}\n\t)\n\n\tfunc (e {{.GoType}}) IsValid() bool {\n\t\tswitch e {\n\t\tcase {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tfunc (e {{.GoType}}) String() string {\n\t\treturn string(e)\n\t}\n\n\tfunc (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {\n\t\tstr, ok := v.(string)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"enums must be strings\")\n\t\t}\n\n\t\t*e = {{.GoType}}(str)\n\t\tif !e.IsValid() {\n\t\t\treturn fmt.Errorf(\"%s is not a valid {{.GQLType}}\", str)\n\t\t}\n\t\treturn nil\n\t}\n\n\tfunc (e {{.GoType}}) MarshalGQL(w io.Writer) {\n\t\tfmt.Fprint(w, strconv.Quote(e.String()))\n\t}\n\n{{- end }}\n", + "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection) func() graphql.Marshaler {\n\tfields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables)\n\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\tObject: {{$object.GQLType|quote}},\n\t})\n\tif len(fields) != 1 {\n\t\tec.Errorf(ctx, \"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tswitch fields[0].Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\treturn ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0])\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(fields[0].Name))\n\t}\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables)\n\t{{if $object.Root}}\n\t\tctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{\n\t\t\tObject: {{$object.GQLType|quote}},\n\t\t})\n\t{{end}}\n\tout := graphql.NewOrderedMap(len(fields))\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\tout.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}})\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\n\treturn out\n}\n{{- end }}\n", +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/field.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/field.gotpl new file mode 100644 index 00000000..4279ad8e --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/field.gotpl @@ -0,0 +1,80 @@ +{{ $field := . }} +{{ $object := $field.Object }} + +{{- if $object.Stream }} + func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler { + {{- template "args.gotpl" $field.Args }} + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field}) + results, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }}) + if err != nil { + ec.Error(ctx, err) + return nil + } + return func() graphql.Marshaler { + res, ok := <-results + if !ok { + return nil + } + var out graphql.OrderedMap + out.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }()) + return &out + } + } +{{ else }} + func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler { + {{- template "args.gotpl" $field.Args }} + + {{- if $field.IsConcurrent }} + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: {{$object.GQLType|quote}}, + Args: {{if $field.Args }}args{{else}}nil{{end}}, + Field: field, + }) + return graphql.Defer(func() (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + userErr := ec.Recover(ctx, r) + ec.Error(ctx, userErr) + ret = graphql.Null + } + }() + {{ else }} + rctx := graphql.GetResolverContext(ctx) + rctx.Object = {{$object.GQLType|quote}} + rctx.Args = {{if $field.Args }}args{{else}}nil{{end}} + rctx.Field = field + rctx.PushField(field.Alias) + defer rctx.Pop() + {{- end }} + + {{- if $field.IsResolver }} + resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }}) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.({{$field.Signature}}) + {{- else if $field.GoVarName }} + res := obj.{{$field.GoVarName}} + {{- else if $field.GoMethodName }} + {{- if $field.NoErr }} + res := {{$field.GoMethodName}}({{ $field.CallArgs }}) + {{- else }} + res, err := {{$field.GoMethodName}}({{ $field.CallArgs }}) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + {{- end }} + {{- end }} + {{ $field.WriteJson }} + {{- if $field.IsConcurrent }} + }) + {{- end }} + } +{{ end }} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/generated.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/generated.gotpl new file mode 100644 index 00000000..cc1dc459 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/generated.gotpl @@ -0,0 +1,175 @@ +// Code generated by github.com/vektah/gqlgen, DO NOT EDIT. + +package {{ .PackageName }} + +import ( +{{- range $import := .Imports }} + {{- $import.Write }} +{{ end }} +) + +// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface. +func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema { + return &executableSchema{resolvers: resolvers} +} + +// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. +func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { + return MakeExecutableSchema(shortMapper{r: resolvers}) +} + +type Resolvers interface { +{{- range $object := .Objects -}} + {{ range $field := $object.Fields -}} + {{ $field.ResolverDeclaration }} + {{ end }} +{{- end }} +} + +type ResolverRoot interface { +{{- range $object := .Objects -}} + {{ if $object.HasResolvers -}} + {{$object.GQLType}}() {{$object.GQLType}}Resolver + {{ end }} +{{- end }} +} + +{{- range $object := .Objects -}} + {{ if $object.HasResolvers }} + type {{$object.GQLType}}Resolver interface { + {{ range $field := $object.Fields -}} + {{ $field.ShortResolverDeclaration }} + {{ end }} + } + {{- end }} +{{- end }} + +type shortMapper struct { + r ResolverRoot +} + +{{- range $object := .Objects -}} + {{ range $field := $object.Fields -}} + {{- if $field.IsResolver }} + func (s shortMapper) {{ $field.ResolverDeclaration }} { + return s.r.{{$field.ShortInvocation}} + } + {{- end }} + {{ end }} +{{- end }} + +type executableSchema struct { + resolvers Resolvers +} + +func (e *executableSchema) Schema() *schema.Schema { + return parsedSchema +} + +func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *graphql.Response { + {{- if .QueryRoot }} + ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + + buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { + data := ec._{{.QueryRoot.GQLType}}(ctx, op.Selections) + var buf bytes.Buffer + data.MarshalGQL(&buf) + return buf.Bytes() + }) + + return &graphql.Response{ + Data: buf, + Errors: ec.Errors, + } + {{- else }} + return graphql.ErrorResponse(ctx, "queries are not supported") + {{- end }} +} + +func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *graphql.Response { + {{- if .MutationRoot }} + ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + + buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { + data := ec._{{.MutationRoot.GQLType}}(ctx, op.Selections) + var buf bytes.Buffer + data.MarshalGQL(&buf) + return buf.Bytes() + }) + + return &graphql.Response{ + Data: buf, + Errors: ec.Errors, + } + {{- else }} + return graphql.ErrorResponse(ctx, "mutations are not supported") + {{- end }} +} + +func (e *executableSchema) Subscription(ctx context.Context, op *query.Operation) func() *graphql.Response { + {{- if .SubscriptionRoot }} + ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} + + next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.Selections) + if ec.Errors != nil { + return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors}) + } + + var buf bytes.Buffer + return func() *graphql.Response { + buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { + buf.Reset() + data := next() + + if data == nil { + return nil + } + data.MarshalGQL(&buf) + return buf.Bytes() + }) + + return &graphql.Response{ + Data: buf, + Errors: ec.Errors, + } + } + {{- else }} + return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported")) + {{- end }} +} + +type executionContext struct { + *graphql.RequestContext + + resolvers Resolvers +} + +{{- range $object := .Objects }} + {{ template "object.gotpl" $object }} + + {{- range $field := $object.Fields }} + {{ template "field.gotpl" $field }} + {{ end }} +{{- end}} + +{{- range $interface := .Interfaces }} + {{ template "interface.gotpl" $interface }} +{{- end }} + +{{- range $input := .Inputs }} + {{ template "input.gotpl" $input }} +{{- end }} + +func (ec *executionContext) introspectSchema() *introspection.Schema { + return introspection.WrapSchema(parsedSchema) +} + +func (ec *executionContext) introspectType(name string) *introspection.Type { + t := parsedSchema.Resolve(name) + if t == nil { + return nil + } + return introspection.WrapType(t) +} + +var parsedSchema = schema.MustParse({{.SchemaRaw|rawQuote}}) diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/input.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/input.gotpl new file mode 100644 index 00000000..6073daf4 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/input.gotpl @@ -0,0 +1,28 @@ + {{- if .IsMarshaled }} + func Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) { + var it {{.FullName}} + var asMap = v.(map[string]interface{}) + {{ range $field := .Fields}} + {{- if $field.Default}} + if _, present := asMap[{{$field.GQLName|quote}}] ; !present { + asMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }} + } + {{- end}} + {{- end }} + + for k, v := range asMap { + switch k { + {{- range $field := .Fields }} + case {{$field.GQLName|quote}}: + var err error + {{ $field.Unmarshal (print "it." $field.GoVarName) "v" }} + if err != nil { + return it, err + } + {{- end }} + } + } + + return it, nil + } + {{- end }} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/interface.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/interface.gotpl new file mode 100644 index 00000000..817d0abe --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/interface.gotpl @@ -0,0 +1,18 @@ +{{- $interface := . }} + +func (ec *executionContext) _{{$interface.GQLType}}(ctx context.Context, sel []query.Selection, obj *{{$interface.FullName}}) graphql.Marshaler { + switch obj := (*obj).(type) { + case nil: + return graphql.Null + {{- range $implementor := $interface.Implementors }} + {{- if $implementor.ValueReceiver }} + case {{$implementor.FullName}}: + return ec._{{$implementor.GQLType}}(ctx, sel, &obj) + {{- end}} + case *{{$implementor.FullName}}: + return ec._{{$implementor.GQLType}}(ctx, sel, obj) + {{- end }} + default: + panic(fmt.Errorf("unexpected type %T", obj)) + } +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/models.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/models.gotpl new file mode 100644 index 00000000..e66266a5 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/models.gotpl @@ -0,0 +1,65 @@ +// Code generated by github.com/vektah/gqlgen, DO NOT EDIT. + +package {{ .PackageName }} + +import ( +{{- range $import := .Imports }} + {{- $import.Write }} +{{ end }} +) + +{{ range $model := .Models }} + {{- if .IsInterface }} + type {{.GoType}} interface {} + {{- else }} + type {{.GoType}} struct { + {{- range $field := .Fields }} + {{- if $field.GoVarName }} + {{ $field.GoVarName }} {{$field.Signature}} `json:"{{$field.GQLName}}"` + {{- else }} + {{ $field.GoFKName }} {{$field.GoFKType}} + {{- end }} + {{- end }} + } + {{- end }} +{{- end}} + +{{ range $enum := .Enums }} + type {{.GoType}} string + const ( + {{ range $value := .Values -}} + {{with .Description}} {{.|prefixLines "// "}} {{end}} + {{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}} + {{- end }} + ) + + func (e {{.GoType}}) IsValid() bool { + switch e { + case {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}: + return true + } + return false + } + + func (e {{.GoType}}) String() string { + return string(e) + } + + func (e *{{.GoType}}) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = {{.GoType}}(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid {{.GQLType}}", str) + } + return nil + } + + func (e {{.GoType}}) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) + } + +{{- end }} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl b/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl new file mode 100644 index 00000000..b531d5fe --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/object.gotpl @@ -0,0 +1,52 @@ +{{ $object := . }} + +var {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}} + +// nolint: gocyclo, errcheck, gas, goconst +{{- if .Stream }} +func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection) func() graphql.Marshaler { + fields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables) + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: {{$object.GQLType|quote}}, + }) + if len(fields) != 1 { + ec.Errorf(ctx, "must subscribe to exactly one stream") + return nil + } + + switch fields[0].Name { + {{- range $field := $object.Fields }} + case "{{$field.GQLName}}": + return ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, fields[0]) + {{- end }} + default: + panic("unknown field " + strconv.Quote(fields[0].Name)) + } +} +{{- else }} +func (ec *executionContext) _{{$object.GQLType}}(ctx context.Context, sel []query.Selection{{if not $object.Root}}, obj *{{$object.FullName}} {{end}}) graphql.Marshaler { + fields := graphql.CollectFields(ec.Doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.Variables) + {{if $object.Root}} + ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ + Object: {{$object.GQLType|quote}}, + }) + {{end}} + out := graphql.NewOrderedMap(len(fields)) + for i, field := range fields { + out.Keys[i] = field.Alias + + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString({{$object.GQLType|quote}}) + {{- range $field := $object.Fields }} + case "{{$field.GQLName}}": + out.Values[i] = ec._{{$object.GQLType}}_{{$field.GQLName}}(ctx, field{{if not $object.Root}}, obj{{end}}) + {{- end }} + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} +{{- end }} diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go b/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go new file mode 100644 index 00000000..3d29b403 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go @@ -0,0 +1,139 @@ +//go:generate go run ./inliner/inliner.go + +package templates + +import ( + "bytes" + "fmt" + "sort" + "strconv" + "strings" + "text/template" + "unicode" +) + +func Run(name string, tpldata interface{}) (*bytes.Buffer, error) { + t := template.New("").Funcs(template.FuncMap{ + "ucFirst": ucFirst, + "lcFirst": lcFirst, + "quote": strconv.Quote, + "rawQuote": rawQuote, + "toCamel": ToCamel, + "dump": dump, + "prefixLines": prefixLines, + }) + + for filename, data := range data { + _, err := t.New(filename).Parse(data) + if err != nil { + panic(err) + } + } + + buf := &bytes.Buffer{} + err := t.Lookup(name).Execute(buf, tpldata) + if err != nil { + return nil, err + } + + return buf, nil +} + +func ucFirst(s string) string { + if s == "" { + return "" + } + r := []rune(s) + r[0] = unicode.ToUpper(r[0]) + return string(r) +} + +func lcFirst(s string) string { + if s == "" { + return "" + } + + r := []rune(s) + r[0] = unicode.ToLower(r[0]) + return string(r) +} + +func isDelimiter(c rune) bool { + return c == '-' || c == '_' || unicode.IsSpace(c) +} + +func ToCamel(s string) string { + buffer := make([]rune, 0, len(s)) + upper := true + lastWasUpper := false + + for _, c := range s { + if isDelimiter(c) { + upper = true + continue + } + if !lastWasUpper && unicode.IsUpper(c) { + upper = true + } + + if upper { + buffer = append(buffer, unicode.ToUpper(c)) + } else { + buffer = append(buffer, unicode.ToLower(c)) + } + upper = false + lastWasUpper = unicode.IsUpper(c) + } + + return string(buffer) +} + +func rawQuote(s string) string { + return "`" + strings.Replace(s, "`", "`+\"`\"+`", -1) + "`" +} + +func dump(val interface{}) string { + switch val := val.(type) { + case int: + return strconv.Itoa(val) + case float64: + return fmt.Sprintf("%f", val) + case string: + return strconv.Quote(val) + case bool: + return strconv.FormatBool(val) + case nil: + return "nil" + case []interface{}: + var parts []string + for _, part := range val { + parts = append(parts, dump(part)) + } + return "[]interface{}{" + strings.Join(parts, ",") + "}" + case map[string]interface{}: + buf := bytes.Buffer{} + buf.WriteString("map[string]interface{}{") + var keys []string + for key := range val { + keys = append(keys, key) + } + sort.Strings(keys) + + for _, key := range keys { + data := val[key] + + buf.WriteString(strconv.Quote(key)) + buf.WriteString(":") + buf.WriteString(dump(data)) + buf.WriteString(",") + } + buf.WriteString("}") + return buf.String() + default: + panic(fmt.Errorf("unsupported type %T", val)) + } +} + +func prefixLines(prefix, s string) string { + return prefix + strings.Replace(s, "\n", "\n"+prefix, -1) +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/type.go b/vendor/github.com/vektah/gqlgen/codegen/type.go new file mode 100644 index 00000000..7af24b3c --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/type.go @@ -0,0 +1,162 @@ +package codegen + +import ( + "strconv" + "strings" +) + +type NamedTypes map[string]*NamedType + +type NamedType struct { + Ref + IsScalar bool + IsInterface bool + IsInput bool + GQLType string // Name of the graphql type + Marshaler *Ref // If this type has an external marshaler this will be set +} + +type Ref struct { + GoType string // Name of the go type + Package string // the package the go type lives in + Import *Import // the resolved import with alias + IsUserDefined bool // does the type exist in the typemap +} + +type Type struct { + *NamedType + + Modifiers []string + CastType *Ref // the type to cast to when unmarshalling +} + +const ( + modList = "[]" + modPtr = "*" +) + +func (t Ref) FullName() string { + return t.PkgDot() + t.GoType +} + +func (t Ref) PkgDot() string { + if t.Import == nil || t.Import.Alias() == "" { + return "" + } + return t.Import.Alias() + "." +} + +func (t Type) Signature() string { + return strings.Join(t.Modifiers, "") + t.FullName() +} + +func (t Type) FullSignature() string { + pkg := "" + if t.Package != "" { + pkg = t.Package + "." + } + + return strings.Join(t.Modifiers, "") + pkg + t.GoType +} + +func (t Type) IsPtr() bool { + return len(t.Modifiers) > 0 && t.Modifiers[0] == modPtr +} + +func (t *Type) StripPtr() { + if !t.IsPtr() { + return + } + t.Modifiers = t.Modifiers[0 : len(t.Modifiers)-1] +} + +func (t Type) IsSlice() bool { + return len(t.Modifiers) > 0 && t.Modifiers[0] == modList || + len(t.Modifiers) > 1 && t.Modifiers[0] == modPtr && t.Modifiers[1] == modList +} + +func (t NamedType) IsMarshaled() bool { + return t.Marshaler != nil +} + +func (t Type) Unmarshal(result, raw string) string { + return t.unmarshal(result, raw, t.Modifiers, 1) +} + +func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) string { + switch { + case len(remainingMods) > 0 && remainingMods[0] == modPtr: + ptr := "ptr" + strconv.Itoa(depth) + return tpl(`var {{.ptr}} {{.mods}}{{.t.FullName}} + if {{.raw}} != nil { + {{.next}} + {{.result}} = &{{.ptr -}} + } + `, map[string]interface{}{ + "ptr": ptr, + "t": t, + "raw": raw, + "result": result, + "mods": strings.Join(remainingMods[1:], ""), + "next": t.unmarshal(ptr, raw, remainingMods[1:], depth+1), + }) + + case len(remainingMods) > 0 && remainingMods[0] == modList: + var rawIf = "rawIf" + strconv.Itoa(depth) + var index = "idx" + strconv.Itoa(depth) + + return tpl(`var {{.rawSlice}} []interface{} + if {{.raw}} != nil { + if tmp1, ok := {{.raw}}.([]interface{}); ok { + {{.rawSlice}} = tmp1 + } + } + {{.result}} = make({{.type}}, len({{.rawSlice}})) + for {{.index}} := range {{.rawSlice}} { + {{ .next -}} + }`, map[string]interface{}{ + "raw": raw, + "rawSlice": rawIf, + "index": index, + "result": result, + "type": strings.Join(remainingMods, "") + t.NamedType.FullName(), + "next": t.unmarshal(result+"["+index+"]", rawIf+"["+index+"]", remainingMods[1:], depth+1), + }) + } + + realResult := result + if t.CastType != nil { + result = "castTmp" + } + + return tpl(`{{- if .t.CastType }} + var castTmp {{.t.FullName}} + {{ end }} + {{- if eq .t.GoType "map[string]interface{}" }} + {{- .result }} = {{.raw}}.(map[string]interface{}) + {{- else if .t.Marshaler }} + {{- .result }}, err = {{ .t.Marshaler.PkgDot }}Unmarshal{{.t.Marshaler.GoType}}({{.raw}}) + {{- else -}} + err = (&{{.result}}).UnmarshalGQL({{.raw}}) + {{- end }} + {{- if .t.CastType }} + {{ .realResult }} = {{.t.CastType.FullName}}(castTmp) + {{- end }}`, map[string]interface{}{ + "realResult": realResult, + "result": result, + "raw": raw, + "t": t, + }) +} + +func (t Type) Marshal(val string) string { + if t.CastType != nil { + val = t.GoType + "(" + val + ")" + } + + if t.Marshaler != nil { + return "return " + t.Marshaler.PkgDot() + "Marshal" + t.Marshaler.GoType + "(" + val + ")" + } + + return "return " + val +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/type_build.go b/vendor/github.com/vektah/gqlgen/codegen/type_build.go new file mode 100644 index 00000000..ba2874b0 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/type_build.go @@ -0,0 +1,112 @@ +package codegen + +import ( + "fmt" + "go/types" + "strings" + + "github.com/vektah/gqlgen/neelance/common" + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +// namedTypeFromSchema objects for every graphql type, including scalars. There should only be one instance of Type for each thing +func (cfg *Config) buildNamedTypes() NamedTypes { + types := map[string]*NamedType{} + for _, schemaType := range cfg.schema.Types { + t := namedTypeFromSchema(schemaType) + + if userEntry, ok := cfg.Models[t.GQLType]; ok && userEntry.Model != "" { + t.IsUserDefined = true + t.Package, t.GoType = pkgAndType(userEntry.Model) + } else if t.IsScalar { + t.Package = "github.com/vektah/gqlgen/graphql" + t.GoType = "String" + } + + types[t.GQLType] = t + } + return types +} + +func (cfg *Config) bindTypes(imports *Imports, namedTypes NamedTypes, destDir string, prog *loader.Program) { + for _, t := range namedTypes { + if t.Package == "" { + continue + } + + def, _ := findGoType(prog, t.Package, "Marshal"+t.GoType) + switch def := def.(type) { + case *types.Func: + sig := def.Type().(*types.Signature) + cpy := t.Ref + t.Marshaler = &cpy + + t.Package, t.GoType = pkgAndType(sig.Params().At(0).Type().String()) + t.Import = imports.add(t.Package) + } + } +} + +// namedTypeFromSchema objects for every graphql type, including primitives. +// don't recurse into object fields or interfaces yet, lets make sure we have collected everything first. +func namedTypeFromSchema(schemaType schema.NamedType) *NamedType { + switch val := schemaType.(type) { + case *schema.Scalar, *schema.Enum: + return &NamedType{GQLType: val.TypeName(), IsScalar: true} + case *schema.Interface, *schema.Union: + return &NamedType{GQLType: val.TypeName(), IsInterface: true} + case *schema.InputObject: + return &NamedType{GQLType: val.TypeName(), IsInput: true} + default: + return &NamedType{GQLType: val.TypeName()} + } +} + +// take a string in the form github.com/package/blah.Type and split it into package and type +func pkgAndType(name string) (string, string) { + parts := strings.Split(name, ".") + if len(parts) == 1 { + return "", name + } + + return normalizeVendor(strings.Join(parts[:len(parts)-1], ".")), parts[len(parts)-1] +} + +func (n NamedTypes) getType(t common.Type) *Type { + var modifiers []string + usePtr := true + for { + if _, nonNull := t.(*common.NonNull); nonNull { + usePtr = false + } else if _, nonNull := t.(*common.List); nonNull { + usePtr = true + } else { + if usePtr { + modifiers = append(modifiers, modPtr) + } + usePtr = true + } + + switch val := t.(type) { + case *common.NonNull: + t = val.OfType + case *common.List: + modifiers = append(modifiers, modList) + t = val.OfType + case schema.NamedType: + t := &Type{ + NamedType: n[val.TypeName()], + Modifiers: modifiers, + } + + if t.IsInterface { + t.StripPtr() + } + + return t + default: + panic(fmt.Errorf("unknown type %T", t)) + } + } +} diff --git a/vendor/github.com/vektah/gqlgen/codegen/util.go b/vendor/github.com/vektah/gqlgen/codegen/util.go new file mode 100644 index 00000000..5ff41074 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/util.go @@ -0,0 +1,312 @@ +package codegen + +import ( + "fmt" + "go/types" + "regexp" + "strings" + + "github.com/pkg/errors" + "golang.org/x/tools/go/loader" +) + +func findGoType(prog *loader.Program, pkgName string, typeName string) (types.Object, error) { + if pkgName == "" { + return nil, nil + } + fullName := typeName + if pkgName != "" { + fullName = pkgName + "." + typeName + } + + pkgName, err := resolvePkg(pkgName) + if err != nil { + return nil, errors.Errorf("unable to resolve package for %s: %s\n", fullName, err.Error()) + } + + pkg := prog.Imported[pkgName] + if pkg == nil { + return nil, errors.Errorf("required package was not loaded: %s", fullName) + } + + for astNode, def := range pkg.Defs { + if astNode.Name != typeName || def.Parent() == nil || def.Parent() != pkg.Pkg.Scope() { + continue + } + + return def, nil + } + + return nil, errors.Errorf("unable to find type %s\n", fullName) +} + +func findGoNamedType(prog *loader.Program, pkgName string, typeName string) (*types.Named, error) { + def, err := findGoType(prog, pkgName, typeName) + if err != nil { + return nil, err + } + if def == nil { + return nil, nil + } + + namedType, ok := def.Type().(*types.Named) + if !ok { + return nil, errors.Errorf("expected %s to be a named type, instead found %T\n", typeName, def.Type()) + } + + return namedType, nil +} + +func findGoInterface(prog *loader.Program, pkgName string, typeName string) (*types.Interface, error) { + namedType, err := findGoNamedType(prog, pkgName, typeName) + if err != nil { + return nil, err + } + if namedType == nil { + return nil, nil + } + + underlying, ok := namedType.Underlying().(*types.Interface) + if !ok { + return nil, errors.Errorf("expected %s to be a named interface, instead found %s", typeName, namedType.String()) + } + + return underlying, nil +} + +func findMethod(typ *types.Named, name string) *types.Func { + for i := 0; i < typ.NumMethods(); i++ { + method := typ.Method(i) + if !method.Exported() { + continue + } + + if strings.EqualFold(method.Name(), name) { + return method + } + } + + if s, ok := typ.Underlying().(*types.Struct); ok { + for i := 0; i < s.NumFields(); i++ { + field := s.Field(i) + if !field.Anonymous() { + continue + } + + if named, ok := field.Type().(*types.Named); ok { + if f := findMethod(named, name); f != nil { + return f + } + } + } + } + + return nil +} + +func findField(typ *types.Struct, name string) *types.Var { + for i := 0; i < typ.NumFields(); i++ { + field := typ.Field(i) + if field.Anonymous() { + if named, ok := field.Type().(*types.Struct); ok { + if f := findField(named, name); f != nil { + return f + } + } + + if named, ok := field.Type().Underlying().(*types.Struct); ok { + if f := findField(named, name); f != nil { + return f + } + } + } + + if !field.Exported() { + continue + } + + if strings.EqualFold(field.Name(), name) { + return field + } + } + return nil +} + +type BindError struct { + object *Object + field *Field + typ types.Type + methodErr error + varErr error +} + +func (b BindError) Error() string { + return fmt.Sprintf( + "Unable to bind %s.%s to %s\n %s\n %s", + b.object.GQLType, + b.field.GQLName, + b.typ.String(), + b.methodErr.Error(), + b.varErr.Error(), + ) +} + +type BindErrors []BindError + +func (b BindErrors) Error() string { + var errs []string + for _, err := range b { + errs = append(errs, err.Error()) + } + return strings.Join(errs, "\n\n") +} + +func bindObject(t types.Type, object *Object, imports *Imports) BindErrors { + var errs BindErrors + for i := range object.Fields { + field := &object.Fields[i] + + // first try binding to a method + methodErr := bindMethod(imports, t, field) + if methodErr == nil { + continue + } + + // otherwise try binding to a var + varErr := bindVar(imports, t, field) + + if varErr != nil { + errs = append(errs, BindError{ + object: object, + typ: t, + field: field, + varErr: varErr, + methodErr: methodErr, + }) + } + } + return errs +} + +func bindMethod(imports *Imports, t types.Type, field *Field) error { + namedType, ok := t.(*types.Named) + if !ok { + return fmt.Errorf("not a named type") + } + + method := findMethod(namedType, field.GQLName) + if method == nil { + return fmt.Errorf("no method named %s", field.GQLName) + } + sig := method.Type().(*types.Signature) + + if sig.Results().Len() == 1 { + field.NoErr = true + } else if sig.Results().Len() != 2 { + return fmt.Errorf("method has wrong number of args") + } + newArgs, err := matchArgs(field, sig.Params()) + if err != nil { + return err + } + + result := sig.Results().At(0) + if err := validateTypeBinding(imports, field, result.Type()); err != nil { + return errors.Wrap(err, "method has wrong return type") + } + + // success, args and return type match. Bind to method + field.GoMethodName = "obj." + method.Name() + field.Args = newArgs + return nil +} + +func bindVar(imports *Imports, t types.Type, field *Field) error { + underlying, ok := t.Underlying().(*types.Struct) + if !ok { + return fmt.Errorf("not a struct") + } + + structField := findField(underlying, field.GQLName) + if structField == nil { + return fmt.Errorf("no field named %s", field.GQLName) + } + + if err := validateTypeBinding(imports, field, structField.Type()); err != nil { + return errors.Wrap(err, "field has wrong type") + } + + // success, bind to var + field.GoVarName = structField.Name() + return nil +} + +func matchArgs(field *Field, params *types.Tuple) ([]FieldArgument, error) { + var newArgs []FieldArgument + +nextArg: + for j := 0; j < params.Len(); j++ { + param := params.At(j) + for _, oldArg := range field.Args { + if strings.EqualFold(oldArg.GQLName, param.Name()) { + oldArg.Type.Modifiers = modifiersFromGoType(param.Type()) + newArgs = append(newArgs, oldArg) + continue nextArg + } + } + + // no matching arg found, abort + return nil, fmt.Errorf("arg %s not found on method", param.Name()) + } + return newArgs, nil +} + +func validateTypeBinding(imports *Imports, field *Field, goType types.Type) error { + gqlType := normalizeVendor(field.Type.FullSignature()) + goTypeStr := normalizeVendor(goType.String()) + + if goTypeStr == gqlType || "*"+goTypeStr == gqlType || goTypeStr == "*"+gqlType { + field.Type.Modifiers = modifiersFromGoType(goType) + return nil + } + + // deal with type aliases + underlyingStr := normalizeVendor(goType.Underlying().String()) + if underlyingStr == gqlType || "*"+underlyingStr == gqlType || underlyingStr == "*"+gqlType { + field.Type.Modifiers = modifiersFromGoType(goType) + pkg, typ := pkgAndType(goType.String()) + imp := imports.findByPath(pkg) + field.CastType = &Ref{GoType: typ, Import: imp} + return nil + } + + return fmt.Errorf("%s is not compatible with %s", gqlType, goTypeStr) +} + +func modifiersFromGoType(t types.Type) []string { + var modifiers []string + for { + switch val := t.(type) { + case *types.Pointer: + modifiers = append(modifiers, modPtr) + t = val.Elem() + case *types.Array: + modifiers = append(modifiers, modList) + t = val.Elem() + case *types.Slice: + modifiers = append(modifiers, modList) + t = val.Elem() + default: + return modifiers + } + } +} + +var modsRegex = regexp.MustCompile(`^(\*|\[\])*`) + +func normalizeVendor(pkg string) string { + modifiers := modsRegex.FindAllString(pkg, 1)[0] + pkg = strings.TrimPrefix(pkg, modifiers) + parts := strings.Split(pkg, "/vendor/") + return modifiers + parts[len(parts)-1] +} |