Skip to content

Commit 7916c54

Browse files
committed
Merge pull request #37 from mcg-web/execution-result-extensions
Add ExecutionResult $extensions
2 parents 4d024ee + de4eba7 commit 7916c54

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/Executor/ExecutionResult.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ class ExecutionResult
1414
* @var Error[]
1515
*/
1616
public $errors;
17+
18+
/**
19+
* @var array[]
20+
*/
21+
public $extensions;
1722

1823
/**
1924
* @param array $data
2025
* @param array $errors
26+
* @param array $extensions
2127
*/
22-
public function __construct(array $data = null, array $errors = [])
28+
public function __construct(array $data = null, array $errors = [], array $extensions = [])
2329
{
2430
$this->data = $data;
2531
$this->errors = $errors;
32+
$this->extensions = $extensions;
2633
}
2734

2835
/**
@@ -35,6 +42,10 @@ public function toArray()
3542
if (!empty($this->errors)) {
3643
$result['errors'] = array_map(['GraphQL\Error', 'formatError'], $this->errors);
3744
}
45+
46+
if (!empty($this->extensions)) {
47+
$result['extensions'] = (array) $this->extensions;
48+
}
3849

3950
return $result;
4051
}

src/Type/Definition/InterfaceType.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
4949
public static function addImplementationToInterfaces(ObjectType $impl)
5050
{
5151
self::$_lazyLoadImplementations[] = function() use ($impl) {
52+
/** @var self $interface */
5253
foreach ($impl->getInterfaces() as $interface) {
53-
$interface->_implementations[] = $impl;
54+
$interface->addImplementation($impl);
5455
}
5556
};
5657
}
@@ -66,6 +67,16 @@ public static function loadImplementationToInterfaces()
6667
self::$_lazyLoadImplementations = [];
6768
}
6869

70+
/**
71+
* Add a implemented object type to interface
72+
*
73+
* @param ObjectType $impl
74+
*/
75+
protected function addImplementation(ObjectType $impl)
76+
{
77+
$this->_implementations[] = $impl;
78+
}
79+
6980
/**
7081
* InterfaceType constructor.
7182
* @param array $config
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace GraphQL\Tests\Executor;
3+
4+
use GraphQL\Executor\ExecutionResult;
5+
6+
class ExecutionResultTest extends \PHPUnit_Framework_TestCase
7+
{
8+
public function testToArrayWithoutExtensions()
9+
{
10+
$executionResult = new ExecutionResult();
11+
12+
$this->assertEquals(['data' => null], $executionResult->toArray());
13+
}
14+
15+
public function testToArrayExtensions()
16+
{
17+
$executionResult = new ExecutionResult(null, [], ['foo' => 'bar']);
18+
19+
$this->assertEquals(['data' => null, 'extensions' => ['foo' => 'bar']], $executionResult->toArray());
20+
21+
$executionResult->extensions = ['bar' => 'foo'];
22+
23+
$this->assertEquals(['data' => null, 'extensions' => ['bar' => 'foo']], $executionResult->toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)