diff --git a/composer.json b/composer.json index 4f98eac..3e39220 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "psr/simple-cache": "^2.0 || ^3.0", "webonyx/graphql-php": "^15.9", "x-graphql/delegate-execution": "^0.9.0", + "x-graphql/schema-cache": "^0.1.0", "x-graphql/utils": ">=0.2.0" }, "config": { diff --git a/src/SchemaGatewayFactory.php b/src/SchemaGatewayFactory.php index a1ebd14..543d827 100644 --- a/src/SchemaGatewayFactory.php +++ b/src/SchemaGatewayFactory.php @@ -7,19 +7,16 @@ use GraphQL\Error\Error; use GraphQL\Error\SerializationError; use GraphQL\Type\Schema; -use GraphQL\Utils\AST; use GraphQL\Utils\BuildSchema; -use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; use XGraphQL\DelegateExecution\ErrorsReporterInterface; use XGraphQL\DelegateExecution\Execution; +use XGraphQL\SchemaCache\SchemaCache; use XGraphQL\SchemaGateway\AST\ASTBuilder; use XGraphQL\SchemaGateway\Execution\ExecutionDelegator; final class SchemaGatewayFactory { - public const CACHE_KEY = '_x_graphql_ast_schema_gateway'; - /** * @throws Error * @throws \ReflectionException @@ -30,29 +27,23 @@ final class SchemaGatewayFactory public static function create( iterable $subSchemas, iterable $relations = [], - CacheInterface $cache = null, + SchemaCache $cache = null, ErrorsReporterInterface $errorsReporter = null, ): Schema { $subSchemasRegistry = new SubSchemaRegistry($subSchemas); $relationsRegistry = new RelationRegistry($relations); + $schema = $cache?->load(); - if (!$cache?->has(self::CACHE_KEY)) { + if (null === $schema) { $astBuilder = new ASTBuilder($subSchemasRegistry, $relationsRegistry); $ast = $astBuilder->build(); - $astNormalized = AST::toArray($ast); + $schema = BuildSchema::buildAST($ast, options: ['assumeValidSDL' => true]); - $cache?->set(self::CACHE_KEY, $astNormalized); - } else { - $astNormalized = $cache->get(self::CACHE_KEY); - $ast = AST::fromArray($astNormalized); + $cache?->save($schema); } - $schema = BuildSchema::buildAST($ast, options: ['assumeValidSDL' => true]); - $delegator = new ExecutionDelegator($subSchemasRegistry, $relationsRegistry); - Execution::delegate($schema, $delegator, $errorsReporter); - - return $schema; + return Execution::delegate($schema, $delegator, $errorsReporter); } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 68e2726..033e673 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -439,7 +439,8 @@ private function createSchemaGateway(ErrorsReporterInterface $errorsReporter = n 'remoteCountry', RelationOperation::QUERY, 'country', - new class() implements RelationArgumentResolverInterface, + new class() implements + RelationArgumentResolverInterface, MandatorySelectionSetProviderInterface { public function shouldKeep(string $argumentName, Relation $relation): bool { @@ -462,7 +463,8 @@ public function getMandatorySelectionSet(Relation $relation): string 'remoteLanguages', RelationOperation::QUERY, 'languages', - new class() implements RelationArgumentResolverInterface, + new class() implements + RelationArgumentResolverInterface, MandatorySelectionSetProviderInterface { public function shouldKeep(string $argumentName, Relation $relation): bool { @@ -508,7 +510,8 @@ public function resolve(array $objectValue, array $currentArgs, Relation $relati 'remoteCountry', RelationOperation::QUERY, 'country', - new class() implements RelationArgumentResolverInterface, + new class() implements + RelationArgumentResolverInterface, MandatorySelectionSetProviderInterface { public function getMandatorySelectionSet(Relation $relation): string { diff --git a/tests/SchemaGatewayFactoryTest.php b/tests/SchemaGatewayFactoryTest.php index 566d59e..2b204d4 100644 --- a/tests/SchemaGatewayFactoryTest.php +++ b/tests/SchemaGatewayFactoryTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Cache\Psr16Cache; +use XGraphQL\SchemaCache\SchemaCache; use XGraphQL\SchemaGateway\SchemaGatewayFactory; use XGraphQL\SchemaGateway\SubSchema; use XGraphQL\Utils\SchemaPrinter; @@ -24,29 +25,29 @@ public function testCreateWithoutCache() public function testCreateWithCache() { - $arrayAdapter = new ArrayAdapter(); - $psr16 = new Psr16Cache($arrayAdapter); + $arrayCache = new ArrayAdapter(); + $schemaCache = new SchemaCache(new Psr16Cache($arrayCache)); - $this->assertFalse($psr16->has(SchemaGatewayFactory::CACHE_KEY)); + $this->assertEmpty($arrayCache->getValues()); - $schema = SchemaGatewayFactory::create([], cache: $psr16); + $schema = SchemaGatewayFactory::create([], cache: $schemaCache); - $this->assertTrue($psr16->has(SchemaGatewayFactory::CACHE_KEY)); + $this->assertNotEmpty($arrayCache->getValues()); - $schemaCached = SchemaGatewayFactory::create( + $schemaFromCache = SchemaGatewayFactory::create( [ new SubSchema( 'test', BuildSchema::build('type Query { name: String! }'), ) ], - cache: $psr16, + cache: $schemaCache, ); /// new sub schema should not affect cached result - $this->assertEquals(SchemaPrinter::doPrint($schema), SchemaPrinter::doPrint($schemaCached)); + $this->assertEquals(SchemaPrinter::doPrint($schema), SchemaPrinter::doPrint($schemaFromCache)); - $this->assertTrue($psr16->clear()); + $this->assertTrue($arrayCache->clear()); $schemaRebuilt = SchemaGatewayFactory::create( [ @@ -55,7 +56,7 @@ public function testCreateWithCache() BuildSchema::build('type Query { name: String! }'), ) ], - cache: $psr16, + cache: $schemaCache, ); $this->assertNotEquals(SchemaPrinter::doPrint($schema), SchemaPrinter::doPrint($schemaRebuilt));