Skip to content

Commit

Permalink
Merge pull request #6 from x-graphql/revert-remove-schema-execution-d…
Browse files Browse the repository at this point in the history
…elegator

Revert remove schema execution delegator
  • Loading branch information
vuongxuongminh authored Feb 20, 2024
2 parents 790adcb + 180f85b commit deac87c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/SchemaExecutionDelegatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace XGraphQL\DelegateExecution;

use GraphQL\Type\Schema;

interface SchemaExecutionDelegatorInterface extends ExecutionDelegatorInterface
{
/**
* @return Schema used to delegate query to execute
*/
public function getSchema(): Schema;
}
1 change: 1 addition & 0 deletions tests/ExecutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use XGraphQL\DelegateExecution\Execution;
use XGraphQL\DelegateExecution\ExecutionDelegatorInterface;
use XGraphQL\DelegateExecution\RootFieldsResolver;
use XGraphQL\DelegateExecution\SchemaExecutionDelegator;

class ExecutionTest extends TestCase
{
Expand Down
62 changes: 62 additions & 0 deletions tests/SchemaExecutionDelegatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace XGraphQL\DelegateExecution\Test;

use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Language\Parser;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use XGraphQL\DelegateExecution\SchemaExecutionDelegator;

class SchemaExecutionDelegatorTest extends TestCase
{
use DummySchemaTrait;

public function testConstructor(): void
{
$schema = $this->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());
}
}

0 comments on commit deac87c

Please sign in to comment.