Skip to content

Commit

Permalink
Merge pull request #148 from jainpiyush19/possibletypes_fix
Browse files Browse the repository at this point in the history
Fix possible types not being returned for interface introspection
  • Loading branch information
Lucian Jones authored Apr 20, 2022
2 parents 1fe4a6d + 8916475 commit ad49de9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
26 changes: 15 additions & 11 deletions execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,14 @@ func resolveType(ctx context.Context, schema *ast.Schema, typ *ast.Type, selecti
}
result[f.Alias] = interfaces
case "possibleTypes":
if len(namedType.Types) > 0 {
if namedType.Kind != ast.Interface && namedType.Kind != ast.Union {
result[f.Alias] = nil
} else {
types := []map[string]interface{}{}
for _, t := range namedType.Types {
types = append(types, resolveType(ctx, schema, &ast.Type{NamedType: t}, f.SelectionSet))
for _, t := range schema.PossibleTypes[namedType.Name] {
types = append(types, resolveType(ctx, schema, &ast.Type{NamedType: t.Name}, f.SelectionSet))
}
result[f.Alias] = types
} else {
result[f.Alias] = nil
}
case "enumValues":
includeDeprecated := false
Expand All @@ -425,13 +425,17 @@ func resolveType(ctx context.Context, schema *ast.Schema, typ *ast.Type, selecti
}
result[f.Alias] = enums
case "inputFields":
inputFields := []map[string]interface{}{}
for _, fi := range namedType.Fields {
// call resolveField instead of resolveInputValue because it has
// the right type and is a superset of it
inputFields = append(inputFields, resolveField(ctx, schema, fi, f.SelectionSet))
if namedType.Kind == ast.InputObject {
inputFields := []map[string]interface{}{}
for _, fi := range namedType.Fields {
// call resolveField instead of resolveInputValue because it has
// the right type and is a superset of it
inputFields = append(inputFields, resolveField(ctx, schema, fi, f.SelectionSet))
}
result[f.Alias] = inputFields
} else {
result[f.Alias] = nil
}
result[f.Alias] = inputFields
default:
result[f.Alias] = nil
}
Expand Down
25 changes: 25 additions & 0 deletions execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,31 @@ func TestIntrospectionQuery(t *testing.T) {
`, string(resp.Data))
})

t.Run("interface", func(t *testing.T) {
query := gqlparser.MustLoadQuery(es.MergedSchema, `
{
__type(name: "Person") {
possibleTypes {
name
}
}
}
`)
ctx := testContextWithoutVariables(query.Operations[0])
resp := es.ExecuteQuery(ctx)
require.JSONEq(t, `
{
"__type": {
"possibleTypes": [
{
"name": "Cast"
}
]
}
}
`, string(resp.Data))
})

t.Run("type referenced only through an interface", func(t *testing.T) {
query := gqlparser.MustLoadQuery(es.MergedSchema, `{
__type(name: "Cast") {
Expand Down

0 comments on commit ad49de9

Please sign in to comment.