Skip to content
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

Automatically reset static caches in Type, Introspection and Directive when overriding standard types #1632

Merged
merged 25 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
25 changes: 15 additions & 10 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class ReferenceExecutor implements ExecutorImplementation
*/
protected \SplObjectStorage $fieldArgsCache;

protected FieldDefinition $schemaMetaFieldDef;

protected FieldDefinition $typeMetaFieldDef;

protected FieldDefinition $typeNameMetaFieldDef;

protected function __construct(ExecutionContext $context)
{
if (! isset(static::$UNDEFINED)) {
Expand Down Expand Up @@ -701,23 +707,22 @@ protected function resolveField(
*/
protected function getFieldDef(Schema $schema, ObjectType $parentType, string $fieldName): ?FieldDefinition
{
static $schemaMetaFieldDef, $typeMetaFieldDef, $typeNameMetaFieldDef;
$schemaMetaFieldDef ??= Introspection::schemaMetaFieldDef();
$typeMetaFieldDef ??= Introspection::typeMetaFieldDef();
$typeNameMetaFieldDef ??= Introspection::typeNameMetaFieldDef();
$this->schemaMetaFieldDef ??= Introspection::schemaMetaFieldDef();
$this->typeMetaFieldDef ??= Introspection::typeMetaFieldDef();
$this->typeNameMetaFieldDef ??= Introspection::typeNameMetaFieldDef();

$queryType = $schema->getQueryType();

if ($fieldName === $schemaMetaFieldDef->name && $queryType === $parentType) {
return $schemaMetaFieldDef;
if ($fieldName === $this->schemaMetaFieldDef->name && $queryType === $parentType) {
return $this->schemaMetaFieldDef;
}

if ($fieldName === $typeMetaFieldDef->name && $queryType === $parentType) {
return $typeMetaFieldDef;
if ($fieldName === $this->typeMetaFieldDef->name && $queryType === $parentType) {
return $this->typeMetaFieldDef;
}

if ($fieldName === $typeNameMetaFieldDef->name) {
return $typeNameMetaFieldDef;
if ($fieldName === $this->typeNameMetaFieldDef->name) {
return $this->typeNameMetaFieldDef;
}

return $parentType->findField($fieldName);
Expand Down
9 changes: 7 additions & 2 deletions src/Type/Definition/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Directive
/**
* Lazily initialized.
*
* @var array<string, Directive>
* @var array<string, Directive>|null
*/
protected static array $internalDirectives;
protected static ?array $internalDirectives = null;

public string $name;

Expand Down Expand Up @@ -162,4 +162,9 @@ public static function isSpecifiedDirective(Directive $directive): bool
{
return \array_key_exists($directive->name, self::getInternalDirectives());
}

public static function reset(): void
{
self::$internalDirectives = null;
}
}
21 changes: 16 additions & 5 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ abstract class Type implements \JsonSerializable
...Introspection::TYPE_NAMES,
];

/** @var array<string, ScalarType> */
protected static array $standardTypes;
/** @var array<string, ScalarType>|null */
protected static ?array $standardTypes;

/** @var array<string, Type&NamedType>|null */
protected static ?array $builtInTypes;

/**
* @api
Expand Down Expand Up @@ -116,9 +119,7 @@ public static function nonNull($type): NonNull
*/
public static function builtInTypes(): array
{
static $builtInTypes;

return $builtInTypes ??= \array_merge(
return self::$builtInTypes ??= \array_merge(
Introspection::getTypes(),
self::getStandardTypes()
);
Expand Down Expand Up @@ -149,6 +150,8 @@ public static function getStandardTypes(): array
*/
public static function overrideStandardTypes(array $types): void
{
self::reset();

foreach ($types as $type) {
// @phpstan-ignore-next-line generic type is not enforced by PHP
if (! $type instanceof ScalarType) {
Expand Down Expand Up @@ -261,4 +264,12 @@ public function jsonSerialize(): string
{
return $this->toString();
}

public static function reset(): void
{
static::$builtInTypes = null;

Introspection::reset();
Directive::reset();
}
}
5 changes: 5 additions & 0 deletions src/Type/Introspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,9 @@ public static function typeNameMetaFieldDef(): FieldDefinition
'resolve' => static fn ($source, array $args, $context, ResolveInfo $info): string => $info->parentType->name,
]);
}

public static function reset(): void
{
self::$map = [];
}
}
24 changes: 24 additions & 0 deletions tests/Type/Definition/TypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace GraphQL\Tests\Type\Definition;

use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection;
use PHPUnit\Framework\TestCase;

final class TypeTest extends TestCase
{
public function testReset(): void
{
$string = Type::string();
$schema = Introspection::_schema();
$directives = Directive::getInternalDirectives();

Type::reset();

self::assertNotSame($string, Type::string());
self::assertNotSame($schema, Introspection::_schema());
self::assertNotSame($directives, Directive::getInternalDirectives());
}
}
20 changes: 18 additions & 2 deletions tests/Type/StandardTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection;
use PHPUnit\Framework\TestCase;

final class StandardTypesTest extends TestCase
Expand All @@ -29,7 +31,6 @@ public function testAllowsOverridingStandardTypes(): void
{
$originalTypes = Type::getStandardTypes();
self::assertCount(5, $originalTypes);
self::assertSame(self::$originalStandardTypes, $originalTypes);

$newBooleanType = self::createCustomScalarType(Type::BOOLEAN);
$newFloatType = self::createCustomScalarType(Type::FLOAT);
Expand Down Expand Up @@ -65,7 +66,6 @@ public function testPreservesOriginalStandardTypes(): void
{
$originalTypes = Type::getStandardTypes();
self::assertCount(5, $originalTypes);
self::assertSame(self::$originalStandardTypes, $originalTypes);

$newIDType = self::createCustomScalarType(Type::ID);
$newStringType = self::createCustomScalarType(Type::STRING);
Expand Down Expand Up @@ -122,6 +122,22 @@ public function testStandardTypesOverrideDoesSanityChecks($notType, string $expe
Type::overrideStandardTypes([$notType]);
}

public function testCachesShouldResetWhenOverridingStandardTypes(): void
{
$string = Type::string();
$schema = Introspection::_schema();
$directives = Directive::getInternalDirectives();

Type::overrideStandardTypes([
$newString = self::createCustomScalarType(Type::STRING),
]);

self::assertNotSame($string, Type::string());
self::assertSame($newString, Type::string());
self::assertNotSame($schema, Introspection::_schema());
self::assertNotSame($directives, Directive::getInternalDirectives());
}

/** @throws InvariantViolation */
private static function createCustomScalarType(string $name): CustomScalarType
{
Expand Down
Loading