Skip to content

Commit 9e784f0

Browse files
simhnnaKNiepok
authored andcommitted
Fix __type queries sometimes not returning data (graph-gophers#540)
* Fix __type queries sometimes not returning data If the queried type was not defined it would return an empty data section instead of `__type: null` Fixes graph-gophers#539
1 parent 92d22a8 commit 9e784f0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

graphql_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,24 @@ func (r *testBadEnumCharacterResolver) AppearsIn() []string {
15451545
return []string{"STAR_TREK"}
15461546
}
15471547

1548+
func TestUnknownType(t *testing.T) {
1549+
gqltesting.RunTest(t, &gqltesting.Test{
1550+
Schema: starwarsSchema,
1551+
Query: `
1552+
query TypeInfo {
1553+
__type(name: "unknown-type") {
1554+
name
1555+
}
1556+
}
1557+
`,
1558+
ExpectedResult: `
1559+
{
1560+
"__type": null
1561+
}
1562+
`,
1563+
})
1564+
}
1565+
15481566
func TestEnums(t *testing.T) {
15491567
gqltesting.RunTests(t, []*gqltesting.Test{
15501568
// Valid input enum supplied in query text

internal/exec/selected/selected.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s
107107
return nil
108108
}
109109

110+
var resolvedType *introspection.Type
110111
t, ok := r.Schema.Types[v.String()]
111-
if !ok {
112-
return nil
112+
if ok {
113+
resolvedType = introspection.WrapType(t)
113114
}
114115

115116
flattenedSels = append(flattenedSels, &SchemaField{
116117
Field: s.Meta.FieldType,
117118
Alias: field.Alias.Name,
118119
Sels: applySelectionSet(r, s, s.Meta.Type, field.SelectionSet),
119120
Async: true,
120-
FixedResult: reflect.ValueOf(introspection.WrapType(t)),
121+
FixedResult: reflect.ValueOf(resolvedType),
121122
})
122123
}
123124

0 commit comments

Comments
 (0)