Skip to content

Commit

Permalink
Merge pull request #55 from vektah/fix-input-ptr-unpacking
Browse files Browse the repository at this point in the history
Fix ptr unpacking in input fields
  • Loading branch information
vektah authored Mar 18, 2018
2 parents 15b3af2 + 10541f1 commit f3a70da
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
3 changes: 2 additions & 1 deletion codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
switch {
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
ptr := "ptr" + strconv.Itoa(depth)
return tpl(`var {{.ptr}} {{.t.FullName}}
return tpl(`var {{.ptr}} {{.mods}}{{.t.FullName}}
{{.next}}
{{.result}} = &{{.ptr -}}
`, map[string]interface{}{
"ptr": ptr,
"t": t,
"result": result,
"mods": strings.Join(remainingMods[1:], ""),
"next": t.unmarshal(ptr, raw, remainingMods[1:], depth+1),
})

Expand Down
64 changes: 63 additions & 1 deletion test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Resolvers interface {
Query_nestedInputs(ctx context.Context, input [][]OuterInput) (*bool, error)
Query_nestedOutputs(ctx context.Context) ([][]OuterObject, error)
Query_shapes(ctx context.Context) ([]Shape, error)
Query_recursive(ctx context.Context, input *RecursiveInputSlice) (*bool, error)
}

type executableSchema struct {
Expand Down Expand Up @@ -183,6 +184,8 @@ func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler {
out.Values[i] = ec._Query_nestedOutputs(field)
case "shapes":
out.Values[i] = ec._Query_shapes(field)
case "recursive":
out.Values[i] = ec._Query_recursive(field)
case "__schema":
out.Values[i] = ec._Query___schema(field)
case "__type":
Expand Down Expand Up @@ -302,6 +305,39 @@ func (ec *executionContext) _Query_shapes(field graphql.CollectedField) graphql.
})
}

func (ec *executionContext) _Query_recursive(field graphql.CollectedField) graphql.Marshaler {
var arg0 *RecursiveInputSlice
if tmp, ok := field.Args["input"]; ok {
var err error
var ptr1 RecursiveInputSlice

ptr1, err = UnmarshalRecursiveInputSlice(tmp)
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_recursive(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 @@ -925,7 +961,33 @@ func UnmarshalOuterInput(v interface{}) (OuterInput, 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\nunion ShapeUnion = Circle | Rectangle\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n}\n")
func UnmarshalRecursiveInputSlice(v interface{}) (RecursiveInputSlice, error) {
var it RecursiveInputSlice

for k, v := range v.(map[string]interface{}) {
switch k {
case "self":
var err error
var ptr1 []*RecursiveInputSlice
rawIf2 := v.([]interface{})
ptr1 = make([]*RecursiveInputSlice, len(rawIf2))
for idx2 := range rawIf2 {
var ptr3 RecursiveInputSlice

ptr3, err = UnmarshalRecursiveInputSlice(rawIf2[idx2])
ptr1[idx2] = &ptr3
}
it.Self = &ptr1
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\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")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
Expand Down
4 changes: 4 additions & 0 deletions test/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ type Rectangle struct {
func (r *Rectangle) Area() float64 {
return r.Length * r.Width
}

type RecursiveInputSlice struct {
Self *[]*RecursiveInputSlice
}
5 changes: 5 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ type Rectangle implements Shape {
area: Float
}

input RecursiveInputSlice {
self: [RecursiveInputSlice!]
}

union ShapeUnion = Circle | Rectangle

type Query {
nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean
nestedOutputs: [[OuterObject]]
shapes: [Shape]
recursive(input: RecursiveInputSlice): Boolean
}
3 changes: 2 additions & 1 deletion test/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"Shape": "github.com/vektah/gqlgen/test.Shape",
"ShapeUnion": "github.com/vektah/gqlgen/test.ShapeUnion",
"Circle": "github.com/vektah/gqlgen/test.Circle",
"Rectangle": "github.com/vektah/gqlgen/test.Rectangle"
"Rectangle": "github.com/vektah/gqlgen/test.Rectangle",
"RecursiveInputSlice": "github.com/vektah/gqlgen/test.RecursiveInputSlice"
}

0 comments on commit f3a70da

Please sign in to comment.