diff --git a/src/Exception/QueryError.php b/src/Exception/QueryError.php index 0a2e6ee..4288d26 100644 --- a/src/Exception/QueryError.php +++ b/src/Exception/QueryError.php @@ -17,6 +17,14 @@ class QueryError extends RuntimeException * @var array */ protected $errorDetails; + /** + * @var array + */ + protected $data; + /** + * @var array + */ + protected $errors; /** * QueryError constructor. @@ -26,6 +34,11 @@ class QueryError extends RuntimeException public function __construct($errorDetails) { $this->errorDetails = $errorDetails['errors'][0]; + $this->data = []; + if (!empty($errorDetails['data'])) { + $this->data = $errorDetails['data']; + } + $this->errors = $errorDetails['errors']; parent::__construct($this->errorDetails['message']); } @@ -36,4 +49,20 @@ public function getErrorDetails() { return $this->errorDetails; } -} \ No newline at end of file + + /** + * @return array + */ + public function getData() + { + return $this->data; + } + + /** + * @return array + */ + public function getErrors() + { + return $this->errors; + } +} diff --git a/tests/QueryErrorTest.php b/tests/QueryErrorTest.php index 4ee121c..414e58b 100644 --- a/tests/QueryErrorTest.php +++ b/tests/QueryErrorTest.php @@ -47,5 +47,99 @@ public function testConstructQueryError() ], $queryError->getErrorDetails() ); + $this->assertEquals([], $queryError->getData()); } -} \ No newline at end of file + + /** + * @covers \GraphQL\Exception\QueryError::__construct + * @covers \GraphQL\Exception\QueryError::getErrorDetails + */ + public function testConstructQueryErrorWhenResponseHasData() + { + $errorData = [ + 'errors' => [ + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ], + ], + [ + 'message' => 'second error message', + 'location' => [ + [ + 'line' => 2, + 'column' => 4, + ] + ], + ], + ], + 'data' => [ + 'someField' => [ + [ + 'data' => 'value', + ], + [ + 'data' => 'value', + ] + ] + ] + ]; + + $queryError = new QueryError($errorData); + $this->assertEquals('first error message', $queryError->getMessage()); + $this->assertEquals( + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ] + ], + $queryError->getErrorDetails() + ); + + $this->assertEquals( + [ + [ + 'message' => 'first error message', + 'location' => [ + [ + 'line' => 1, + 'column' => 3, + ] + ] + ], + [ + 'message' => 'second error message', + 'location' => [ + [ + 'line' => 2, + 'column' => 4, + ] + ] + ] + ], + $queryError->getErrors() + ); + + $this->assertEquals( + [ + 'someField' => [ + [ + 'data' => 'value', + ], + [ + 'data' => 'value', + ] + ] + ], + $queryError->getData() + ); + } +}