Skip to content

Commit

Permalink
Add Type::reset and Directive::reset methods
Browse files Browse the repository at this point in the history
See #1424

This library uses a static cache for standard types and the internal directives.

While not ideal, this works fine. As long as you don't have a scenario where you want to change
them.

In my situation, we have 2 schema's with a custom ID type. They are not the same instance.
This still works fine, but as soon as we run our whole test suite at once, the static cache
is initialized in some test, and another test later expects something else.

While I think the better solution would be to remove these static caches completely, and store
them on an instance instead of statically. But that will be a much bigger task given the current
architecture.

With these 2 reset methods, we can manually reset the caches in our test suite.

It could also be used for people that run their GraphQL server in tools like RoadRunner
and FrankenPHP.
  • Loading branch information
ruudk committed Nov 11, 2024
1 parent dde68bf commit 5df4e31
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Type/Definition/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Directive
*
* @var array<string, Directive>
*/
protected static array $internalDirectives;
protected static array $internalDirectives = [];

public string $name;

Expand Down Expand Up @@ -90,7 +90,11 @@ public static function includeDirective(): Directive
*/
public static function getInternalDirectives(): array
{
return self::$internalDirectives ??= [
if (self::$internalDirectives !== []) {
return self::$internalDirectives;
}

return self::$internalDirectives = [
'include' => new self([
'name' => self::INCLUDE_NAME,
'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
Expand Down Expand Up @@ -162,4 +166,9 @@ public static function isSpecifiedDirective(Directive $directive): bool
{
return \array_key_exists($directive->name, self::getInternalDirectives());
}

public static function reset(): void
{
self::$internalDirectives = [];
}
}
7 changes: 6 additions & 1 deletion src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class Type implements \JsonSerializable
];

/** @var array<string, ScalarType> */
protected static array $standardTypes;
protected static array $standardTypes = [];

/**
* @api
Expand Down Expand Up @@ -261,4 +261,9 @@ public function jsonSerialize(): string
{
return $this->toString();
}

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

declare(strict_types=1);
namespace GraphQL\Tests\Type\Definition;

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

final class TypeTest extends TestCase
{
public function testReset(): void
{
$stringType = Type::string();

Type::reset();

self::assertNotSame($stringType, Type::string());
}
}
9 changes: 9 additions & 0 deletions tests/Type/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@ public function testDefinesADirectiveWithMultipleArgs(): void
self::assertFalse($argumentBar->isDeprecated());
}

public function testReset(): void
{
$directives = Directive::getInternalDirectives();

Directive::reset();

self::assertNotSame($directives, Directive::getInternalDirectives());
}

// TODO implement all of https://github.com/graphql/graphql-js/blob/master/src/type/__tests__/directive-test.js
}

0 comments on commit 5df4e31

Please sign in to comment.