Skip to content

Commit

Permalink
Fix marhaling interfaces and unions
Browse files Browse the repository at this point in the history
  • Loading branch information
Вячеслав Крупянский committed Sep 19, 2024
1 parent 766c00b commit 9a1d923
Show file tree
Hide file tree
Showing 16 changed files with 717 additions and 36 deletions.
2 changes: 1 addition & 1 deletion codegen/interface.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func (ec *executionContext) _{{$interface.Name}}(ctx context.Context, sel ast.Se
return graphql.Null
{{- range $implementor := $interface.Implementors }}
case {{$implementor.Type | ref}}:
if len(graphql.CollectFields(ec.OperationContext, sel, []string{"{{$interface.Type | typeName }}", "{{$implementor.Type | typeName}}"})) == 0 {
if len(graphql.CollectFields(ec.OperationContext, sel, []string{"{{$interface.Type | typeName }}", "{{$implementor.Name}}"})) == 0 {
return graphql.Empty{}
}
{{- if $implementor.CanBeNil }}
Expand Down
176 changes: 176 additions & 0 deletions codegen/testserver/followschema/interfaces.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions codegen/testserver/followschema/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func (r *Rectangle) Area() float64 {
func (r *Rectangle) isShapeUnion() {}
func (r *Rectangle) isShape() {}

type SquareShape struct {
Length float64
Coordinates
}

func (r *SquareShape) Area() float64 {
return r.Length * r.Length
}
func (r *SquareShape) isShapeUnion() {}
func (r *SquareShape) isShape() {}

type Node interface {
Child() (Node, error)
}
Expand Down
7 changes: 6 additions & 1 deletion codegen/testserver/followschema/interfaces.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ type Rectangle implements Shape {
area: Float
coordinates: Coordinates
}
union ShapeUnion @goModel(model: "followschema.ShapeUnion") = Circle | Rectangle
type Square implements Shape @goModel(model: "followschema.SquareShape") {
length: Float
area: Float
coordinates: Coordinates
}
union ShapeUnion @goModel(model: "followschema.ShapeUnion") = Circle | Rectangle | Square

directive @makeNil on FIELD_DEFINITION
directive @makeTypedNil on FIELD_DEFINITION
Expand Down
91 changes: 91 additions & 0 deletions codegen/testserver/followschema/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,95 @@ func TestInterfaces(t *testing.T) {
require.Equal(t, 100, resp.Dog.Size.Height)
require.Equal(t, 35, resp.Dog.Size.Weight)
})

t.Run("fetch_go_model_for_union", func(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.Shapes = func(ctx context.Context) ([]Shape, error) {
return []Shape{
&SquareShape{
Length: 10,
Coordinates: Coordinates{
X: 5,
Y: 15,
},
},
}, nil
}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
var resp struct {
Shapes []struct {
Length float64
Area float64
Coordinates struct {
X float64
Y float64
}
}
}

c.MustPost(`
{
shapes {
... on Square {
length
area
coordinates {
x
y
}
}
}
}
`, &resp)

require.Len(t, resp.Shapes, 1)

square := resp.Shapes[0]

require.InDelta(t, 10, square.Length, 0.1)
require.InDelta(t, 100, square.Area, 0.1)
require.InDelta(t, 5, square.Coordinates.X, 0.1)
require.InDelta(t, 15, square.Coordinates.Y, 0.1)
})

t.Run("not_fetch_field_that_has_values", func(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.Shapes = func(ctx context.Context) ([]Shape, error) {
return []Shape{
&Circle{
Radius: 10,
Coordinates: Coordinates{
X: 5,
Y: 15,
},
},
}, nil
}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
var resp struct {
Shapes []struct {
Coordinates struct {
X float64
Y float64
}
}
}

c.MustPost(`
{
shapes {
... on Rectangle {
coordinates {
x
y
}
}
}
}
`, &resp)

require.Empty(t, resp.Shapes)
})
}
27 changes: 27 additions & 0 deletions codegen/testserver/followschema/root_.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9a1d923

Please sign in to comment.