Skip to content

Commit 653514d

Browse files
committed
handle case where interface is type-asserted to same interface
1 parent 5b0cad1 commit 653514d

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

graphql_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,31 @@ func TestInlineFragments(t *testing.T) {
17261726
`,
17271727
},
17281728

1729+
{
1730+
Schema: starwarsSchema,
1731+
Query: `
1732+
query CharacterSearch {
1733+
hero {
1734+
... on Character {
1735+
... on Human {
1736+
name
1737+
}
1738+
... on Droid {
1739+
name
1740+
}
1741+
}
1742+
}
1743+
}
1744+
`,
1745+
ExpectedResult: `
1746+
{
1747+
"hero": {
1748+
"name": "R2-D2"
1749+
}
1750+
}
1751+
`,
1752+
},
1753+
17291754
{
17301755
Schema: socialSchema,
17311756
Query: `
@@ -2992,27 +3017,27 @@ type helloInputMismatch struct {
29923017
World string
29933018
}
29943019

2995-
func (r *inputArgumentsHello) Hello(args struct { Input *helloInput }) string {
3020+
func (r *inputArgumentsHello) Hello(args struct{ Input *helloInput }) string {
29963021
return "Hello " + args.Input.Name + "!"
29973022
}
29983023

29993024
func (r *inputArgumentsScalarMismatch1) Hello(name string) string {
30003025
return "Hello " + name + "!"
30013026
}
30023027

3003-
func (r *inputArgumentsScalarMismatch2) Hello(args struct { World string }) string {
3028+
func (r *inputArgumentsScalarMismatch2) Hello(args struct{ World string }) string {
30043029
return "Hello " + args.World + "!"
30053030
}
30063031

30073032
func (r *inputArgumentsObjectMismatch1) Hello(in helloInput) string {
30083033
return "Hello " + in.Name + "!"
30093034
}
30103035

3011-
func (r *inputArgumentsObjectMismatch2) Hello(args struct { Input *helloInputMismatch }) string {
3036+
func (r *inputArgumentsObjectMismatch2) Hello(args struct{ Input *helloInputMismatch }) string {
30123037
return "Hello " + args.Input.World + "!"
30133038
}
30143039

3015-
func (r *inputArgumentsObjectMismatch3) Hello(args struct { Input *struct { Thing string } }) string {
3040+
func (r *inputArgumentsObjectMismatch3) Hello(args struct{ Input *struct{ Thing string } }) string {
30163041
return "Hello " + args.Input.Thing + "!"
30173042
}
30183043

internal/exec/selected/selected.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -173,37 +173,39 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s
173173
}
174174

175175
func applyFragment(r *Request, s *resolvable.Schema, e *resolvable.Object, frag *query.Fragment) []Selection {
176-
t := r.Schema.Resolve(frag.On.Name)
177-
face, ok := t.(*schema.Interface)
178-
if !ok && frag.On.Name != "" && frag.On.Name != e.Name {
179-
a, ok := e.TypeAssertions[frag.On.Name]
180-
if !ok {
181-
panic(fmt.Errorf("%q does not implement %q", frag.On, e.Name)) // TODO proper error handling
176+
if frag.On.Name != e.Name {
177+
t := r.Schema.Resolve(frag.On.Name)
178+
face, ok := t.(*schema.Interface)
179+
if !ok && frag.On.Name != "" {
180+
a, ok := e.TypeAssertions[frag.On.Name]
181+
if !ok {
182+
panic(fmt.Errorf("%q does not implement %q", frag.On, e.Name)) // TODO proper error handling
183+
}
184+
185+
return []Selection{&TypeAssertion{
186+
TypeAssertion: *a,
187+
Sels: applySelectionSet(r, s, a.TypeExec.(*resolvable.Object), frag.Selections),
188+
}}
182189
}
190+
if ok && len(face.PossibleTypes) > 0 {
191+
sels := []Selection{}
192+
for _, t := range face.PossibleTypes {
193+
if t.Name == e.Name {
194+
return applySelectionSet(r, s, e, frag.Selections)
195+
}
183196

184-
return []Selection{&TypeAssertion{
185-
TypeAssertion: *a,
186-
Sels: applySelectionSet(r, s, a.TypeExec.(*resolvable.Object), frag.Selections),
187-
}}
188-
}
189-
if ok && len(face.PossibleTypes) > 0 {
190-
sels := []Selection{}
191-
for _, t := range face.PossibleTypes {
192-
if t.Name == e.Name {
193-
return applySelectionSet(r, s, e, frag.Selections)
197+
if a, ok := e.TypeAssertions[t.Name]; ok {
198+
sels = append(sels, &TypeAssertion{
199+
TypeAssertion: *a,
200+
Sels: applySelectionSet(r, s, a.TypeExec.(*resolvable.Object), frag.Selections),
201+
})
202+
}
194203
}
195-
196-
if a, ok := e.TypeAssertions[t.Name]; ok {
197-
sels = append(sels, &TypeAssertion{
198-
TypeAssertion: *a,
199-
Sels: applySelectionSet(r, s, a.TypeExec.(*resolvable.Object), frag.Selections),
200-
})
204+
if len(sels) == 0 {
205+
panic(fmt.Errorf("%q does not implement %q", e.Name, frag.On)) // TODO proper error handling
201206
}
207+
return sels
202208
}
203-
if len(sels) == 0 {
204-
panic(fmt.Errorf("%q does not implement %q", e.Name, frag.On)) // TODO proper error handling
205-
}
206-
return sels
207209
}
208210
return applySelectionSet(r, s, e, frag.Selections)
209211
}

0 commit comments

Comments
 (0)