|
1 | 1 | from pytest import raises # type: ignore |
2 | 2 |
|
| 3 | +from graphql.error import GraphQLError |
3 | 4 | from graphql.language import parse |
4 | | -from graphql.utilities import TypeInfo |
5 | | -from graphql.validation import ASTValidationRule, validate |
| 5 | +from graphql.utilities import TypeInfo, build_schema |
| 6 | +from graphql.validation import ValidationRule, validate |
6 | 7 |
|
7 | 8 | from .harness import test_schema |
8 | 9 |
|
@@ -80,6 +81,36 @@ def validates_using_a_custom_type_info(): |
80 | 81 | " Did you mean 'isHouseTrained'?", |
81 | 82 | ] |
82 | 83 |
|
| 84 | + def validates_using_a_custom_rule(): |
| 85 | + schema = build_schema( |
| 86 | + """ |
| 87 | + directive @custom(arg: String) on FIELD |
| 88 | +
|
| 89 | + type Query { |
| 90 | + foo: String |
| 91 | + } |
| 92 | + """ |
| 93 | + ) |
| 94 | + |
| 95 | + doc = parse( |
| 96 | + """ |
| 97 | + query { |
| 98 | + name @custom |
| 99 | + } |
| 100 | + """ |
| 101 | + ) |
| 102 | + |
| 103 | + class CustomRule(ValidationRule): |
| 104 | + def enter_directive(self, node, *_args): |
| 105 | + directive_def = self.context.get_directive() |
| 106 | + error = GraphQLError("Reporting directive: " + str(directive_def), node) |
| 107 | + self.context.report_error(error) |
| 108 | + |
| 109 | + errors = validate(schema, doc, [CustomRule]) |
| 110 | + assert errors == [ |
| 111 | + {"message": "Reporting directive: @custom", "locations": [(3, 20)]} |
| 112 | + ] |
| 113 | + |
83 | 114 |
|
84 | 115 | def describe_validate_limit_maximum_number_of_validation_errors(): |
85 | 116 | query = """ |
@@ -120,7 +151,7 @@ def when_max_errors_is_less_than_number_of_errors(): |
120 | 151 | ] |
121 | 152 |
|
122 | 153 | def pass_through_exceptions_from_rules(): |
123 | | - class CustomRule(ASTValidationRule): |
| 154 | + class CustomRule(ValidationRule): |
124 | 155 | def enter_field(self, *_args): |
125 | 156 | raise RuntimeError("Error from custom rule!") |
126 | 157 |
|
|
0 commit comments