BuildSchema::build() now accepts DocumentNode as well

This commit is contained in:
Vladimir Razuvaev 2017-08-17 20:33:36 +07:00
parent 03629c1e3c
commit e52fe8c384
2 changed files with 11 additions and 10 deletions

View File

@ -328,7 +328,7 @@ class BuildSchema
if ($this->typeConfigDecorator) {
$fn = $this->typeConfigDecorator;
try {
$config = $fn($this->nodeMap[$typeName], $config, $this->nodeMap);
$config = $fn($config, $this->nodeMap[$typeName], $this->nodeMap);
} catch (\Exception $e) {
throw new Error(
"Type config decorator passed to " . (static::class) . " threw an error ".
@ -608,13 +608,14 @@ class BuildSchema
* A helper function to build a GraphQLSchema directly from a source
* document.
*
* @param Source|string $source
* @param DocumentNode|Source|string $source
* @param callable $typeConfigDecorator
* @return Schema
*/
public static function build($source, callable $typeConfigDecorator = null)
{
return self::buildAST(Parser::parse($source), $typeConfigDecorator);
$doc = $source instanceof DocumentNode ? $source : Parser::parse($source);
return self::buildAST($doc, $typeConfigDecorator);
}
// Count the number of spaces on the starting side of a string.

View File

@ -957,9 +957,9 @@ interface Hello {
$decorated = [];
$calls = [];
$typeConfigDecorator = function($node, $defaultConfig, $allNodesMap) use (&$decorated, &$calls) {
$decorated[] = $node->name->value;
$calls[] = [$node, $defaultConfig, $allNodesMap];
$typeConfigDecorator = function($defaultConfig, $node, $allNodesMap) use (&$decorated, &$calls) {
$decorated[] = $defaultConfig['name'];
$calls[] = [$defaultConfig, $node, $allNodesMap];
return ['description' => 'My description of ' . $node->name->value] + $defaultConfig;
};
@ -967,7 +967,7 @@ interface Hello {
$schema->getTypeMap();
$this->assertEquals(['Query', 'Color', 'Hello'], $decorated);
list($node, $defaultConfig, $allNodesMap) = $calls[0];
list($defaultConfig, $node, $allNodesMap) = $calls[0];
$this->assertInstanceOf(ObjectTypeDefinitionNode::class, $node);
$this->assertEquals('Query', $defaultConfig['name']);
$this->assertInstanceOf(\Closure::class, $defaultConfig['fields']);
@ -978,7 +978,7 @@ interface Hello {
$this->assertEquals('My description of Query', $schema->getType('Query')->description);
list($node, $defaultConfig, $allNodesMap) = $calls[1];
list($defaultConfig, $node, $allNodesMap) = $calls[1];
$this->assertInstanceOf(EnumTypeDefinitionNode::class, $node);
$this->assertEquals('Color', $defaultConfig['name']);
$enumValue = [
@ -994,7 +994,7 @@ interface Hello {
$this->assertEquals(array_keys($allNodesMap), ['Query', 'Color', 'Hello']);
$this->assertEquals('My description of Color', $schema->getType('Color')->description);
list($node, $defaultConfig, $allNodesMap) = $calls[2];
list($defaultConfig, $node, $allNodesMap) = $calls[2];
$this->assertInstanceOf(InterfaceTypeDefinitionNode::class, $node);
$this->assertEquals('Hello', $defaultConfig['name']);
$this->assertInstanceOf(\Closure::class, $defaultConfig['fields']);
@ -1035,7 +1035,7 @@ type World implements Hello {
$doc = Parser::parse($body);
$created = [];
$typeConfigDecorator = function($node, $config) use (&$created) {
$typeConfigDecorator = function($config, $node) use (&$created) {
$created[] = $node->name->value;
return $config;
};