mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 00:25:17 +03:00
Allow constructing GraphQLError with single node.
A common case is encountering an error which blames to a single AST node. Ensure the GraphQLError constructor can handle this case. ref: graphql/graphql-js#1123
This commit is contained in:
parent
b5106a06c9
commit
0c984a83bb
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace GraphQL\Error;
|
||||
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\Source;
|
||||
use GraphQL\Language\SourceLocation;
|
||||
use GraphQL\Utils\Utils;
|
||||
@ -139,7 +140,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array|null $nodes
|
||||
* @param array|Node|null $nodes
|
||||
* @param Source $source
|
||||
* @param array|null $positions
|
||||
* @param array|null $path
|
||||
@ -158,8 +159,11 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
|
||||
{
|
||||
parent::__construct($message, 0, $previous);
|
||||
|
||||
// Compute list of blame nodes.
|
||||
if ($nodes instanceof \Traversable) {
|
||||
$nodes = iterator_to_array($nodes);
|
||||
} else if ($nodes && !is_array($nodes)) {
|
||||
$nodes = [$nodes];
|
||||
}
|
||||
|
||||
$this->nodes = $nodes;
|
||||
|
@ -37,6 +37,24 @@ class ErrorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
||||
}
|
||||
|
||||
/**
|
||||
* @it converts single node to positions and locations
|
||||
*/
|
||||
public function testConvertSingleNodeToPositionsAndLocations()
|
||||
{
|
||||
$source = new Source('{
|
||||
field
|
||||
}');
|
||||
$ast = Parser::parse($source);
|
||||
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
|
||||
$e = new Error('msg', $fieldNode); // Non-array value.
|
||||
|
||||
$this->assertEquals([$fieldNode], $e->nodes);
|
||||
$this->assertEquals($source, $e->getSource());
|
||||
$this->assertEquals([8], $e->getPositions());
|
||||
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
||||
}
|
||||
|
||||
/**
|
||||
* @it converts node with loc.start === 0 to positions and locations
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user