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) { if ($this->typeConfigDecorator) {
$fn = $this->typeConfigDecorator; $fn = $this->typeConfigDecorator;
try { try {
$config = $fn($this->nodeMap[$typeName], $config, $this->nodeMap); $config = $fn($config, $this->nodeMap[$typeName], $this->nodeMap);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new Error( throw new Error(
"Type config decorator passed to " . (static::class) . " threw an 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 * A helper function to build a GraphQLSchema directly from a source
* document. * document.
* *
* @param Source|string $source * @param DocumentNode|Source|string $source
* @param callable $typeConfigDecorator * @param callable $typeConfigDecorator
* @return Schema * @return Schema
*/ */
public static function build($source, callable $typeConfigDecorator = null) 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. // Count the number of spaces on the starting side of a string.

View File

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