Skip to content

Commit 891d4a6

Browse files
committed
[Validation] Report errors rather than return them
Related GraphQL-js commit graphql/graphql-js@5e545cc
1 parent ccf167c commit 891d4a6

27 files changed

+104
-112
lines changed

graphql/core/validation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def visit_using_rules(schema, type_info, ast, rules):
1919
errors = []
2020
rules = [rule(context) for rule in rules]
2121
visit(ast, ValidationVisitor(rules, context, type_info, errors))
22-
return errors
22+
return context.get_errors()

graphql/core/validation/context.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,25 @@ def leave(self, node, key, parent, path, ancestors):
3131

3232

3333
class ValidationContext(object):
34-
__slots__ = '_schema', '_ast', '_type_info', '_fragments', '_fragment_spreads', '_recursively_referenced_fragments', '_variable_usages', '_recursive_variable_usages'
34+
__slots__ = '_schema', '_ast', '_type_info', '_errors', '_fragments', '_fragment_spreads', '_recursively_referenced_fragments', '_variable_usages', '_recursive_variable_usages'
3535

3636
def __init__(self, schema, ast, type_info):
3737
self._schema = schema
3838
self._ast = ast
3939
self._type_info = type_info
40+
self._errors = []
4041
self._fragments = None
4142
self._fragment_spreads = {}
4243
self._recursively_referenced_fragments = {}
4344
self._variable_usages = {}
4445
self._recursive_variable_usages = {}
4546

47+
def report_error(self, error):
48+
self._errors.append(error)
49+
50+
def get_errors(self):
51+
return self._errors
52+
4653
def get_schema(self):
4754
return self._schema
4855

graphql/core/validation/rules/arguments_of_correct_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def enter_Argument(self, node, key, parent, path, ancestors):
1010
if arg_def:
1111
errors = is_valid_literal_value(arg_def.type, node.value)
1212
if errors:
13-
return GraphQLError(
13+
self.context.report_error(GraphQLError(
1414
self.bad_value_message(node.name.value, arg_def.type,
1515
print_ast(node.value), errors),
1616
[node.value]
17-
)
17+
))
1818
return False
1919

2020
@staticmethod

graphql/core/validation/rules/default_values_of_correct_type.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ def enter_VariableDefinition(self, node, key, parent, path, ancestors):
1212
type = self.context.get_input_type()
1313

1414
if isinstance(type, GraphQLNonNull) and default_value:
15-
return GraphQLError(
15+
self.context.report_error(GraphQLError(
1616
self.default_for_non_null_arg_message(name, type, type.of_type),
1717
[default_value]
18-
)
18+
))
1919

2020
if type and default_value:
2121
errors = is_valid_literal_value(type, default_value)
2222
if errors:
23-
return GraphQLError(
23+
self.context.report_error(GraphQLError(
2424
self.bad_value_for_default_arg_message(name, type, print_ast(default_value), errors),
2525
[default_value]
26-
)
26+
))
2727
return False
2828

2929
def enter_SelectionSet(self, node, key, parent, path, ancestors):

graphql/core/validation/rules/fields_on_correct_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def enter_Field(self, node, key, parent, path, ancestors):
1010

1111
field_def = self.context.get_field_def()
1212
if not field_def:
13-
return GraphQLError(
13+
self.context.report_error(GraphQLError(
1414
self.undefined_field_message(node.name.value, type.name),
1515
[node]
16-
)
16+
))
1717

1818
@staticmethod
1919
def undefined_field_message(field_name, type):

graphql/core/validation/rules/fragments_on_composite_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ def enter_InlineFragment(self, node, key, parent, path, ancestors):
99
type = self.context.get_type()
1010

1111
if node.type_condition and type and not is_composite_type(type):
12-
return GraphQLError(
12+
self.context.report_error(GraphQLError(
1313
self.inline_fragment_on_non_composite_error_message(print_ast(node.type_condition)),
1414
[node.type_condition]
15-
)
15+
))
1616

1717
def enter_FragmentDefinition(self, node, key, parent, path, ancestors):
1818
type = self.context.get_type()
1919

