Skip to content

Warn about orphaned object types #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,16 @@ protected function ensureValidRuntimeType(
);
}

if ($this->exeContext->schema->getType($runtimeType->name) === null) {
throw new InvariantViolation(
'Schema does not contain type "' . $runtimeType->name . '". ' .
'This can happen when an object type is only referenced indirectly through ' .
'abstract types and never directly through fields.' .
'List the type in the option "types" during schema construction, ' .
'see https://webonyx.github.io/graphql-php/type-system/schema/#configuration-options.'
);
}

if ($runtimeType !== $this->exeContext->schema->getType($runtimeType->name)) {
throw new InvariantViolation(
sprintf(
Expand Down
60 changes: 60 additions & 0 deletions tests/Executor/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,66 @@ public function testReturningInvalidValueFromResolveTypeYieldsUsefulError(): voi
self::assertEquals($expected, $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
}

public function testWarnsAboutOrphanedTypesWhenMissingType(): void
{
$fooObject = null;
$fooInterface = new InterfaceType([
'name' => 'FooInterface',
'fields' => [
'bar' => [
'type' => Type::string(),
],
],
'resolveType' => static function () use (&$fooObject): ?ObjectType {
return $fooObject;
},
]);

$fooObject = new ObjectType([
'name' => 'FooObject',
'fields' => [
'bar' => [
'type' => Type::string(),
],
],
'interfaces' => [$fooInterface],
]);

$schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'foo' => [
'type' => $fooInterface,
'resolve' => static fn (): array => ['bar' => 'baz'],
],
],
]),
]);

$result = GraphQL::executeQuery($schema, '{ foo { bar } }');

$expected = [
'data' => ['foo' => null],
'errors' => [
[
'message' => 'Internal server error',
'locations' => [['line' => 1, 'column' => 3]],
'path' => ['foo'],
'extensions' => [
'debugMessage' =>
'Schema does not contain type "FooObject". ' .
'This can happen when an object type is only referenced indirectly through ' .
'abstract types and never directly through fields.' .
'List the type in the option "types" during schema construction, ' .
'see https://webonyx.github.io/graphql-php/type-system/schema/#configuration-options.',
],
],
],
];
self::assertEquals($expected, $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
}

/**
* @see it('resolveType allows resolving with type name')
*/
Expand Down