@@ -18,75 +18,79 @@ def __init__(self, context):
18
18
19
19
def enter_OperationDefinition (self , node , * args ):
20
20
operation_name = node .name
21
- if operation_name :
22
- if operation_name .value in self .known_operation_names :
23
- return GraphQLError (
24
- self .message (operation_name .value ),
25
- [self .known_operation_names [operation_name .value ], operation_name ]
26
- )
27
- self .known_operation_names [operation_name .value ] = operation_name
21
+ if not operation_name :
22
+ return
23
+
24
+ if operation_name .value in self .known_operation_names :
25
+ return GraphQLError (
26
+ self .duplicate_operation_name_message (operation_name .value ),
27
+ [self .known_operation_names [operation_name .value ], operation_name ]
28
+ )
29
+
30
+ self .known_operation_names [operation_name .value ] = operation_name
28
31
29
32
@staticmethod
30
- def message (operation_name ):
33
+ def duplicate_operation_name_message (operation_name ):
31
34
return 'There can only be one operation named "{}".' .format (operation_name )
32
35
33
36
34
37
class LoneAnonymousOperation (ValidationRule ):
38
+ operation_count = 0
39
+
35
40
def __init__ (self , context ):
36
41
super (LoneAnonymousOperation , self ).__init__ (context )
37
- self ._op_count = 0
38
42
39
43
def enter_Document (self , node , * args ):
40
- n = 0
41
- for definition in node .definitions :
42
- if isinstance (definition , ast .OperationDefinition ):
43
- n += 1
44
- self ._op_count = n
44
+ self .operation_count = \
45
+ sum (1 for definition in node .definitions if isinstance (definition , ast .OperationDefinition ))
45
46
46
47
def enter_OperationDefinition (self , node , * args ):
47
- if not node .name and self ._op_count > 1 :
48
- return GraphQLError (self .message (), [node ])
48
+ if not node .name and self .operation_count > 1 :
49
+ return GraphQLError (self .anonymous_operation_not_alone_message (), [node ])
49
50
50
51
@staticmethod
51
- def message ():
52
+ def anonymous_operation_not_alone_message ():
52
53
return 'This anonymous operation must be the only defined operation.'
53
54
54
55
55
56
class KnownTypeNames (ValidationRule ):
56
57
def enter_NamedType (self , node , * args ):
57
58
type_name = node .name .value
58
59
type = self .context .get_schema ().get_type (type_name )
60
+
59
61
if not type :
60
- return GraphQLError (self .message (type_name ), [node ])
62
+ return GraphQLError (self .unknown_type_message (type_name ), [node ])
61
63
62
64
@staticmethod
63
- def message (type ):
65
+ def unknown_type_message (type ):
64
66
return 'Unknown type "{}".' .format (type )
65
67
66
68
67
69
class FragmentsOnCompositeTypes (ValidationRule ):
68
70
def enter_InlineFragment (self , node , * args ):
69
71
type = self .context .get_type ()
72
+
70
73
if type and not is_composite_type (type ):
71
74
return GraphQLError (
72
- self .inline_message (print_ast (node .type_condition )),
75
+ self .inline_fragment_on_non_composite_error_message (print_ast (node .type_condition )),
73
76
[node .type_condition ]
74
77
)
75
78
76
79
def enter_FragmentDefinition (self , node , * args ):
77
80
type = self .context .get_type ()
81
+
78
82
if type and not is_composite_type (type ):
79
83
return GraphQLError (
80
- self .message (node .name .value , print_ast (node .type_condition )),
84
+ self .fragment_on_non_composite_error_message (node .name .value , print_ast (node .type_condition )),
81
85
[node .type_condition ]
82
86
)
83
87
84
88
@staticmethod
85
- def inline_message (type ):
89
+ def inline_fragment_on_non_composite_error_message (type ):
86
90
return 'Fragment cannot condition on non composite type "{}".' .format (type )
87
91
88
92
@staticmethod
89
- def message (frag_name , type ):
93
+ def fragment_on_non_composite_error_message (frag_name , type ):
90
94
return 'Fragment "{}" cannot condition on non composite type "{}".' .format (frag_name , type )
91
95
92
96
@@ -95,55 +99,60 @@ def enter_VariableDefinition(self, node, *args):
95
99
type = type_from_ast (self .context .get_schema (), node .type )
96
100
97
101
if type and not is_input_type (type ):
98
- variable_name = node .variable .name .value
99
102
return GraphQLError (
100
- self .message ( variable_name , print_ast (node .type )),
103
+ self .non_input_type_on_variable_message ( node . variable . name . value , print_ast (node .type )),
101
104
[node .type ]
102
105
)
103
106
104
107
@staticmethod
105
- def message (variable_name , type_name ):
108
+ def non_input_type_on_variable_message (variable_name , type_name ):
106
109
return 'Variable "${}" cannot be non-input type "{}".' .format (variable_name , type_name )
107
110
108
111
109
112
class ScalarLeafs (ValidationRule ):
110
113
def enter_Field (self , node , * args ):
111
114
type = self .context .get_type ()
112
- if type :
113
- if is_leaf_type (type ):
114
- if node .selection_set :
115
- return GraphQLError (
116
- self .not_allowed_message (node .name .value , type ),
117
- [node .selection_set ]
118
- )
119
- elif not node .selection_set :
115
+
116
+ if not type :
117
+ return
118
+
119
+ if is_leaf_type (type ):
120
+ if node .selection_set :
120
121
return GraphQLError (
121
- self .required_message (node .name .value , type ),
122
- [node ]
122
+ self .no_subselection_allowed_message (node .name .value , type ),
123
+ [node . selection_set ]
123
124
)
124
125
126
+ elif not node .selection_set :
127
+ return GraphQLError (
128
+ self .required_subselection_message (node .name .value , type ),
129
+ [node ]
130
+ )
131
+
125
132
@staticmethod
126
- def not_allowed_message (field , type ):
133
+ def no_subselection_allowed_message (field , type ):
127
134
return 'Field "{}" of type "{}" must not have a sub selection.' .format (field , type )
128
135
129
136
@staticmethod
130
- def required_message (field , type ):
137
+ def required_subselection_message (field , type ):
131
138
return 'Field "{}" of type "{}" must have a sub selection.' .format (field , type )
132
139
133
140
134
141
class FieldsOnCorrectType (ValidationRule ):
135
142
def enter_Field (self , node , * args ):
136
143
type = self .context .get_parent_type ()
137
- if type :
138
- field_def = self .context .get_field_def ()
139
- if not field_def :
140
- return GraphQLError (
141
- self .message (node .name .value , type .name ),
142
- [node ]
143
- )
144
+ if not type :
145
+ return
146
+
147
+ field_def = self .context .get_field_def ()
148
+ if not field_def :
149
+ return GraphQLError (
150
+ self .undefined_field_message (node .name .value , type .name ),
151
+ [node ]
152
+ )
144
153
145
154
@staticmethod
146
- def message (field_name , type ):
155
+ def undefined_field_message (field_name , type ):
147
156
return 'Cannot query field "{}" on "{}".' .format (field_name , type )
148
157
149
158
@@ -159,17 +168,19 @@ def enter_FragmentDefinition(self, node, *args):
159
168
self .duplicate_fragment_name_message (fragment_name ),
160
169
[self .known_fragment_names [fragment_name ], node .name ]
161
170
)
171
+
162
172
self .known_fragment_names [fragment_name ] = node .name
163
173
164
174
@staticmethod
165
175
def duplicate_fragment_name_message (field ):
166
- return 'There can only be one fragment named {} ' .format (field )
176
+ return 'There can only be one fragment named "{}". ' .format (field )
167
177
168
178
169
179
class KnownFragmentNames (ValidationRule ):
170
180
def enter_FragmentSpread (self , node , * args ):
171
181
fragment_name = node .name .value
172
182
fragment = self .context .get_fragment (fragment_name )
183
+
173
184
if not fragment :
174
185
return GraphQLError (
175
186
self .unknown_fragment_message (fragment_name ),
@@ -310,9 +321,9 @@ def collect_fragment_spread_nodes(self):
310
321
311
322
class NoUndefinedVariables (ValidationRule ):
312
323
visit_spread_fragments = True
324
+ operation = None
313
325
314
326
def __init__ (self , context ):
315
- self .operation = None
316
327
self .visited_fragment_names = set ()
317
328
self .defined_variable_names = set ()
318
329
super (NoUndefinedVariables , self ).__init__ (context )
@@ -409,27 +420,31 @@ def unused_variable_message(variable_name):
409
420
410
421
class KnownDirectives (ValidationRule ):
411
422
def enter_Directive (self , node , key , parent , path , ancestors ):
412
- directive_def = None
413
- for definition in self .context .get_schema ().get_directives ():
414
- if definition .name == node .name .value :
415
- directive_def = definition
416
- break
423
+ directive_def = next ((
424
+ definition for definition in self .context .get_schema ().get_directives ()
425
+ if definition .name == node .name .value
426
+ ), None )
427
+
417
428
if not directive_def :
418
429
return GraphQLError (
419
- self .message (node .name .value ),
430
+ self .unknown_directive_message (node .name .value ),
420
431
[node ]
421
432
)
433
+
422
434
applied_to = ancestors [- 1 ]
435
+
423
436
if isinstance (applied_to , ast .OperationDefinition ) and not directive_def .on_operation :
424
437
return GraphQLError (
425
438
self .misplaced_directive_message (node .name .value , 'operation' ),
426
439
[node ]
427
440
)
441
+
428
442
if isinstance (applied_to , ast .Field ) and not directive_def .on_field :
429
443
return GraphQLError (
430
444
self .misplaced_directive_message (node .name .value , 'field' ),
431
445
[node ]
432
446
)
447
+
433
448
if (isinstance (applied_to , (ast .FragmentSpread , ast .InlineFragment , ast .FragmentDefinition )) and
434
449
not directive_def .on_fragment ):
435
450
return GraphQLError (
@@ -438,7 +453,7 @@ def enter_Directive(self, node, key, parent, path, ancestors):
438
453
)
439
454
440
455
@staticmethod
441
- def message (directive_name ):
456
+ def unknown_directive_message (directive_name ):
442
457
return 'Unknown directive "{}".' .format (directive_name )
443
458
444
459
@staticmethod
@@ -449,41 +464,41 @@ def misplaced_directive_message(directive_name, placement):
449
464
class KnownArgumentNames (ValidationRule ):
450
465
def enter_Argument (self , node , key , parent , path , ancestors ):
451
466
argument_of = ancestors [- 1 ]
467
+
452
468
if isinstance (argument_of , ast .Field ):
453
469
field_def = self .context .get_field_def ()
454
- if field_def :
455
- field_arg_def = None
456
- for arg in field_def . args :
457
- if arg .name == node .name .value :
458
- field_arg_def = arg
459
- break
460
- if not field_arg_def :
461
- parent_type = self . context . get_parent_type ()
462
- assert parent_type
463
- return GraphQLError (
464
- self . message ( node . name . value , field_def . name , parent_type . name ),
465
- [ node ]
466
- )
470
+ if not field_def :
471
+ return
472
+
473
+ field_arg_def = next (( arg for arg in field_def . args if arg .name == node .name .value ), None )
474
+
475
+ if not field_arg_def :
476
+ parent_type = self . context . get_parent_type ()
477
+ assert parent_type
478
+ return GraphQLError (
479
+ self . unknown_arg_message ( node . name . value , field_def . name , parent_type . name ),
480
+ [ node ]
481
+ )
482
+
467
483
elif isinstance (argument_of , ast .Directive ):
468
484
directive = self .context .get_directive ()
469
- if directive :
470
- directive_arg_def = None
471
- for arg in directive .args :
472
- if arg .name == node .name .value :
473
- directive_arg_def = arg
474
- break
475
- if not directive_arg_def :
476
- return GraphQLError (
477
- self .directive_message (node .name .value , directive .name ),
478
- [node ]
479
- )
485
+ if not directive :
486
+ return
487
+
488
+ directive_arg_def = next ((arg for arg in directive .args if arg .name == node .name .value ), None )
489
+
490
+ if not directive_arg_def :
491
+ return GraphQLError (
492
+ self .unknown_directive_arg_message (node .name .value , directive .name ),
493
+ [node ]
494
+ )
480
495
481
496
@staticmethod
482
- def message (arg_name , field_name , type ):
497
+ def unknown_arg_message (arg_name , field_name , type ):
483
498
return 'Unknown argument "{}" on field "{}" of type "{}".' .format (arg_name , field_name , type )
484
499
485
500
@staticmethod
486
- def directive_message (arg_name , directive_name ):
501
+ def unknown_directive_arg_message (arg_name , directive_name ):
487
502
return 'Unknown argument "{}" on directive "@{}".' .format (arg_name , directive_name )
488
503
489
504
@@ -492,24 +507,26 @@ def __init__(self, context):
492
507
super (UniqueArgumentNames , self ).__init__ (context )
493
508
self .known_arg_names = {}
494
509
495
- def enter_Field (self , node , * args ):
510
+ def enter_Field (self , * args ):
496
511
self .known_arg_names = {}
497
512
498
- def enter_Directive (self , node , key , parent , path , ancestors ):
513
+ def enter_Directive (self , * args ):
499
514
self .known_arg_names = {}
500
515
501
516
def enter_Argument (self , node , * args ):
502
517
arg_name = node .name .value
518
+
503
519
if arg_name in self .known_arg_names :
504
520
return GraphQLError (
505
521
self .duplicate_arg_message (arg_name ),
506
522
[self .known_arg_names [arg_name ], node .name ]
507
523
)
524
+
508
525
self .known_arg_names [arg_name ] = node .name
509
526
510
527
@staticmethod
511
528
def duplicate_arg_message (field ):
512
- return 'There can only be one argument named {} ' .format (field )
529
+ return 'There can only be one argument named "{}". ' .format (field )
513
530
514
531
515
532
class ArgumentsOfCorrectType (ValidationRule ):
@@ -528,7 +545,7 @@ def bad_value_message(arg_name, type, value):
528
545
529
546
530
547
class ProvidedNonNullArguments (ValidationRule ):
531
- def leave_Field (self , node , key , parent , path , ancestors ):
548
+ def leave_Field (self , node , * args ):
532
549
field_def = self .context .get_field_def ()
533
550
if not field_def :
534
551
return False
@@ -544,10 +561,11 @@ def leave_Field(self, node, key, parent, path, ancestors):
544
561
self .missing_field_arg_message (node .name .value , arg_def .name , arg_def .type ),
545
562
[node ]
546
563
))
564
+
547
565
if errors :
548
566
return errors
549
567
550
- def leave_Directive (self , node , key , parent , path , ancestors ):
568
+ def leave_Directive (self , node , * args ):
551
569
directive_def = self .context .get_directive ()
552
570
if not directive_def :
553
571
return False
@@ -581,6 +599,7 @@ def enter_VariableDefinition(self, node, *args):
581
599
name = node .variable .name .value
582
600
default_value = node .default_value
583
601
type = self .context .get_input_type ()
602
+
584
603
if isinstance (type , GraphQLNonNull ) and default_value :
585
604
return GraphQLError (
586
605
self .default_for_non_null_arg_message (name , type , type .of_type ),
0 commit comments