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 5 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;
}
}
15 changes: 11 additions & 4 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ abstract class Type implements \JsonSerializable
];

/** @var array<string, ScalarType> */
ruudk marked this conversation as resolved.
Show resolved Hide resolved
protected static array $standardTypes;
protected static ?array $standardTypes = [];
ruudk marked this conversation as resolved.
Show resolved Hide resolved

/** @var array<string, Type&NamedType>|null */
protected static ?array $builtInTypes = null;
ruudk marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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 @@ -261,4 +262,10 @@ public function jsonSerialize(): string
{
return $this->toString();
}

public static function reset(): void
{
static::$standardTypes = [];
ruudk marked this conversation as resolved.
Show resolved Hide resolved
static::$builtInTypes = null;
}
}
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 = [];
}
}
26 changes: 26 additions & 0 deletions tests/ResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace GraphQL\Tests;

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

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

Type::reset();
Introspection::reset();
Directive::reset();

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