Skip to content

Commit 296cc75

Browse files
committed
Added Server class as a new facade for library, including HTTP endpoint compatible with express-graphql format
1 parent ff3a40d commit 296cc75

File tree

3 files changed

+534
-2
lines changed

3 files changed

+534
-2
lines changed

src/Executor/ExecutionResult.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use GraphQL\Error\Error;
55

6-
class ExecutionResult
6+
class ExecutionResult implements \JsonSerializable
77
{
88
/**
99
* @var array
@@ -20,6 +20,11 @@ class ExecutionResult
2020
*/
2121
public $extensions;
2222

23+
/**
24+
* @var callable
25+
*/
26+
private $errorFormatter = ['GraphQL\Error\Error', 'formatError'];
27+
2328
/**
2429
* @param array $data
2530
* @param array $errors
@@ -32,6 +37,16 @@ public function __construct(array $data = null, array $errors = [], array $exten
3237
$this->extensions = $extensions;
3338
}
3439

40+
/**
41+
* @param callable $errorFormatter
42+
* @return $this
43+
*/
44+
public function setErrorFormatter(callable $errorFormatter)
45+
{
46+
$this->errorFormatter = $errorFormatter;
47+
return $this;
48+
}
49+
3550
/**
3651
* @return array
3752
*/
@@ -44,7 +59,7 @@ public function toArray()
4459
}
4560

4661
if (!empty($this->errors)) {
47-
$result['errors'] = array_map(['GraphQL\Error\Error', 'formatError'], $this->errors);
62+
$result['errors'] = array_map($this->errorFormatter, $this->errors);
4863
}
4964

5065
if (!empty($this->extensions)) {
@@ -53,4 +68,9 @@ public function toArray()
5368

5469
return $result;
5570
}
71+
72+
public function jsonSerialize()
73+
{
74+
return $this->toArray();
75+
}
5676
}

0 commit comments

Comments
 (0)