Skip to content

Commit 3a77d4b

Browse files
committed
unify internal error handling of parser, validator and executor
1 parent 257a38d commit 3a77d4b

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

src/Execution/Executor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function collectFields(ExecutionContext $executionContext, GraphQLObjectT
6868
case "Field":
6969
// check field
7070
if (!$this->shouldIncludeNode($executionContext, $selection)) {
71-
continue;
71+
break;
7272
}
7373
$name = $this->getFieldEntryKey($selection);
7474
if (!($fields[$name] ?? null)) {
@@ -79,7 +79,7 @@ public function collectFields(ExecutionContext $executionContext, GraphQLObjectT
7979
case "InlineFragment":
8080
if (!$this->shouldIncludeNode($executionContext, $selection)
8181
|| !$this->doesFragmentConditionMatch($executionContext, $selection, $runtimeType)) {
82-
continue;
82+
break;
8383
}
8484
$this->collectFields(
8585
$executionContext,
@@ -92,12 +92,12 @@ public function collectFields(ExecutionContext $executionContext, GraphQLObjectT
9292
case "FragmentSpread":
9393
$fragName = $selection["name"]["value"];
9494
if (array_key_exists($fragName, $visitedFragmentNames) || !$this->shouldIncludeNode($executionContext, $selection)) {
95-
continue;
95+
break;
9696
}
9797
$visitedFragmentNames[$fragName] = true;
9898
$fragment = $executionContext->getFragments()[$fragName];
9999
if (!$fragment || !$this->doesFragmentConditionMatch($executionContext, $fragment, $runtimeType)) {
100-
continue;
100+
break;
101101
}
102102
$this->collectFields(
103103
$executionContext,
@@ -178,7 +178,7 @@ public function executeFields(ExecutionContext $executionContext, GraphQLObjectT
178178
);
179179

180180
if ($result === null) {
181-
return $results;
181+
//return $results;
182182
}
183183

184184
$results[$responseName] = $result;

src/Parser/Parser.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
namespace GraphQL\Parser;
44

55
use GraphQL\Errors\BadUserInputError;
6+
use GraphQL\Errors\GraphQLError;
67
use GraphQL\Errors\UnexpectedEndOfInputError;
78
use GraphQL\Errors\UnexpectedTokenError;
89

910
class Parser
1011
{
1112
private $tokenizer;
1213
private $string = "";
14+
private $document;
1315
private $lookahead;
16+
private $errors;
1417

1518
public function __construct()
1619
{
1720
$this->string = "";
1821
$this->tokenizer = new Tokenizer();
22+
$this->errors = [];
1923
}
2024

2125
/**
@@ -36,8 +40,37 @@ public function parse($string)
3640

3741
$this->lookahead = $this->tokenizer->getNextToken();
3842

39-
// entry point for parsing
40-
return $this->Document();
43+
// try parsing the document -> if it fails, add error to the errors
44+
try{
45+
// entry point for parsing
46+
$this->document = $this->Document();
47+
}catch (GraphQLError $error){
48+
$this->errors[] = $error;
49+
}
50+
}
51+
52+
/**
53+
* @return bool
54+
*/
55+
public function queryIsValid() : bool
56+
{
57+
return count($this->errors) === 0;
58+
}
59+
60+
/**
61+
* @return array
62+
*/
63+
public function getErrors(): array
64+
{
65+
return $this->errors;
66+
}
67+
68+
/**
69+
* @return array
70+
*/
71+
public function getParsedDocument(): array
72+
{
73+
return $this->document;
4174
}
4275

4376
/***

src/Servers/Server.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use GraphQL\Execution\Executor;
66
use GraphQL\Parser\Parser;
77
use GraphQL\Schemas\Schema;
8-
use GraphQL\Errors\GraphQLError;
98
use GraphQL\Errors\InternalServerError;
109
use GraphQL\Utilities\Errors;
1110
use GraphQL\Validation\Rules\ValidationRule;
@@ -68,29 +67,35 @@ public function listen()
6867
} else {
6968
// try to parse the query
7069
try {
71-
// parse the query
72-
$document = $this->parser->parse($query);
70+
// parse query
71+
$this->parser->parse($query);
7372

74-
// validate the query
75-
$this->validator->validate($this->schema, $document);
73+
// check if is valid
74+
if(!$this->parser->queryIsValid()){
75+
// if invalid -> show errors
76+
$this->returnData([
77+
"errors" => Errors::prettyPrintErrors($this->parser->getErrors())
78+
]);
79+
return;
80+
}
81+
82+
// validate query
83+
$this->validator->validate($this->schema, $this->parser->getParsedDocument());
7684

77-
// check if query is valid
85+
// check if is valid
7886
if (!$this->validator->documentIsValid()) {
7987
// if invalid -> show errors
8088
$this->returnData([
8189
"errors" => Errors::prettyPrintErrors($this->validator->getErrors())
8290
]);
83-
} else {
84-
// valid -> execute query
85-
$result = $this->executor->execute($this->schema, $document, null, null, $variables);
86-
$this->returnData($result);
91+
return;
8792
}
8893

89-
} catch (GraphQLError $graphQLError) {
90-
// GraphQLError (while parsing or execution) -> error
91-
$this->returnData([
92-
"errors" => Errors::prettyPrintErrors([$graphQLError])
93-
]);
94+
95+
// execute query
96+
$result = $this->executor->execute($this->schema, $this->parser->getParsedDocument(), null, null, $variables);
97+
$this->returnData($result);
98+
9499
} catch (\Error $error) {
95100
// 500 error -> error
96101
$this->returnData([

src/Validation/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Validator
3838
* Takes a schema and a parsed document and validates them against each other.
3939
* @param Schema $schema
4040
* @param array $document
41-
* @return array
41+
* @return void
4242
*/
43-
public function validate(Schema $schema, array $document)
43+
public function validate(Schema $schema, array $document) : void
4444
{
4545
// create validation context
4646
$validationContext = new ValidationContext($schema, $document);

0 commit comments

Comments
 (0)