Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate with schema cache package #22

Merged
merged 1 commit into from
Apr 3, 2024
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 7 additions & 16 deletions src/SchemaGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
9 changes: 6 additions & 3 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
21 changes: 11 additions & 10 deletions tests/SchemaGatewayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
[
Expand All @@ -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));
Expand Down
Loading