From 0c984a83bb61584e9379f3b9142dde1dbf8cbb45 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 11 Feb 2018 21:18:54 +0100 Subject: [PATCH] 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 --- src/Error/Error.php | 6 +++++- tests/ErrorTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Error/Error.php b/src/Error/Error.php index ccbd323..06a1d12 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -1,6 +1,7 @@ nodes = $nodes; diff --git a/tests/ErrorTest.php b/tests/ErrorTest.php index 8a39970..fc52609 100644 --- a/tests/ErrorTest.php +++ b/tests/ErrorTest.php @@ -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 */