2020
if type and not is_composite_type(type):
21-
return GraphQLError(
21+
self.context.report_error(GraphQLError(
2222
self.fragment_on_non_composite_error_message(node.name.value, print_ast(node.type_condition)),
2323
[node.type_condition]
24-
)
24+
))
2525

2626
@staticmethod
2727
def inline_fragment_on_non_composite_error_message(type):

graphql/core/validation/rules/known_argument_names.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ def enter_Argument(self, node, key, parent, path, ancestors):
1717
if not field_arg_def:
1818
parent_type = self.context.get_parent_type()
1919
assert parent_type
20-
return GraphQLError(
20+
self.context.report_error(GraphQLError(
2121
self.unknown_arg_message(node.name.value, field_def.name, parent_type.name),
2222
[node]
23-
)
23+
))
2424

2525
elif isinstance(argument_of, ast.Directive):
2626
directive = self.context.get_directive()
@@ -30,10 +30,10 @@ def enter_Argument(self, node, key, parent, path, ancestors):
3030
directive_arg_def = next((arg for arg in directive.args if arg.name == node.name.value), None)
3131

3232
if not directive_arg_def:
33-
return GraphQLError(
33+
self.context.report_error(GraphQLError(
3434
self.unknown_directive_arg_message(node.name.value, directive.name),
3535
[node]
36-
)
36+
))
3737

3838
@staticmethod
3939
def unknown_arg_message(arg_name, field_name, type):

graphql/core/validation/rules/known_directives.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ def enter_Directive(self, node, key, parent, path, ancestors):
1111
), None)
1212

1313
if not directive_def:
14-
return GraphQLError(
14+
return self.context.report_error(GraphQLError(
1515
self.unknown_directive_message(node.name.value),
1616
[node]
17-
)
17+
))
1818

1919
applied_to = ancestors[-1]
2020

2121
if isinstance(applied_to, ast.OperationDefinition) and not directive_def.on_operation:
22-
return GraphQLError(
22+
self.context.report_error(GraphQLError(
2323
self.misplaced_directive_message(node.name.value, 'operation'),
2424
[node]
25-
)
25+
))
2626

27-
if isinstance(applied_to, ast.Field) and not directive_def.on_field:
28-
return GraphQLError(
27+
elif isinstance(applied_to, ast.Field) and not directive_def.on_field:
28+
self.context.report_error(GraphQLError(
2929
self.misplaced_directive_message(node.name.value, 'field'),
3030
[node]
31-
)
31+
))
3232

33-
if (isinstance(applied_to, (ast.FragmentSpread, ast.InlineFragment, ast.FragmentDefinition)) and
33+
elif (isinstance(applied_to, (ast.FragmentSpread, ast.InlineFragment, ast.FragmentDefinition)) and
3434
not directive_def.on_fragment):
35-
return GraphQLError(
35+
self.context.report_error(GraphQLError(
3636
self.misplaced_directive_message(node.name.value, 'fragment'),
3737
[node]
38-
)
38+
))
3939

4040
@staticmethod
4141
def unknown_directive_message(directive_name):

graphql/core/validation/rules/known_fragment_names.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def enter_FragmentSpread(self, node, key, parent, path, ancestors):
88
fragment = self.context.get_fragment(fragment_name)
99

1010
if not fragment:
11-
return GraphQLError(
11+
self.context.report_error(GraphQLError(
1212
self.unknown_fragment_message(fragment_name),
1313
[node.name]
14-
)
14+
))
1515

1616
@staticmethod
1717
def unknown_fragment_message(fragment_name):

graphql/core/validation/rules/known_type_names.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def enter_NamedType(self, node, *args):
88
type = self.context.get_schema().get_type(type_name)
99

1010
if not type:
11-
return GraphQLError(self.unknown_type_message(type_name), [node])
11+
self.context.report_error(GraphQLError(self.unknown_type_message(type_name), [node]))
1212

1313
@staticmethod
1414
def unknown_type_message(type):

0 commit comments

Comments
 (0)