Skip to content

Commit

Permalink
Add reset method to Type, Directive and Introspection
Browse files Browse the repository at this point in the history
See webonyx#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 12, 2024
1 parent 0420ef7 commit 5b982c1
Show file tree
Hide file tree
Showing 3 changed files with 22 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 = [];
}
}
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 = [];
}
}

0 comments on commit 5b982c1

Please sign in to comment.