Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions docs/schema-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ or an array with the following options:
| subscription | `ObjectType` or `callable(): ?ObjectType` or `null` | Reserved for future subscriptions implementation. Currently presented for compatibility with introspection query of **graphql-js**, used by various clients (like Relay or GraphiQL) |
| directives | `array<Directive>` | A full list of [directives](type-definitions/directives.md) supported by your schema. By default, contains built-in **@skip** and **@include** directives.<br><br> If you pass your own directives and still want to use built-in directives - add them explicitly. For example:<br><br> _array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);_ |
| types | `array<ObjectType>` | List of object types which cannot be detected by **graphql-php** during static schema analysis.<br><br>Most often this happens when the object type is never referenced in fields directly but is still a part of a schema because it implements an interface which resolves to this object type in its **resolveType** callable. <br><br> Note that you are not required to pass all of your types here - it is simply a workaround for a concrete use-case. |
| typeLoader | `callable(string $name): Type` | Expected to return a type instance given the name. Must always return the same instance if called multiple times, see [lazy loading](#lazy-loading-of-types). See section below on lazy type loading. |
| typeLoader | `callable(string $name): ?Type` | Expected to return a type instance given the name, or `null` for unknown types. Must always return the same instance if called multiple times, see [lazy loading](#lazy-loading-of-types). See section below on lazy type loading. |

### Using config class

Expand Down Expand Up @@ -164,8 +164,8 @@ final class Types
/** @var array<string, Type&NamedType> */
private static array $types = [];

/** @return Type&NamedType */
public static function load(string $typeName): Type
/** @return (Type&NamedType)|null */
public static function load(string $typeName): ?Type
{
if (isset(self::$types[$typeName])) {
return self::$types[$typeName];
Expand All @@ -178,7 +178,7 @@ final class Types
default => lcfirst($typeName),
};
if (! method_exists(self::class, $methodName)) {
throw new \Exception("Unknown GraphQL type: {$typeName}.");
return null;
}

$type = self::{$methodName}(); // @phpstan-ignore-line variable static method call
Expand Down
9 changes: 7 additions & 2 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static function overrideStandardTypes(array $types): void
throw new InvariantViolation("Expecting instance of {$typeClass}, got {$notType}");
}

if (! in_array($type->name, self::BUILT_IN_SCALAR_NAMES, true)) {
if (! self::isBuiltInScalarName($type->name)) {
$standardTypeNames = implode(', ', self::BUILT_IN_SCALAR_NAMES);
$notStandardTypeName = Utils::printSafe($type->name);
throw new InvariantViolation("Expecting one of the following names for a standard type: {$standardTypeNames}; got {$notStandardTypeName}");
Expand All @@ -228,7 +228,12 @@ public static function overrideStandardTypes(array $types): void
public static function isBuiltInScalar($type): bool
{
return $type instanceof ScalarType
&& in_array($type->name, self::BUILT_IN_SCALAR_NAMES, true);
&& self::isBuiltInScalarName($type->name);
}

public static function isBuiltInScalarName(string $name): bool
{
return in_array($name, self::BUILT_IN_SCALAR_NAMES, true);
}

/**
Expand Down
32 changes: 30 additions & 2 deletions src/Type/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\GraphQL;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SchemaDefinitionNode;
Expand Down Expand Up @@ -189,12 +190,19 @@ public function getTypeMap(): array
}

if (isset($this->config->typeLoader)) {
$typeLoader = $this->config->typeLoader;
foreach (Type::BUILT_IN_SCALAR_NAMES as $scalarName) {
if (isset($scalarOverrides[$scalarName])) {
continue;
}

$type = ($this->config->typeLoader)($scalarName);
try {
$type = $typeLoader($scalarName);
} catch (\Throwable $e) {
self::deprecateThrowingTypeLoaderForBuiltInScalar($scalarName, $e);
continue;
}

if ($type instanceof ScalarType
&& $type->name === $scalarName
&& $type !== $builtInScalars[$scalarName]
Expand Down Expand Up @@ -225,6 +233,15 @@ public function getDirectives(): array
return $this->config->directives ?? GraphQL::getStandardDirectives();
}

private static function deprecateThrowingTypeLoaderForBuiltInScalar(string $scalarName, \Throwable $e): void
{
Warning::warn(
"Type loader should return null for unknown types, but threw for built-in scalar \"{$scalarName}\": {$e->getMessage()}. In a future major version, this exception will not be caught.",
Warning::WARNING_CONFIG_DEPRECATION,
\E_USER_DEPRECATED,
);
}

/** @param mixed $typeLoaderReturn could be anything */
public static function typeLoaderNotType($typeLoaderReturn): string
{
Expand Down Expand Up @@ -366,7 +383,18 @@ private function loadType(string $typeName): ?Type
return $this->getTypeMap()[$typeName] ?? null;
}

$type = ($this->config->typeLoader)($typeName);
try {
$type = ($this->config->typeLoader)($typeName);
} catch (\Throwable $e) {
if (Type::isBuiltInScalarName($typeName)) {
self::deprecateThrowingTypeLoaderForBuiltInScalar($typeName, $e);

return null;
}

throw $e;
}

if ($type === null) {
return null;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Type/TypeLoaderTestCaseBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Tests\TestCaseBase;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\ObjectType;
Expand Down Expand Up @@ -80,6 +81,51 @@ public function testIgnoresNonExistentType(): void
self::assertNull($schema->getType('NonExistingType'));
}

public function testThrowingTypeLoaderForBuiltInScalarTriggersDeprecation(): void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => function (string $name): ?Type {
if (Type::isBuiltInScalarName($name)) {
throw new \Exception("Type \"{$name}\" not found");
}

return ($this->typeLoader)($name);
},
]);

$warnings = [];
Warning::setWarningHandler(static function (string $message, int $warningId) use (&$warnings): void {
$warnings[] = ['message' => $message, 'id' => $warningId];
});

try {
self::assertSame(Type::string(), $schema->getType('String'));
self::assertSame(Type::string(), $schema->getTypeMap()['String']);

self::assertNotEmpty($warnings);
self::assertSame(Warning::WARNING_CONFIG_DEPRECATION, $warnings[0]['id']);
self::assertStringContainsString('String', $warnings[0]['message']);
} finally {
Warning::setWarningHandler(null);
}
}

public function testThrowingTypeLoaderForNonBuiltInTypeIsNotCaught(): void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => static function (string $name): ?Type {
throw new \Exception("Type \"{$name}\" not found");
},
]);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Type "Node" not found');

$schema->getType('Node');
}

public function testFailsOnNonType(): void
{
$notType = new \stdClass();
Expand Down
Loading