Skip to content

Commit

Permalink
Merge 0f0f061 into bcbb692
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed May 10, 2021
2 parents bcbb692 + 0f0f061 commit 97117a2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
23 changes: 23 additions & 0 deletions benchmarks/StarWarsBench.php
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
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
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
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
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
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

0 comments on commit 97117a2

Please sign in to comment.