diff --git a/codegen/type.go b/codegen/type.go index cd81c53da1..7d945ccce5 100644 --- a/codegen/type.go +++ b/codegen/type.go @@ -126,7 +126,9 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s return tpl(`{{- if .t.CastType }} var castTmp {{.t.FullName}} {{ end }} - {{- if .t.Marshaler }} + {{- 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}}) diff --git a/example/todo/generated.go b/example/todo/generated.go index aca76f1176..b54977fdc9 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -128,7 +128,7 @@ func (ec *executionContext) _MyMutation_updateTodo(field graphql.CollectedField) var arg1 map[string]interface{} if tmp, ok := field.Args["changes"]; ok { var err error - arg1, err = graphql.UnmarshalMap(tmp) + arg1 = tmp.(map[string]interface{}) if err != nil { ec.Error(err) return graphql.Null diff --git a/test/generated.go b/test/generated.go index 10be323217..6a67e478a4 100644 --- a/test/generated.go +++ b/test/generated.go @@ -25,6 +25,7 @@ type Resolvers interface { Query_nestedOutputs(ctx context.Context) ([][]OuterObject, error) Query_shapes(ctx context.Context) ([]Shape, error) Query_recursive(ctx context.Context, input *RecursiveInputSlice) (*bool, error) + Query_mapInput(ctx context.Context, input *map[string]interface{}) (*bool, error) } type executableSchema struct { @@ -186,6 +187,8 @@ func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler { out.Values[i] = ec._Query_shapes(field) case "recursive": out.Values[i] = ec._Query_recursive(field) + case "mapInput": + out.Values[i] = ec._Query_mapInput(field) case "__schema": out.Values[i] = ec._Query___schema(field) case "__type": @@ -338,6 +341,41 @@ func (ec *executionContext) _Query_recursive(field graphql.CollectedField) graph }) } +func (ec *executionContext) _Query_mapInput(field graphql.CollectedField) graphql.Marshaler { + var arg0 *map[string]interface{} + if tmp, ok := field.Args["input"]; ok { + var err error + var ptr1 map[string]interface{} + if tmp != nil { + ptr1 = tmp.(map[string]interface{}) + arg0 = &ptr1 + } + + if err != nil { + ec.Error(err) + return graphql.Null + } + } + return graphql.Defer(func() (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + userErr := ec.recover(r) + ec.Error(userErr) + ret = graphql.Null + } + }() + res, err := ec.resolvers.Query_mapInput(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___schema(field graphql.CollectedField) graphql.Marshaler { res := ec.introspectSchema() if res == nil { @@ -986,7 +1024,7 @@ func UnmarshalRecursiveInputSlice(v interface{}) (RecursiveInputSlice, error) { 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\ninterface Shape {\n area: Float\n}\n\ntype Circle implements Shape {\n radius: Float\n area: Float\n}\n\ntype Rectangle implements Shape {\n length: Float\n width: Float\n area: Float\n}\n\ninput RecursiveInputSlice {\n self: [RecursiveInputSlice!]\n}\n\nunion ShapeUnion = Circle | Rectangle\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n recursive(input: RecursiveInputSlice): Boolean\n}\n") +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\ninterface Shape {\n area: Float\n}\n\ntype Circle implements Shape {\n radius: Float\n area: Float\n}\n\ntype Rectangle implements Shape {\n length: Float\n width: Float\n area: Float\n}\n\ninput RecursiveInputSlice {\n self: [RecursiveInputSlice!]\n}\n\nunion ShapeUnion = Circle | Rectangle\n\ninput Changes {\n a: Int\n b: Int\n}\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n recursive(input: RecursiveInputSlice): Boolean\n mapInput(input: Changes): Boolean\n}\n") func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) diff --git a/test/schema.graphql b/test/schema.graphql index 3983c39f3e..b2df01bcda 100644 --- a/test/schema.graphql +++ b/test/schema.graphql @@ -35,9 +35,15 @@ input RecursiveInputSlice { union ShapeUnion = Circle | Rectangle +input Changes { + a: Int + b: Int +} + type Query { nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean nestedOutputs: [[OuterObject]] shapes: [Shape] recursive(input: RecursiveInputSlice): Boolean + mapInput(input: Changes): Boolean } diff --git a/test/types.json b/test/types.json index 2ee3d3baa1..b1d8223f56 100644 --- a/test/types.json +++ b/test/types.json @@ -3,5 +3,6 @@ "ShapeUnion": "github.com/vektah/gqlgen/test.ShapeUnion", "Circle": "github.com/vektah/gqlgen/test.Circle", "Rectangle": "github.com/vektah/gqlgen/test.Rectangle", - "RecursiveInputSlice": "github.com/vektah/gqlgen/test.RecursiveInputSlice" + "RecursiveInputSlice": "github.com/vektah/gqlgen/test.RecursiveInputSlice", + "Changes": "map[string]interface{}" }