Skip to content

Commit

Permalink
Merge pull request #5 from sipsma/enforce-iface-at-runtime
Browse files Browse the repository at this point in the history
check whether object implements interface at runtime
  • Loading branch information
sipsma authored Dec 12, 2023
2 parents bc781b6 + 01b09ff commit c34d978
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions abstract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
humanType = graphql.NewObject(graphql.ObjectConfig{
Name: "Human",
Fields: graphql.Fields{
"name": &graphql.Field{
"job": &graphql.Field{
Type: graphql.String,
},
},
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
},
Errors: []gqlerrors.FormattedError{
{
Message: `Runtime Object type "Human" is not a possible type for "Pet".`,
Message: `"Pet" expects field "name" but "Human" does not provide it.`,
Locations: []location.SourceLocation{
{
Line: 2,
Expand Down
17 changes: 10 additions & 7 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,18 @@ func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldAST
}
if unionReturnType, ok := returnType.(*Union); ok && unionReturnType.ResolveType != nil {
runtimeType = unionReturnType.ResolveType(resolveTypeParams)
if !eCtx.Schema.IsPossibleType(returnType, runtimeType) {
panic(gqlerrors.NewFormattedError(
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
`for "%v".`, runtimeType, returnType),
))
}
} else if interfaceReturnType, ok := returnType.(*Interface); ok && interfaceReturnType.ResolveType != nil {
runtimeType = interfaceReturnType.ResolveType(resolveTypeParams)
// verify the object matches the interface
if err := assertObjectImplementsInterface(&eCtx.Schema, runtimeType, interfaceReturnType); err != nil {
panic(err)
}
} else {
runtimeType = defaultResolveTypeFn(resolveTypeParams, returnType)
}
Expand All @@ -807,13 +817,6 @@ func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldAST
panic(err)
}

if !eCtx.Schema.IsPossibleType(returnType, runtimeType) {
panic(gqlerrors.NewFormattedError(
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
`for "%v".`, runtimeType, returnType),
))
}

return completeObjectValue(eCtx, runtimeType, fieldASTs, info, path, result)
}

Expand Down

0 comments on commit c34d978

Please sign in to comment.