Skip to content

Commit

Permalink
Merge pull request 99designs#526 from 99designs/union-fragment-bug
Browse files Browse the repository at this point in the history
Union Fragment Bug Fix
  • Loading branch information
Mathew Byrne authored Feb 7, 2019
2 parents b512a0f + 7ec7415 commit 676f50c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 9 deletions.
4 changes: 2 additions & 2 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ type Objects []*Object

func (o *Object) Implementors() string {
satisfiedBy := strconv.Quote(o.Name)
for _, s := range o.Definition.Interfaces {
satisfiedBy += ", " + strconv.Quote(s)
for _, s := range o.Implements {
satisfiedBy += ", " + strconv.Quote(s.Name)
}
return "[]string{" + satisfiedBy + "}"
}
Expand Down
53 changes: 51 additions & 2 deletions codegen/testserver/generated.go

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

51 changes: 51 additions & 0 deletions codegen/testserver/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package testserver

import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)
Expand All @@ -30,3 +33,51 @@ func TestEnums(t *testing.T) {
require.Equal(t, StatusError, AllStatus[1])
})
}

func TestUnionFragments(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.ShapeUnion = func(ctx context.Context) (ShapeUnion, error) {
return &Circle{Radius: 32}, nil
}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

t.Run("inline fragment on union", func(t *testing.T) {
var resp struct {
ShapeUnion struct {
Radius float64
}
}
c.MustPost(`query {
shapeUnion {
... on Circle {
radius
}
}
}
`, &resp)
require.NotEmpty(t, resp.ShapeUnion.Radius)
})

t.Run("named fragment", func(t *testing.T) {
var resp struct {
ShapeUnion struct {
Radius float64
}
}
c.MustPost(`query {
shapeUnion {
...C
}
}
fragment C on ShapeUnion {
... on Circle {
radius
}
}
`, &resp)
require.NotEmpty(t, resp.ShapeUnion.Radius)
})
}
3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (r *queryResolver) DirectiveInput(ctx context.Context, arg InputDirectives)
func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, error) {
panic("not implemented")
}
func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) {
panic("not implemented")
}
func (r *queryResolver) KeywordArgs(ctx context.Context, breakArg string, defaultArg string, funcArg string, interfaceArg string, selectArg string, caseArg string, deferArg string, goArg string, mapArg string, structArg string, chanArg string, elseArg string, gotoArg string, packageArg string, switchArg string, constArg string, fallthroughArg string, ifArg string, rangeArg string, typeArg string, continueArg string, forArg string, importArg string, returnArg string, varArg string) (bool, error) {
panic("not implemented")
}
Expand Down
1 change: 1 addition & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Query {
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
inputSlice(arg: [String!]!): Boolean!
shapeUnion: ShapeUnion!
}

type Subscription {
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

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

6 changes: 3 additions & 3 deletions example/starwars/generated.go

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

2 changes: 1 addition & 1 deletion example/type-system-extension/generated.go

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

5 changes: 4 additions & 1 deletion graphql/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func collectFields(reqCtx *RequestContext, selSet ast.SelectionSet, satisfies []

f.Selections = append(f.Selections, sel.SelectionSet...)
case *ast.InlineFragment:
if !shouldIncludeNode(sel.Directives, reqCtx.Variables) || !instanceOf(sel.TypeCondition, satisfies) {
if !shouldIncludeNode(sel.Directives, reqCtx.Variables) {
continue
}
if !instanceOf(sel.TypeCondition, satisfies) {
continue
}
for _, childField := range collectFields(reqCtx, sel.SelectionSet, satisfies, visited) {
Expand Down

0 comments on commit 676f50c

Please sign in to comment.