Merge pull request #37 from mcg-web/execution-result-extensions

Add ExecutionResult $extensions
This commit is contained in:
Vladimir Razuvaev 2016-04-25 19:55:11 +06:00
commit 7916c54926
3 changed files with 49 additions and 2 deletions

View File

@ -15,14 +15,21 @@ class ExecutionResult
*/
public $errors;
/**
* @var array[]
*/
public $extensions;
/**
* @param array $data
* @param array $errors
* @param array $extensions
*/
public function __construct(array $data = null, array $errors = [])
public function __construct(array $data = null, array $errors = [], array $extensions = [])
{
$this->data = $data;
$this->errors = $errors;
$this->extensions = $extensions;
}
/**
@ -36,6 +43,10 @@ class ExecutionResult
$result['errors'] = array_map(['GraphQL\Error', 'formatError'], $this->errors);
}
if (!empty($this->extensions)) {
$result['extensions'] = (array) $this->extensions;
}
return $result;
}
}

View File

@ -49,8 +49,9 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
public static function addImplementationToInterfaces(ObjectType $impl)
{
self::$_lazyLoadImplementations[] = function() use ($impl) {
/** @var self $interface */
foreach ($impl->getInterfaces() as $interface) {
$interface->_implementations[] = $impl;
$interface->addImplementation($impl);
}
};
}
@ -66,6 +67,16 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
self::$_lazyLoadImplementations = [];
}
/**
* Add a implemented object type to interface
*
* @param ObjectType $impl
*/
protected function addImplementation(ObjectType $impl)
{
$this->_implementations[] = $impl;
}
/**
* InterfaceType constructor.
* @param array $config

View File

@ -0,0 +1,25 @@
<?php
namespace GraphQL\Tests\Executor;
use GraphQL\Executor\ExecutionResult;
class ExecutionResultTest extends \PHPUnit_Framework_TestCase
{
public function testToArrayWithoutExtensions()
{
$executionResult = new ExecutionResult();
$this->assertEquals(['data' => null], $executionResult->toArray());
}
public function testToArrayExtensions()
{
$executionResult = new ExecutionResult(null, [], ['foo' => 'bar']);
$this->assertEquals(['data' => null, 'extensions' => ['foo' => 'bar']], $executionResult->toArray());
$executionResult->extensions = ['bar' => 'foo'];
$this->assertEquals(['data' => null, 'extensions' => ['bar' => 'foo']], $executionResult->toArray());
}
}