Skip to content

Commit 7db17b6

Browse files
committed
DisableIntrospection should not skip __typename for usages of GraphQL union types
1 parent beb923f commit 7db17b6

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

graphql_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,44 @@ func TestIntrospectionDisableIntrospection(t *testing.T) {
26142614
}
26152615
`,
26162616
},
2617+
2618+
{
2619+
Schema: starwarsSchemaNoIntrospection,
2620+
Query: `
2621+
{
2622+
search(text: "an") {
2623+
__typename
2624+
... on Human {
2625+
name
2626+
}
2627+
... on Droid {
2628+
name
2629+
}
2630+
... on Starship {
2631+
name
2632+
}
2633+
}
2634+
}
2635+
`,
2636+
ExpectedResult: `
2637+
{
2638+
"search": [
2639+
{
2640+
"__typename": "Human",
2641+
"name": "Han Solo"
2642+
},
2643+
{
2644+
"__typename": "Human",
2645+
"name": "Leia Organa"
2646+
},
2647+
{
2648+
"__typename": "Starship",
2649+
"name": "TIE Advanced x1"
2650+
}
2651+
]
2652+
}
2653+
`,
2654+
},
26172655
})
26182656
}
26192657

@@ -2997,7 +3035,7 @@ func TestInput(t *testing.T) {
29973035
})
29983036
}
29993037

3000-
type inputArgumentsHello struct {}
3038+
type inputArgumentsHello struct{}
30013039

30023040
type inputArgumentsScalarMismatch1 struct{}
30033041

internal/exec/selected/selected.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s
8181

8282
switch field.Name.Name {
8383
case "__typename":
84-
if !r.DisableIntrospection {
85-
flattenedSels = append(flattenedSels, &TypenameField{
86-
Object: *e,
87-
Alias: field.Alias.Name,
88-
})
89-
}
84+
// __typename is available even though r.DisableIntrospection == true
85+
// because it is necessary when using union types and interfaces: https://graphql.org/learn/schema/#union-types
86+
flattenedSels = append(flattenedSels, &TypenameField{
87+
Object: *e,
88+
Alias: field.Alias.Name,
89+
})
9090

9191
case "__schema":
9292
if !r.DisableIntrospection {

0 commit comments

Comments
 (0)