From 8ea2884db8bdde130fef0c3391deadd5d8810194 Mon Sep 17 00:00:00 2001 From: Minh Vuong Date: Tue, 20 Feb 2024 14:44:12 +0700 Subject: [PATCH 1/2] Revert "ref!: remove schema execution delegator contract" This reverts commit c57b1e7c608a0af888e6d27affa2f6604060b36a. --- {tests => src}/SchemaExecutionDelegator.php | 10 +++- src/SchemaExecutionDelegatorInterface.php | 15 +++++ tests/ExecutionTest.php | 1 + tests/SchemaExecutionDelegatorTest.php | 62 +++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) rename {tests => src}/SchemaExecutionDelegator.php (85%) create mode 100644 src/SchemaExecutionDelegatorInterface.php create mode 100644 tests/SchemaExecutionDelegatorTest.php diff --git a/tests/SchemaExecutionDelegator.php b/src/SchemaExecutionDelegator.php similarity index 85% rename from tests/SchemaExecutionDelegator.php rename to src/SchemaExecutionDelegator.php index 36f8fab..1b56269 100644 --- a/tests/SchemaExecutionDelegator.php +++ b/src/SchemaExecutionDelegator.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace XGraphQL\DelegateExecution\Test; +namespace XGraphQL\DelegateExecution; use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter; use GraphQL\Executor\Promise\Promise; @@ -12,9 +12,8 @@ use GraphQL\Language\AST\NodeList; use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Type\Schema; -use XGraphQL\DelegateExecution\ExecutionDelegatorInterface; -final readonly class SchemaExecutionDelegator implements ExecutionDelegatorInterface +final readonly class SchemaExecutionDelegator implements SchemaExecutionDelegatorInterface { private PromiseAdapter $promiseAdapter; @@ -41,6 +40,11 @@ public function delegate(Schema $executionSchema, OperationDefinitionNode $opera ); } + public function getSchema(): Schema + { + return $this->schema; + } + public function getPromiseAdapter(): PromiseAdapter { return $this->promiseAdapter; diff --git a/src/SchemaExecutionDelegatorInterface.php b/src/SchemaExecutionDelegatorInterface.php new file mode 100644 index 0000000..c651096 --- /dev/null +++ b/src/SchemaExecutionDelegatorInterface.php @@ -0,0 +1,15 @@ +createStub(Schema::class); + $instance = new SchemaExecutionDelegator($schema); + + $this->assertInstanceOf(SchemaExecutionDelegator::class, $instance); + $this->assertInstanceOf(SyncPromiseAdapter::class, $instance->getPromiseAdapter()); + $this->assertEquals($schema, $instance->getSchema()); + } + + public function testCanDelegateQuery() + { + $schema = $this->createDummySchema(); + $adapter = new SyncPromiseAdapter(); + $instance = new SchemaExecutionDelegator($schema, $adapter); + $operation = Parser::operationDefinition( + <<<'GQL' +query test($include: Boolean!) { + a: dummy @include(if: $include) + ...b +} +GQL + ); + $fragment = Parser::fragmentDefinition( + <<<'GQL' +fragment b on Query { + b: dummy_error +} +GQL + ); + + $promise = $instance->delegate($operation, [$fragment], ['include' => true]); + + $this->assertInstanceOf(Promise::class, $promise); + + $result = $adapter->wait($promise); + + $this->assertInstanceOf(ExecutionResult::class, $result); + $this->assertEquals(['a' => 'dummy', 'b' => null], $result->data); + $this->assertCount(1, $result->errors); + $this->assertEquals(['b'], $result->errors[0]->getPath()); + $this->assertEquals('Dummy error', $result->errors[0]->getMessage()); + } +} From 180f85bfa9ed0936aaab71b1c954d55858f271d9 Mon Sep 17 00:00:00 2001 From: Minh Vuong Date: Tue, 20 Feb 2024 14:45:42 +0700 Subject: [PATCH 2/2] fix: test --- tests/SchemaExecutionDelegatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SchemaExecutionDelegatorTest.php b/tests/SchemaExecutionDelegatorTest.php index ea104b6..2c324cb 100644 --- a/tests/SchemaExecutionDelegatorTest.php +++ b/tests/SchemaExecutionDelegatorTest.php @@ -47,7 +47,7 @@ public function testCanDelegateQuery() GQL ); - $promise = $instance->delegate($operation, [$fragment], ['include' => true]); + $promise = $instance->delegate($schema, $operation, [$fragment], ['include' => true]); $this->assertInstanceOf(Promise::class, $promise);