Skip to content

Commit

Permalink
Merge pull request #888 from ruudk/remove-addDefaultFallBackToTypeLoader
Browse files Browse the repository at this point in the history
Remove `addDefaultFallBackToTypeLoader`
  • Loading branch information
mcg-web committed Dec 22, 2021
2 parents 18a0fa0 + 2c02009 commit ebdcfe0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
30 changes: 2 additions & 28 deletions src/Definition/Type/ExtensibleSchema.php
Expand Up @@ -4,19 +4,17 @@

namespace Overblog\GraphQLBundle\Definition\Type;

use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Type\SchemaConfig;
use Overblog\GraphQLBundle\Definition\Type\SchemaExtension\SchemaExtensionInterface;
use Overblog\GraphQLBundle\Resolver\UnresolvableException;

class ExtensibleSchema extends Schema
{
public function __construct($config)
{
parent::__construct($this->addDefaultFallBackToTypeLoader(
parent::__construct(
$config instanceof SchemaConfig ? $config : SchemaConfig::create($config)
));
);
}

/** @var SchemaExtensionInterface[] */
Expand Down Expand Up @@ -53,28 +51,4 @@ public function processExtensions()

return $this;
}

private function addDefaultFallBackToTypeLoader(SchemaConfig $schemaConfig): SchemaConfig
{
$typeLoader = $schemaConfig->typeLoader;
$loaderWrapper = null;
$loaderWrapper = function ($name) use ($typeLoader, &$schemaConfig, &$loaderWrapper): ?Type {
$type = null;
try {
$type = $typeLoader($name);
} catch (UnresolvableException $e) {
// second chance for types with un-registered name in TypeResolver
// we disabled the custom typeLoader to force default loader usage
$schemaConfig->typeLoader = null;
$type = $this->getType($name);
$schemaConfig->typeLoader = $loaderWrapper; // @phpstan-ignore-line
}

return $type;
};

$schemaConfig->typeLoader = $loaderWrapper; // @phpstan-ignore-line

return $schemaConfig;
}
}
3 changes: 3 additions & 0 deletions src/Resolver/TypeResolver.php
Expand Up @@ -51,6 +51,9 @@ public function resolve($alias): ?Type
if (!isset($this->cache[$alias])) {
$type = $this->baseType($alias);
$this->cache[$alias] = $type;
if (isset($type->name) && $type->name !== $alias) {
$this->cache[$type->name] = $type;
}
}

return $this->cache[$alias];
Expand Down
7 changes: 5 additions & 2 deletions tests/Functional/MultipleSchema/MultipleSchemaTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Overblog\GraphQLBundle\Tests\Functional\MultipleSchema;

use Overblog\GraphQLBundle\Resolver\UnresolvableException;
use Overblog\GraphQLBundle\Tests\Functional\TestCase;

final class MultipleSchemaTest extends TestCase
Expand Down Expand Up @@ -73,11 +74,13 @@ public function testInternalSchema(): void
$this->assertGraphQL($query, $expectedData, null, [], 'internal');
}

public function testUnknownTypeShouldReturnNull(): void
public function testUnknownTypeShouldThrowAnUnresolvableException(): void
{
// @phpstan-ignore-next-line
$schema = $this->getContainer()->get('overblog_graphql.request_executor')->getSchema('public');
$this->assertNull($schema->getType('unknown'));
$this->expectException(UnresolvableException::class);
$this->expectExceptionMessage('Could not find type with alias "unknown". Did you forget to define it?');
$schema->getType('unknown');
}

private function assertSchemaQueryTypeName(string $typeName): void
Expand Down

0 comments on commit ebdcfe0

Please sign in to comment.