diff --git a/codegen/models_build.go b/codegen/models_build.go index a3ebb12a634..ee1521719d6 100644 --- a/codegen/models_build.go +++ b/codegen/models_build.go @@ -49,8 +49,7 @@ func bindGenerated(types NamedTypes, object *Object) { field.GoVarName = "ID" } } else if object.Input { - field.GoFKName = ucFirst(field.GQLName) - field.GoFKType = types[field.GQLType].GoType + field.GoVarName = ucFirst(field.GQLName) } else { field.GoFKName = ucFirst(field.GQLName) + "ID" field.GoFKType = "int" // todo: use schema to determine type of id? diff --git a/codegen/templates/args.gotpl b/codegen/templates/args.gotpl index 79f3971cbca..4e809a4850c 100644 --- a/codegen/templates/args.gotpl +++ b/codegen/templates/args.gotpl @@ -12,7 +12,7 @@ {{- end }} } } {{ if $arg.Default }} else { - tmp := {{ $arg.Default | dump }} + var tmp interface{} = {{ $arg.Default | dump }} var err error {{$arg.Unmarshal (print "arg" $i) "tmp" }} if err != nil { diff --git a/codegen/templates/data.go b/codegen/templates/data.go index 81b5b8aeeac..a39e8130425 100644 --- a/codegen/templates/data.go +++ b/codegen/templates/data.go @@ -1,7 +1,7 @@ package templates var data = map[string]string{ - "args.gotpl": "\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(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\ttmp := {{ $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(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{{- end -}}\n", + "args.gotpl": "\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(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(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{{- end -}}\n", "field.gotpl": "{{ $field := . }}\n{{ $object := $field.Object }}\n\n{{- if $object.Stream }}\n\tfunc (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(field graphql.CollectedField) func() graphql.Marshaler {\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\tresults, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\tif err != nil {\n\t\t\tec.Error(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}}(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\treturn graphql.Defer(func() graphql.Marshaler {\n\t\t{{- end }}\n\n\t\t\t{{- 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(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{{- else }}\n\t\t\t\tres, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\t\t\tif err != nil {\n\t\t\t\t\tec.Error(err)\n\t\t\t\t\treturn graphql.Null\n\t\t\t\t}\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": "// This file was 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\nfunc MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {\n\treturn &executableSchema{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 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, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\tdata := ec._{{.QueryRoot.GQLType}}(op.Selections)\n\t\tvar buf bytes.Buffer\n\t\tdata.MarshalGQL(&buf)\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf.Bytes(),\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn &graphql.Response{Errors: []*errors.QueryError{ {Message: \"queries are not supported\"} }}\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\tdata := ec._{{.MutationRoot.GQLType}}(op.Selections)\n\t\tvar buf bytes.Buffer\n\t\tdata.MarshalGQL(&buf)\n\n\t\treturn &graphql.Response{\n\t\t\tData: buf.Bytes(),\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn &graphql.Response{Errors: []*errors.QueryError{ {Message: \"mutations are not supported\"} }}\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) func() *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\tnext := ec._{{.SubscriptionRoot.GQLType}}(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.Reset()\n\t\t\tdata := next()\n\t\t\tif data == nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tdata.MarshalGQL(&buf)\n\n\t\t\terrs := ec.Errors\n\t\t\tec.Errors = nil\n\t\t\treturn &graphql.Response{\n\t\t\t\tData: buf.Bytes(),\n\t\t\t\tErrors: errs,\n\t\t\t}\n\t\t}\n\t{{- else }}\n\t\treturn graphql.OneShot(&graphql.Response{Errors: []*errors.QueryError{ {Message: \"subscriptions are not supported\"} }})\n\t{{- end }}\n}\n\ntype executionContext struct {\n\terrors.Builder\n\tresolvers Resolvers\n\tvariables map[string]interface{}\n\tdoc *query.Document\n\tctx context.Context\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\nvar parsedSchema = schema.MustParse({{.SchemaRaw|quote}})\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", "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\n\t\tfor k, v := range v.(map[string]interface{}) {\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", diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 2d37a488246..8e16de6bab5 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -124,7 +124,7 @@ func (ec *executionContext) _Query_search(field graphql.CollectedField) graphql. return graphql.Null } } else { - tmp := map[string]interface{}{"location": "37,144"} + var tmp interface{} = map[string]interface{}{"location": "37,144"} var err error arg0, err = UnmarshalSearchArgs(tmp) diff --git a/example/starwars/generated.go b/example/starwars/generated.go index 1df185613f7..94a352a4683 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -353,7 +353,7 @@ func (ec *executionContext) _Human_height(field graphql.CollectedField, obj *Hum return graphql.Null } } else { - tmp := "METER" + var tmp interface{} = "METER" var err error arg0, err = graphql.UnmarshalString(tmp) @@ -590,7 +590,7 @@ func (ec *executionContext) _Query_hero(field graphql.CollectedField) graphql.Ma return graphql.Null } } else { - tmp := "NEWHOPE" + var tmp interface{} = "NEWHOPE" var err error arg0, err = graphql.UnmarshalString(tmp) @@ -884,7 +884,7 @@ func (ec *executionContext) _Starship_length(field graphql.CollectedField, obj * return graphql.Null } } else { - tmp := "METER" + var tmp interface{} = "METER" var err error arg0, err = graphql.UnmarshalString(tmp) diff --git a/test/generated.go b/test/generated.go new file mode 100644 index 00000000000..afdbfd65a62 --- /dev/null +++ b/test/generated.go @@ -0,0 +1,791 @@ +// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT + +package test + +import ( + "bytes" + context "context" + strconv "strconv" + + graphql "github.com/vektah/gqlgen/graphql" + errors "github.com/vektah/gqlgen/neelance/errors" + introspection "github.com/vektah/gqlgen/neelance/introspection" + query "github.com/vektah/gqlgen/neelance/query" + schema "github.com/vektah/gqlgen/neelance/schema" +) + +func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema { + return &executableSchema{resolvers} +} + +type Resolvers interface { + OuterObject_inner(ctx context.Context, obj *OuterObject) (InnerObject, error) + Query_nestedInputs(ctx context.Context, input [][]OuterInput) (*bool, error) + Query_nestedOutputs(ctx context.Context) ([][]OuterObject, error) +} + +type executableSchema struct { + resolvers Resolvers +} + +func (e *executableSchema) Schema() *schema.Schema { + return parsedSchema +} + +func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response { + ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx} + + data := ec._Query(op.Selections) + var buf bytes.Buffer + data.MarshalGQL(&buf) + + return &graphql.Response{ + Data: buf.Bytes(), + Errors: ec.Errors, + } +} + +func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response { + return &graphql.Response{Errors: []*errors.QueryError{{Message: "mutations are not supported"}}} +} + +func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) func() *graphql.Response { + return graphql.OneShot(&graphql.Response{Errors: []*errors.QueryError{{Message: "subscriptions are not supported"}}}) +} + +type executionContext struct { + errors.Builder + resolvers Resolvers + variables map[string]interface{} + doc *query.Document + ctx context.Context +} + +var innerObjectImplementors = []string{"InnerObject"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) _InnerObject(sel []query.Selection, obj *InnerObject) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, innerObjectImplementors, ec.variables) + 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("InnerObject") + case "id": + out.Values[i] = ec._InnerObject_id(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) _InnerObject_id(field graphql.CollectedField, obj *InnerObject) graphql.Marshaler { + res := obj.ID + return graphql.MarshalInt(res) +} + +var outerObjectImplementors = []string{"OuterObject"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) _OuterObject(sel []query.Selection, obj *OuterObject) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, outerObjectImplementors, ec.variables) + 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("OuterObject") + case "inner": + out.Values[i] = ec._OuterObject_inner(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) _OuterObject_inner(field graphql.CollectedField, obj *OuterObject) graphql.Marshaler { + return graphql.Defer(func() graphql.Marshaler { + res, err := ec.resolvers.OuterObject_inner(ec.ctx, obj) + if err != nil { + ec.Error(err) + return graphql.Null + } + return ec._InnerObject(field.Selections, &res) + }) +} + +var queryImplementors = []string{"Query"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, queryImplementors, ec.variables) + 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("Query") + case "nestedInputs": + out.Values[i] = ec._Query_nestedInputs(field) + case "nestedOutputs": + out.Values[i] = ec._Query_nestedOutputs(field) + case "__schema": + out.Values[i] = ec._Query___schema(field) + case "__type": + out.Values[i] = ec._Query___type(field) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) _Query_nestedInputs(field graphql.CollectedField) graphql.Marshaler { + var arg0 [][]OuterInput + if tmp, ok := field.Args["input"]; ok { + var err error + rawIf1 := tmp.([]interface{}) + arg0 = make([][]OuterInput, len(rawIf1)) + for idx1 := range rawIf1 { + rawIf2 := rawIf1[idx1].([]interface{}) + arg0[idx1] = make([]OuterInput, len(rawIf2)) + for idx2 := range rawIf2 { + + arg0[idx1][idx2], err = UnmarshalOuterInput(rawIf2[idx2]) + } + } + if err != nil { + ec.Error(err) + return graphql.Null + } + } else { + var tmp interface{} = []interface{}{[]interface{}{map[string]interface{}{"inner": map[string]interface{}{"id": 1}}}} + var err error + rawIf1 := tmp.([]interface{}) + arg0 = make([][]OuterInput, len(rawIf1)) + for idx1 := range rawIf1 { + rawIf2 := rawIf1[idx1].([]interface{}) + arg0[idx1] = make([]OuterInput, len(rawIf2)) + for idx2 := range rawIf2 { + + arg0[idx1][idx2], err = UnmarshalOuterInput(rawIf2[idx2]) + } + } + if err != nil { + ec.Error(err) + return graphql.Null + } + } + + return graphql.Defer(func() graphql.Marshaler { + res, err := ec.resolvers.Query_nestedInputs(ec.ctx, arg0) + if err != nil { + ec.Error(err) + return graphql.Null + } + if res == nil { + return graphql.Null + } + return graphql.MarshalBoolean(*res) + }) +} + +func (ec *executionContext) _Query_nestedOutputs(field graphql.CollectedField) graphql.Marshaler { + return graphql.Defer(func() graphql.Marshaler { + res, err := ec.resolvers.Query_nestedOutputs(ec.ctx) + if err != nil { + ec.Error(err) + return graphql.Null + } + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + arr2 := graphql.Array{} + for idx2 := range res[idx1] { + arr2 = append(arr2, func() graphql.Marshaler { return ec._OuterObject(field.Selections, &res[idx1][idx2]) }()) + } + return arr2 + }()) + } + return arr1 + }) +} + +func (ec *executionContext) _Query___schema(field graphql.CollectedField) graphql.Marshaler { + res := ec.introspectSchema() + if res == nil { + return graphql.Null + } + return ec.___Schema(field.Selections, res) +} + +func (ec *executionContext) _Query___type(field graphql.CollectedField) graphql.Marshaler { + var arg0 string + if tmp, ok := field.Args["name"]; ok { + var err error + + arg0, err = graphql.UnmarshalString(tmp) + if err != nil { + ec.Error(err) + return graphql.Null + } + } + res := ec.introspectType(arg0) + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +var __DirectiveImplementors = []string{"__Directive"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___Directive(sel []query.Selection, obj *introspection.Directive) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __DirectiveImplementors, ec.variables) + 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("__Directive") + case "name": + out.Values[i] = ec.___Directive_name(field, obj) + case "description": + out.Values[i] = ec.___Directive_description(field, obj) + case "locations": + out.Values[i] = ec.___Directive_locations(field, obj) + case "args": + out.Values[i] = ec.___Directive_args(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___Directive_name(field graphql.CollectedField, obj *introspection.Directive) graphql.Marshaler { + res := obj.Name() + return graphql.MarshalString(res) +} + +func (ec *executionContext) ___Directive_description(field graphql.CollectedField, obj *introspection.Directive) graphql.Marshaler { + res := obj.Description() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___Directive_locations(field graphql.CollectedField, obj *introspection.Directive) graphql.Marshaler { + res := obj.Locations() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { return graphql.MarshalString(res[idx1]) }()) + } + return arr1 +} + +func (ec *executionContext) ___Directive_args(field graphql.CollectedField, obj *introspection.Directive) graphql.Marshaler { + res := obj.Args() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___InputValue(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +var __EnumValueImplementors = []string{"__EnumValue"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___EnumValue(sel []query.Selection, obj *introspection.EnumValue) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __EnumValueImplementors, ec.variables) + 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("__EnumValue") + case "name": + out.Values[i] = ec.___EnumValue_name(field, obj) + case "description": + out.Values[i] = ec.___EnumValue_description(field, obj) + case "isDeprecated": + out.Values[i] = ec.___EnumValue_isDeprecated(field, obj) + case "deprecationReason": + out.Values[i] = ec.___EnumValue_deprecationReason(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___EnumValue_name(field graphql.CollectedField, obj *introspection.EnumValue) graphql.Marshaler { + res := obj.Name() + return graphql.MarshalString(res) +} + +func (ec *executionContext) ___EnumValue_description(field graphql.CollectedField, obj *introspection.EnumValue) graphql.Marshaler { + res := obj.Description() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___EnumValue_isDeprecated(field graphql.CollectedField, obj *introspection.EnumValue) graphql.Marshaler { + res := obj.IsDeprecated() + return graphql.MarshalBoolean(res) +} + +func (ec *executionContext) ___EnumValue_deprecationReason(field graphql.CollectedField, obj *introspection.EnumValue) graphql.Marshaler { + res := obj.DeprecationReason() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +var __FieldImplementors = []string{"__Field"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___Field(sel []query.Selection, obj *introspection.Field) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __FieldImplementors, ec.variables) + 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("__Field") + case "name": + out.Values[i] = ec.___Field_name(field, obj) + case "description": + out.Values[i] = ec.___Field_description(field, obj) + case "args": + out.Values[i] = ec.___Field_args(field, obj) + case "type": + out.Values[i] = ec.___Field_type(field, obj) + case "isDeprecated": + out.Values[i] = ec.___Field_isDeprecated(field, obj) + case "deprecationReason": + out.Values[i] = ec.___Field_deprecationReason(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___Field_name(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.Name() + return graphql.MarshalString(res) +} + +func (ec *executionContext) ___Field_description(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.Description() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___Field_args(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.Args() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___InputValue(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Field_type(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.Type() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func (ec *executionContext) ___Field_isDeprecated(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.IsDeprecated() + return graphql.MarshalBoolean(res) +} + +func (ec *executionContext) ___Field_deprecationReason(field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { + res := obj.DeprecationReason() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +var __InputValueImplementors = []string{"__InputValue"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___InputValue(sel []query.Selection, obj *introspection.InputValue) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __InputValueImplementors, ec.variables) + 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("__InputValue") + case "name": + out.Values[i] = ec.___InputValue_name(field, obj) + case "description": + out.Values[i] = ec.___InputValue_description(field, obj) + case "type": + out.Values[i] = ec.___InputValue_type(field, obj) + case "defaultValue": + out.Values[i] = ec.___InputValue_defaultValue(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___InputValue_name(field graphql.CollectedField, obj *introspection.InputValue) graphql.Marshaler { + res := obj.Name() + return graphql.MarshalString(res) +} + +func (ec *executionContext) ___InputValue_description(field graphql.CollectedField, obj *introspection.InputValue) graphql.Marshaler { + res := obj.Description() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___InputValue_type(field graphql.CollectedField, obj *introspection.InputValue) graphql.Marshaler { + res := obj.Type() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func (ec *executionContext) ___InputValue_defaultValue(field graphql.CollectedField, obj *introspection.InputValue) graphql.Marshaler { + res := obj.DefaultValue() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +var __SchemaImplementors = []string{"__Schema"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___Schema(sel []query.Selection, obj *introspection.Schema) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __SchemaImplementors, ec.variables) + 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("__Schema") + case "types": + out.Values[i] = ec.___Schema_types(field, obj) + case "queryType": + out.Values[i] = ec.___Schema_queryType(field, obj) + case "mutationType": + out.Values[i] = ec.___Schema_mutationType(field, obj) + case "subscriptionType": + out.Values[i] = ec.___Schema_subscriptionType(field, obj) + case "directives": + out.Values[i] = ec.___Schema_directives(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___Schema_types(field graphql.CollectedField, obj *introspection.Schema) graphql.Marshaler { + res := obj.Types() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Schema_queryType(field graphql.CollectedField, obj *introspection.Schema) graphql.Marshaler { + res := obj.QueryType() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func (ec *executionContext) ___Schema_mutationType(field graphql.CollectedField, obj *introspection.Schema) graphql.Marshaler { + res := obj.MutationType() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func (ec *executionContext) ___Schema_subscriptionType(field graphql.CollectedField, obj *introspection.Schema) graphql.Marshaler { + res := obj.SubscriptionType() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func (ec *executionContext) ___Schema_directives(field graphql.CollectedField, obj *introspection.Schema) graphql.Marshaler { + res := obj.Directives() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___Directive(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +var __TypeImplementors = []string{"__Type"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) ___Type(sel []query.Selection, obj *introspection.Type) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, __TypeImplementors, ec.variables) + 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("__Type") + case "kind": + out.Values[i] = ec.___Type_kind(field, obj) + case "name": + out.Values[i] = ec.___Type_name(field, obj) + case "description": + out.Values[i] = ec.___Type_description(field, obj) + case "fields": + out.Values[i] = ec.___Type_fields(field, obj) + case "interfaces": + out.Values[i] = ec.___Type_interfaces(field, obj) + case "possibleTypes": + out.Values[i] = ec.___Type_possibleTypes(field, obj) + case "enumValues": + out.Values[i] = ec.___Type_enumValues(field, obj) + case "inputFields": + out.Values[i] = ec.___Type_inputFields(field, obj) + case "ofType": + out.Values[i] = ec.___Type_ofType(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) ___Type_kind(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.Kind() + return graphql.MarshalString(res) +} + +func (ec *executionContext) ___Type_name(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.Name() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___Type_description(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.Description() + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) ___Type_fields(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + var arg0 bool + if tmp, ok := field.Args["includeDeprecated"]; ok { + var err error + + arg0, err = graphql.UnmarshalBoolean(tmp) + if err != nil { + ec.Error(err) + return graphql.Null + } + } + res := obj.Fields(arg0) + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___Field(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Type_interfaces(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.Interfaces() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Type_possibleTypes(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.PossibleTypes() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Type_enumValues(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + var arg0 bool + if tmp, ok := field.Args["includeDeprecated"]; ok { + var err error + + arg0, err = graphql.UnmarshalBoolean(tmp) + if err != nil { + ec.Error(err) + return graphql.Null + } + } + res := obj.EnumValues(arg0) + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___EnumValue(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Type_inputFields(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.InputFields() + arr1 := graphql.Array{} + for idx1 := range res { + arr1 = append(arr1, func() graphql.Marshaler { + if res[idx1] == nil { + return graphql.Null + } + return ec.___InputValue(field.Selections, res[idx1]) + }()) + } + return arr1 +} + +func (ec *executionContext) ___Type_ofType(field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + res := obj.OfType() + if res == nil { + return graphql.Null + } + return ec.___Type(field.Selections, res) +} + +func UnmarshalInnerInput(v interface{}) (InnerInput, error) { + var it InnerInput + + for k, v := range v.(map[string]interface{}) { + switch k { + case "id": + var err error + + it.ID, err = graphql.UnmarshalInt(v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func UnmarshalOuterInput(v interface{}) (OuterInput, error) { + var it OuterInput + + for k, v := range v.(map[string]interface{}) { + switch k { + case "inner": + var err error + + it.Inner, err = UnmarshalInnerInput(v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +var parsedSchema = schema.MustParse("input InnerInput {\n id:Int!\n}\n\ninput OuterInput {\n inner: InnerInput!\n}\n\ntype OuterObject {\n inner: InnerObject!\n}\n\ntype InnerObject {\n id: Int!\n}\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n}\n\nschema {\n query :Query\n}\n") + +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) +} diff --git a/test/models_gen.go b/test/models_gen.go new file mode 100644 index 00000000000..202227c0401 --- /dev/null +++ b/test/models_gen.go @@ -0,0 +1,16 @@ +// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT + +package test + +type InnerInput struct { + ID int +} +type InnerObject struct { + ID int +} +type OuterInput struct { + Inner InnerInput +} +type OuterObject struct { + InnerID int +} diff --git a/test/resolvers_test.go b/test/resolvers_test.go new file mode 100644 index 00000000000..54b5f7588c9 --- /dev/null +++ b/test/resolvers_test.go @@ -0,0 +1,7 @@ +//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go + +package test + +import "testing" + +func TestCompiles(t *testing.T) {} diff --git a/test/schema.graphql b/test/schema.graphql new file mode 100644 index 00000000000..1caa3760174 --- /dev/null +++ b/test/schema.graphql @@ -0,0 +1,24 @@ +input InnerInput { + id:Int! +} + +input OuterInput { + inner: InnerInput! +} + +type OuterObject { + inner: InnerObject! +} + +type InnerObject { + id: Int! +} + +type Query { + nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean + nestedOutputs: [[OuterObject]] +} + +schema { + query :Query +}