From a3dde79bf14c9672997044aec14c624333653dca Mon Sep 17 00:00:00 2001 From: Simon Hanna Date: Mon, 12 Dec 2022 15:43:18 +0100 Subject: [PATCH 1/3] 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 #539 --- graphql_test.go | 18 ++++++++++++++++++ internal/exec/selected/selected.go | 7 ++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/graphql_test.go b/graphql_test.go index f906f14b..a6770c2c 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -1544,6 +1544,24 @@ func (r *testBadEnumCharacterResolver) AppearsIn() []string { return []string{"STAR_TREK"} } +func TestUnknownType(t *testing.T) { + gqltesting.RunTest(t, &gqltesting.Test{ + Schema: starwarsSchema, + Query: ` + query TypeInfo { + __type(name: "unknown-type") { + name + } + } + `, + ExpectedResult: ` + { + "__type": null + } + `, + }) +} + func TestEnums(t *testing.T) { gqltesting.RunTests(t, []*gqltesting.Test{ // Valid input enum supplied in query text diff --git a/internal/exec/selected/selected.go b/internal/exec/selected/selected.go index 868dc1e9..cda5f48a 100644 --- a/internal/exec/selected/selected.go +++ b/internal/exec/selected/selected.go @@ -108,8 +108,9 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s } t, ok := r.Schema.Types[v.String()] - if !ok { - return nil + var resolvedType *introspection.Type + if ok { + resolvedType = introspection.WrapType(t) } flattenedSels = append(flattenedSels, &SchemaField{ @@ -117,7 +118,7 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s Alias: field.Alias.Name, Sels: applySelectionSet(r, s, s.Meta.Type, field.SelectionSet), Async: true, - FixedResult: reflect.ValueOf(introspection.WrapType(t)), + FixedResult: reflect.ValueOf(resolvedType), }) } From 10c63bdfafbab416081fcafb68c8ae710a70341a Mon Sep 17 00:00:00 2001 From: Pavel Nikolov Date: Fri, 16 Dec 2022 15:17:45 +0200 Subject: [PATCH 2/3] Update internal/exec/selected/selected.go --- internal/exec/selected/selected.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/exec/selected/selected.go b/internal/exec/selected/selected.go index cda5f48a..3ed701fe 100644 --- a/internal/exec/selected/selected.go +++ b/internal/exec/selected/selected.go @@ -107,7 +107,9 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s return nil } + var resolvedType *introspection.Type t, ok := r.Schema.Types[v.String()] + var resolvedType *introspection.Type if ok { resolvedType = introspection.WrapType(t) From 9d345ff01029f1c0dd09d7d8e4e5b065670e4cb2 Mon Sep 17 00:00:00 2001 From: Pavel Nikolov Date: Fri, 16 Dec 2022 15:19:05 +0200 Subject: [PATCH 3/3] Update internal/exec/selected/selected.go --- internal/exec/selected/selected.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/exec/selected/selected.go b/internal/exec/selected/selected.go index 3ed701fe..f5b37405 100644 --- a/internal/exec/selected/selected.go +++ b/internal/exec/selected/selected.go @@ -109,8 +109,6 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s var resolvedType *introspection.Type t, ok := r.Schema.Types[v.String()] - - var resolvedType *introspection.Type if ok { resolvedType = introspection.WrapType(t) }