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($schema, $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()); + } +}