Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Mar 17, 2018
1 parent 8ef253c commit dc89840
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 deletions.
122 changes: 121 additions & 1 deletion test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package test
import (
"bytes"
context "context"
fmt "fmt"
strconv "strconv"

graphql "github.com/vektah/gqlgen/graphql"
Expand All @@ -22,6 +23,7 @@ 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)
Query_shapes(ctx context.Context) ([]Shape, error)
}

type executableSchema struct {
Expand Down Expand Up @@ -62,6 +64,40 @@ type executionContext struct {
recover graphql.RecoverFunc
}

var circleImplementors = []string{"Circle", "Shape"}

// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _Circle(sel []query.Selection, obj *Circle) graphql.Marshaler {
fields := graphql.CollectFields(ec.doc, sel, circleImplementors, 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("Circle")
case "radius":
out.Values[i] = ec._Circle_radius(field, obj)
case "area":
out.Values[i] = ec._Circle_area(field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}

return out
}

func (ec *executionContext) _Circle_radius(field graphql.CollectedField, obj *Circle) graphql.Marshaler {
res := obj.Radius
return graphql.MarshalFloat(res)
}

func (ec *executionContext) _Circle_area(field graphql.CollectedField, obj *Circle) graphql.Marshaler {
res := obj.Area()
return graphql.MarshalFloat(res)
}

var innerObjectImplementors = []string{"InnerObject"}

// nolint: gocyclo, errcheck, gas, goconst
Expand Down Expand Up @@ -145,6 +181,8 @@ func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler {
out.Values[i] = ec._Query_nestedInputs(field)
case "nestedOutputs":
out.Values[i] = ec._Query_nestedOutputs(field)
case "shapes":
out.Values[i] = ec._Query_shapes(field)
case "__schema":
out.Values[i] = ec._Query___schema(field)
case "__type":
Expand Down Expand Up @@ -242,6 +280,28 @@ func (ec *executionContext) _Query_nestedOutputs(field graphql.CollectedField) g
})
}

func (ec *executionContext) _Query_shapes(field graphql.CollectedField) graphql.Marshaler {
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_shapes(ec.ctx)
if err != nil {
ec.Error(err)
return graphql.Null
}
arr1 := graphql.Array{}
for idx1 := range res {
arr1 = append(arr1, func() graphql.Marshaler { return ec._Shape(field.Selections, &res[idx1]) }())
}
return arr1
})
}

func (ec *executionContext) _Query___schema(field graphql.CollectedField) graphql.Marshaler {
res := ec.introspectSchema()
if res == nil {
Expand All @@ -268,6 +328,47 @@ func (ec *executionContext) _Query___type(field graphql.CollectedField) graphql.
return ec.___Type(field.Selections, res)
}

var rectangleImplementors = []string{"Rectangle", "Shape"}

// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _Rectangle(sel []query.Selection, obj *Rectangle) graphql.Marshaler {
fields := graphql.CollectFields(ec.doc, sel, rectangleImplementors, 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("Rectangle")
case "length":
out.Values[i] = ec._Rectangle_length(field, obj)
case "width":
out.Values[i] = ec._Rectangle_width(field, obj)
case "area":
out.Values[i] = ec._Rectangle_area(field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}

return out
}

func (ec *executionContext) _Rectangle_length(field graphql.CollectedField, obj *Rectangle) graphql.Marshaler {
res := obj.Length
return graphql.MarshalFloat(res)
}

func (ec *executionContext) _Rectangle_width(field graphql.CollectedField, obj *Rectangle) graphql.Marshaler {
res := obj.Width
return graphql.MarshalFloat(res)
}

func (ec *executionContext) _Rectangle_area(field graphql.CollectedField, obj *Rectangle) graphql.Marshaler {
res := obj.Area()
return graphql.MarshalFloat(res)
}

var __DirectiveImplementors = []string{"__Directive"}

// nolint: gocyclo, errcheck, gas, goconst
Expand Down Expand Up @@ -762,6 +863,25 @@ func (ec *executionContext) ___Type_ofType(field graphql.CollectedField, obj *in
return ec.___Type(field.Selections, res)
}

func (ec *executionContext) _Shape(sel []query.Selection, obj *Shape) graphql.Marshaler {
switch obj := (*obj).(type) {
case nil:
return graphql.Null
case Circle:
return ec._Circle(sel, &obj)

case *Circle:
return ec._Circle(sel, obj)
case Rectangle:
return ec._Rectangle(sel, &obj)

case *Rectangle:
return ec._Rectangle(sel, obj)
default:
panic(fmt.Errorf("unexpected type %T", obj))
}
}

func UnmarshalInnerInput(v interface{}) (InnerInput, error) {
var it InnerInput

Expand Down Expand Up @@ -798,7 +918,7 @@ 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\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\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\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n}\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
Expand Down
23 changes: 23 additions & 0 deletions test/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test

import "math"

type Shape interface {
Area() float64
}

type Circle struct {
Radius float64
}

func (c *Circle) Area() float64 {
return c.Radius * math.Pi * math.Pi
}

type Rectangle struct {
Length, Width float64
}

func (r *Rectangle) Area() float64 {
return r.Length * r.Width
}
2 changes: 1 addition & 1 deletion test/resolvers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go -typemap types.json

package test

Expand Down
16 changes: 16 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@ type InnerObject {
id: Int!
}

interface Shape {
area: Float
}

type Circle implements Shape {
radius: Float
area: Float
}

type Rectangle implements Shape {
length: Float
width: Float
area: Float
}

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

0 comments on commit dc89840

Please sign in to comment.