|
2 | 2 | from graphql import build_schema as schema |
3 | 3 |
|
4 | 4 | from schemadiff.changes import Change, Criticality |
| 5 | +from schemadiff.changes.field import FieldArgumentAdded |
| 6 | +from schemadiff.changes.object import ObjectTypeFieldAdded |
5 | 7 | from schemadiff.validation import evaluate_rules, ValidationResult, ValidationError |
6 | 8 | from schemadiff.validation_rules import ( |
7 | 9 | ValidationRule, |
@@ -234,6 +236,30 @@ def test_schema_added_field_no_desc(): |
234 | 236 | def test_cant_create_mutation_with_more_than_10_arguments(): |
235 | 237 | schema_restrictions = ['field-has-too-many-arguments'] |
236 | 238 |
|
| 239 | + class FieldHasTooManyArguments(ValidationRule): |
| 240 | + """Restrict adding fields with too many top level arguments""" |
| 241 | + |
| 242 | + name = "field-has-too-many-arguments" |
| 243 | + limit = 10 |
| 244 | + |
| 245 | + def is_valid(self) -> bool: |
| 246 | + if not isinstance(self.change, (ObjectTypeFieldAdded, FieldArgumentAdded)): |
| 247 | + return True |
| 248 | + |
| 249 | + if len(self.args) > self.limit: |
| 250 | + return False |
| 251 | + else: |
| 252 | + return True |
| 253 | + |
| 254 | + @property |
| 255 | + def args(self): |
| 256 | + return self.change.field.args or {} |
| 257 | + |
| 258 | + @property |
| 259 | + def message(self): |
| 260 | + return f"Field `{self.change.parent.name}.{self.change.field_name}` has too many arguments " \ |
| 261 | + f"({len(self.args)}>{self.limit}). Rule: {self.name}" |
| 262 | + |
237 | 263 | old_schema = schema(""" |
238 | 264 | schema { |
239 | 265 | mutation: Mutation |
@@ -274,6 +300,30 @@ def test_cant_create_mutation_with_more_than_10_arguments(): |
274 | 300 | def test_cant_add_arguments_to_mutation_if_exceeds_10_args(): |
275 | 301 | schema_restrictions = ['field-has-too-many-arguments'] |
276 | 302 |
|
| 303 | + class FieldHasTooManyArguments(ValidationRule): |
| 304 | + """Restrict adding fields with too many top level arguments""" |
| 305 | + |
| 306 | + name = "field-has-too-many-arguments" |
| 307 | + limit = 10 |
| 308 | + |
| 309 | + def is_valid(self) -> bool: |
| 310 | + if not isinstance(self.change, (ObjectTypeFieldAdded, FieldArgumentAdded)): |
| 311 | + return True |
| 312 | + |
| 313 | + if len(self.args) > self.limit: |
| 314 | + return False |
| 315 | + else: |
| 316 | + return True |
| 317 | + |
| 318 | + @property |
| 319 | + def args(self): |
| 320 | + return self.change.field.args or {} |
| 321 | + |
| 322 | + @property |
| 323 | + def message(self): |
| 324 | + return f"Field `{self.change.parent.name}.{self.change.field_name}` has too many arguments " \ |
| 325 | + f"({len(self.args)}>{self.limit}). Rule: {self.name}" |
| 326 | + |
277 | 327 | old_schema = schema(""" |
278 | 328 | schema { |
279 | 329 | mutation: Mutation |
|
0 commit comments