Skip to content
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
23 changes: 23 additions & 0 deletions benchmarks/StarWarsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,27 @@ public function benchStarWarsIntrospectionQuery()
$this->introQuery
);
}

public function benchQueryWithInterfaceFragment()
{
$q = '
query UseInterfaceFragment {
luke: human(id: "1000") {
...CharacterFragment
}
leia: human(id: "1003") {
...CharacterFragment
}
}

fragment CharacterFragment on Character {
name
}
';

GraphQL::executeQuery(
StarWarsSchema::build(),
$q
);
}
}
1 change: 1 addition & 0 deletions benchmarks/shim.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
$b->benchHeroQuery();
$b->benchNestedQuery();
$b->benchQueryWithFragment();
$b->benchQueryWithInterfaceFragment();
$b->benchStarWarsIntrospectionQuery();
32 changes: 8 additions & 24 deletions src/Type/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
use GraphQL\Utils\InterfaceImplementations;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
use InvalidArgumentException;
use Traversable;
use function array_map;
use function array_values;
use function get_class;
use function implode;
use function is_array;
use function is_callable;
Expand Down Expand Up @@ -60,13 +61,6 @@ class Schema
*/
private $resolvedTypes = [];

/**
* Lazily initialized.
*
* @var array<string, array<string, ObjectType|UnionType>>
*/
private $subTypeMap;

/**
* Lazily initialised.
*
Expand Down Expand Up @@ -507,25 +501,15 @@ public function isPossibleType(AbstractType $abstractType, ObjectType $possibleT
*/
public function isSubType(AbstractType $abstractType, ImplementingType $maybeSubType) : bool
{
if (! isset($this->subTypeMap[$abstractType->name])) {
$this->subTypeMap[$abstractType->name] = [];
if ($abstractType instanceof InterfaceType) {
return $maybeSubType->implementsInterface($abstractType);
}

if ($abstractType instanceof UnionType) {
foreach ($abstractType->getTypes() as $type) {
$this->subTypeMap[$abstractType->name][$type->name] = true;
}
} else {
$implementations = $this->getImplementations($abstractType);
foreach ($implementations->objects() as $type) {
$this->subTypeMap[$abstractType->name][$type->name] = true;
}
foreach ($implementations->interfaces() as $type) {
$this->subTypeMap[$abstractType->name][$type->name] = true;
}
}
if ($abstractType instanceof UnionType) {
return $abstractType->isPossibleType($maybeSubType);
}

return isset($this->subTypeMap[$abstractType->name][$maybeSubType->name]);
throw new InvalidArgumentException(sprintf('$abstractType must be of type UnionType|InterfaceType got: %s.', get_class($abstractType)));
}

/**
Expand Down
5 changes: 0 additions & 5 deletions tests/Type/LazyTypeLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ public function testWorksWithTypeLoader() : void
'Node',
'Content',
'PostStoryMutationInput',
'Query.fields',
'Content.fields',
'Node.fields',
'Mutation.fields',
'BlogStory.fields',
],
$this->calls
);
Expand Down
25 changes: 25 additions & 0 deletions tests/Type/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
namespace GraphQL\Tests\Type;

use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\AbstractType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class SchemaTest extends TestCase
Expand Down Expand Up @@ -130,4 +133,26 @@ public function testIncludesInputTypesOnlyUsedInDirectives() : void
self::assertArrayHasKey('DirInput', $typeMap);
self::assertArrayHasKey('WrappedDirInput', $typeMap);
}

// Sub Type

/**
* @see it('validates argument to isSubType to be of the correct type')
*/
public function testThrowsInvalidArgumentExceptionWhenInvalidTypeIsPassedToIsSubType() : void
{
$this->expectException(InvalidArgumentException::class);

$anonymousAbstractType = new class implements AbstractType {
public function resolveType($objectValue, $context, ResolveInfo $info)
{
return null;
}
};

$this->schema->isSubType(
$anonymousAbstractType,
new InterfaceType(['name' => 'Interface'])
);
}
}
5 changes: 0 additions & 5 deletions tests/Type/TypeLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,6 @@ public function testWorksWithTypeLoader() : void
'Node',
'Content',
'PostStoryMutationInput',
'Query.fields',
'Content.fields',
'Node.fields',
'Mutation.fields',
'BlogStory.fields',
],
$this->calls
);
Expand Down