Skip to content

Commit 8ec90a4

Browse files
authored
Merge pull request #5072 from adampetro/support-variable-definition-directives
Allow variable definition directives in validation
2 parents a794fc8 + 493416a commit 8ec90a4

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ def on_directive(node, parent)
1919
GraphQL::Schema::Directive::FRAGMENT_DEFINITION => "fragment definitions",
2020
GraphQL::Schema::Directive::FRAGMENT_SPREAD => "fragment spreads",
2121
GraphQL::Schema::Directive::INLINE_FRAGMENT => "inline fragments",
22+
GraphQL::Schema::Directive::VARIABLE_DEFINITION => "variable definitions",
2223
}
2324

2425
SIMPLE_LOCATIONS = {
2526
Nodes::Field => GraphQL::Schema::Directive::FIELD,
2627
Nodes::InlineFragment => GraphQL::Schema::Directive::INLINE_FRAGMENT,
2728
Nodes::FragmentSpread => GraphQL::Schema::Directive::FRAGMENT_SPREAD,
2829
Nodes::FragmentDefinition => GraphQL::Schema::Directive::FRAGMENT_DEFINITION,
30+
Nodes::VariableDefinition => GraphQL::Schema::Directive::VARIABLE_DEFINITION,
2931
}
3032

3133
SIMPLE_LOCATION_NODES = SIMPLE_LOCATIONS.keys

spec/graphql/introspection/directive_type_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
"onFragment" => false,
5050
"onOperation" => false,
5151
},
52+
{
53+
"name"=>"directiveForVariableDefinition",
54+
"args"=>[],
55+
"locations"=>["VARIABLE_DEFINITION"],
56+
"isRepeatable"=>false,
57+
"onField"=>false,
58+
"onFragment"=>false,
59+
"onOperation"=>false,
60+
},
5261
{
5362
"name"=>"doStuff",
5463
"args"=>[],

spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,56 @@
33

44
describe GraphQL::StaticValidation::DirectivesAreInValidLocations do
55
include StaticValidationHelpers
6-
let(:query_string) {"
7-
query getCheese @skip(if: true) {
8-
okCheese: cheese(id: 1) {
9-
id @skip(if: true),
10-
source
11-
... on Cheese @skip(if: true) {
12-
flavor
13-
... whatever
6+
7+
describe "invalid directive locations" do
8+
let(:query_string) {"
9+
query getCheese @skip(if: true) {
10+
okCheese: cheese(id: 1) {
11+
id @skip(if: true),
12+
source
13+
... on Cheese @skip(if: true) {
14+
flavor
15+
... whatever
16+
}
1417
}
1518
}
16-
}
17-
18-
fragment whatever on Cheese @skip(if: true) {
19-
id
20-
}
21-
"}
19+
20+
fragment whatever on Cheese @skip(if: true) {
21+
id
22+
}
23+
"}
2224

23-
describe "invalid directive locations" do
2425
it "makes errors for them" do
2526
expected = [
2627
{
2728
"message"=> "'@skip' can't be applied to queries (allowed: fields, fragment spreads, inline fragments)",
28-
"locations"=>[{"line"=>2, "column"=>21}],
29+
"locations"=>[{"line"=>2, "column"=>23}],
2930
"path"=>["query getCheese"],
3031
"extensions"=>{"code"=>"directiveCannotBeApplied", "targetName"=>"queries", "name"=>"skip"}
3132
},
3233
{
3334
"message"=>"'@skip' can't be applied to fragment definitions (allowed: fields, fragment spreads, inline fragments)",
34-
"locations"=>[{"line"=>13, "column"=>33}],
35+
"locations"=>[{"line"=>13, "column"=>35}],
3536
"path"=>["fragment whatever"],
3637
"extensions"=>{"code"=>"directiveCannotBeApplied", "targetName"=>"fragment definitions", "name"=>"skip"}
3738
},
3839
]
3940
assert_equal(expected, errors)
4041
end
4142
end
43+
44+
describe "valid directive locations" do
45+
let(:query_string) {"
46+
query getCheese($id: Int! @directiveForVariableDefinition) {
47+
cheese(id: $id) {
48+
id
49+
}
50+
}
51+
"}
52+
53+
it "does not make errors for them" do
54+
expected = []
55+
assert_equal(expected, errors)
56+
end
57+
end
4258
end

spec/support/dummy/schema.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class BaseInputObject < GraphQL::Schema::InputObject
3333
class BaseScalar < GraphQL::Schema::Scalar
3434
end
3535

36+
class BaseDirective < GraphQL::Schema::Directive
37+
end
38+
3639
module LocalProduct
3740
include BaseInterface
3841
description "Something that comes from somewhere"
@@ -530,6 +533,10 @@ def replace_values(input:)
530533
end
531534
end
532535

536+
class DirectiveForVariableDefinition < BaseDirective
537+
locations(VARIABLE_DEFINITION)
538+
end
539+
533540
class Subscription < BaseObject
534541
field :test, String
535542
def test; "Test"; end
@@ -542,6 +549,7 @@ class Schema < GraphQL::Schema
542549
max_depth 5
543550
orphan_types Honey
544551
trace_with GraphQL::Tracing::CallLegacyTracers
552+
directives(DirectiveForVariableDefinition)
545553

546554
rescue_from(NoSuchDairyError) { |err| raise GraphQL::ExecutionError, err.message }
547555

0 commit comments

Comments
 (0)