Skip to content

Commit c92dd79

Browse files
authored
Merge pull request #72 from mcg-web/enabled_override_graphql_executor
Enabled override graphql executor
2 parents 73877dd + ef455e8 commit c92dd79

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

Request/Executor.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Overblog\GraphQLBundle\Request;
1313

14+
use GraphQL\Executor\ExecutionResult;
1415
use GraphQL\GraphQL;
1516
use GraphQL\Schema;
1617
use GraphQL\Validator\DocumentValidator;
@@ -24,6 +25,8 @@
2425

2526
class Executor
2627
{
28+
const DEFAULT_EXECUTOR = 'GraphQL\\GraphQL::executeAndReturnResult';
29+
2730
/**
2831
* @var Schema[]
2932
*/
@@ -43,12 +46,35 @@ class Executor
4346
/** @var bool */
4447
private $hasDebugInfo;
4548

46-
public function __construct(EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null, $hasDebugInfo = false)
49+
/**
50+
* @var callable
51+
*/
52+
private $executor;
53+
54+
public function __construct(
55+
EventDispatcherInterface $dispatcher = null,
56+
$throwException = false,
57+
ErrorHandler $errorHandler = null,
58+
$hasDebugInfo = false,
59+
callable $executor = null
60+
)
4761
{
4862
$this->dispatcher = $dispatcher;
4963
$this->throwException = (bool) $throwException;
5064
$this->errorHandler = $errorHandler;
5165
$hasDebugInfo ? $this->enabledDebugInfo() : $this->disabledDebugInfo();
66+
$this->executor = $executor;
67+
if (null === $this->executor) {
68+
$this->executor = self::DEFAULT_EXECUTOR;
69+
}
70+
71+
}
72+
73+
public function setExecutor(callable $executor)
74+
{
75+
$this->executor = $executor;
76+
77+
return $this;
5278
}
5379

5480
public function addSchema($name, Schema $schema)
@@ -116,7 +142,8 @@ public function execute(array $data, array $context = [], $schemaName = null)
116142
$startTime = microtime(true);
117143
$startMemoryUsage = memory_get_usage(true);
118144

119-
$executionResult = GraphQL::executeAndReturnResult(
145+
$executionResult = call_user_func(
146+
$this->executor,
120147
$schema,
121148
isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null,
122149
$context,
@@ -125,6 +152,10 @@ public function execute(array $data, array $context = [], $schemaName = null)
125152
isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null
126153
);
127154

155+
if (!is_object($executionResult) || !$executionResult instanceof ExecutionResult) {
156+
throw new \RuntimeException(sprintf('Execution result should be an object instantiating "%s".', 'GraphQL\\Executor\\ExecutionResult'));
157+
}
158+
128159
if ($this->hasDebugInfo()) {
129160
$executionResult->extensions['debug'] = [
130161
'executionTime' => sprintf('%d ms', round(microtime(true) - $startTime, 1) * 1000),

Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
overblog_graphql.default_resolver: [Overblog\GraphQLBundle\Resolver\Resolver, defaultResolveFn]
33
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\__DEFINITIONS__"
4+
overblog_graphql.default_executor: [GraphQL\GraphQL, executeAndReturnResult]
45

56
services:
67
overblog_graphql.error_handler:
@@ -18,6 +19,7 @@ services:
1819
- "%kernel.debug%"
1920
- "@overblog_graphql.error_handler"
2021
- false
22+
- "%overblog_graphql.default_executor%"
2123
calls:
2224
- ["setMaxQueryComplexity", ["%overblog_graphql.query_max_complexity%"]]
2325
- ["setMaxQueryDepth", ["%overblog_graphql.query_max_depth%"]]

Tests/Request/ExecutorTest.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,47 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
2626
public function setUp()
2727
{
2828
$this->executor = new Executor();
29+
$queryType = new ObjectType([
30+
'name' => 'Query',
31+
'fields' => [
32+
'myField' => [
33+
'type' => Type::boolean(),
34+
'resolve' => function () {
35+
return false;
36+
},
37+
],
38+
],
39+
]);
40+
$this->executor->addSchema('global', new Schema(['query' => $queryType]));
41+
}
42+
43+
/**
44+
* @expectedException \RuntimeException
45+
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult".
46+
*/
47+
public function testInvalidExecutorReturnNotObject()
48+
{
49+
$this->executor->setExecutor(function() { return false; });
50+
$this->executor->execute($this->request);
51+
}
52+
53+
/**
54+
* @expectedException \RuntimeException
55+
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult".
56+
*/
57+
public function testInvalidExecutorReturnInvalidObject()
58+
{
59+
$this->executor->setExecutor(function() { return new \stdClass(); });
60+
$this->executor->execute($this->request);
2961
}
3062

3163
public function testDisabledDebugInfo()
3264
{
33-
$this->addSchema();
3465
$this->assertArrayNotHasKey('debug', $this->executor->disabledDebugInfo()->execute($this->request)->extensions);
3566
}
3667

3768
public function testEnabledDebugInfo()
3869
{
39-
$this->addSchema();
4070
$result = $this->executor->enabledDebugInfo()->execute($this->request);
4171

4272
$this->assertArrayHasKey('debug', $result->extensions);
@@ -50,23 +80,6 @@ public function testEnabledDebugInfo()
5080
*/
5181
public function testGetSchemaNoSchemaFound()
5282
{
53-
$this->executor->getSchema('fake');
54-
}
55-
56-
private function addSchema()
57-
{
58-
$queryType = new ObjectType([
59-
'name' => 'Query',
60-
'fields' => [
61-
'myField' => [
62-
'type' => Type::boolean(),
63-
'resolve' => function () {
64-
return false;
65-
},
66-
],
67-
],
68-
]);
69-
70-
$this->executor->addSchema('global', new Schema(['query' => $queryType]));
83+
(new Executor())->getSchema('fake');
7184
}
7285
}

0 commit comments

Comments
 (0)