Skip to content

Commit

Permalink
Add map[string]interface{} escape hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Mar 19, 2018
1 parent 5abdba1 commit 49d9216
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand Down
2 changes: 1 addition & 1 deletion example/todo/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion test/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -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{}"
}

0 comments on commit 49d9216

Please sign in to comment.