11from functools import partial
22
3+ from graphql .language import parse
34from graphql .utilities import build_schema
4- from graphql .validation import KnownDirectivesRule
5+ from graphql .validation import validate , KnownDirectivesRule
56from graphql .validation .rules .known_directives import (
67 unknown_directive_message , misplaced_directive_message )
78
89from .harness import (
9- expect_fails_rule , expect_passes_rule , expect_sdl_errors_from_rule )
10+ expect_fails_rule , expect_passes_rule , expect_sdl_errors_from_rule ,
11+ test_schema )
1012
1113
1214expect_sdl_errors = partial (
@@ -98,7 +100,7 @@ def with_many_unknown_directives():
98100
99101 def with_well_placed_directives ():
100102 expect_passes_rule (KnownDirectivesRule , """
101- query Foo($var: Boolean @onVariableDefinition ) @onQuery {
103+ query Foo($var: Boolean) @onQuery {
102104 name @include(if: $var)
103105 ...Frag @include(if: true)
104106 skippedField @skip(if: true)
@@ -110,9 +112,21 @@ def with_well_placed_directives():
110112 }
111113 """ )
112114
115+ def with_well_placed_variable_definition_directives ():
116+ # Need to parse with experimental flag
117+ query_string = """
118+ query Foo($var: Boolean @onVariableDefinition) {
119+ name
120+ }
121+ """
122+ errors = validate (test_schema , parse (
123+ query_string , experimental_variable_definition_directives = True ),
124+ [KnownDirectivesRule ])
125+ assert errors == [], 'Should validate'
126+
113127 def with_misplaced_directives ():
114128 expect_fails_rule (KnownDirectivesRule , """
115- query Foo($var: Boolean @onField ) @include(if: true) {
129+ query Foo($var: Boolean) @include(if: true) {
116130 name @onQuery @include(if: $var)
117131 ...Frag @onQuery
118132 }
@@ -121,13 +135,27 @@ def with_misplaced_directives():
121135 someField
122136 }
123137 """ , [
124- misplaced_directive ('onField' , 'variable definition' , 2 , 37 ),
125- misplaced_directive ('include' , 'query' , 2 , 47 ),
138+ misplaced_directive ('include' , 'query' , 2 , 38 ),
126139 misplaced_directive ('onQuery' , 'field' , 3 , 20 ),
127140 misplaced_directive ('onQuery' , 'fragment spread' , 4 , 23 ),
128141 misplaced_directive ('onQuery' , 'mutation' , 7 , 26 ),
129142 ])
130143
144+ def with_misplaced_variable_definition_directives ():
145+ # Need to parse with experimental flag
146+ query_string = """
147+ query Foo($var: Boolean @onField) {
148+ name
149+ }
150+ """
151+ errors = validate (test_schema , parse (
152+ query_string , experimental_variable_definition_directives = True ),
153+ [KnownDirectivesRule ])
154+ expected_errors = [
155+ misplaced_directive ('onField' , 'variable definition' , 2 , 37 )]
156+ assert len (errors ) >= 1 , 'Should not validate'
157+ assert errors == expected_errors
158+
131159 def describe_within_sdl ():
132160
133161 def with_directive_defined_inside_sdl ():
0 commit comments