Skip to content

Commit

Permalink
Add support for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavbohac committed May 24, 2024
1 parent b714d23 commit 75b4f32
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/graphql/language/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class DirectiveLocation < NameOnlyNode
end

class DirectiveDefinition < AbstractNode
attr_reader :description
attr_reader :comment, :description
scalar_methods :name, :repeatable
children_methods(
arguments: Nodes::Argument,
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/language/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def definition
advance_token
directive_locations << DirectiveLocation.new(pos: pos, name: parse_name, filename: @filename, source: self)
end
DirectiveDefinition.new(pos: loc, definition_pos: defn_loc, description: desc, name: name, arguments: arguments_definition, locations: directive_locations, repeatable: repeatable, filename: @filename, source: self)
DirectiveDefinition.new(pos: loc, definition_pos: defn_loc, description: desc, comment: comment, name: name, arguments: arguments_definition, locations: directive_locations, repeatable: repeatable, filename: @filename, source: self)
when :TYPE
advance_token
name = parse_name
Expand Down
1 change: 1 addition & 0 deletions lib/graphql/language/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def print_input_object_type_definition(input_object_type, extension: false)

def print_directive_definition(directive)
print_description(directive)
print_comment(directive)
print_string("directive @")
print_string(directive.name)

Expand Down
8 changes: 7 additions & 1 deletion spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
end

describe "string comment" do
it "is parsed for fields, unions, interfaces, enums, enum values, inputs and arguments" do
it "is parsed for fields, unions, interfaces, enums, enum values, inputs, directives and arguments" do
document = subject.parse <<-GRAPHQL
# type comment
type Thing {
Expand Down Expand Up @@ -229,6 +229,9 @@
# Custom input name comment
name: String!
}
# Directive comment
directive @skip(if: Boolean!) on FIELD
GRAPHQL

thing_defn = document.definitions[0]
Expand Down Expand Up @@ -265,6 +268,9 @@
input_field_defn = custom_input_defn.fields[0]
assert_equal "name", input_field_defn.name
assert_equal "Custom input name comment", input_field_defn.comment

custom_directive_defn = document.definitions[6]
assert_equal "Directive comment", custom_directive_defn.comment
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/graphql/language/printer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
"""
Directive description
"""
# Directive comment
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
scalar AnnotatedScalar @onScalar
Expand Down
24 changes: 18 additions & 6 deletions spec/graphql/schema/directive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

describe GraphQL::Schema::Directive do
class MultiWord < GraphQL::Schema::Directive
comment "MultiWord comment"
end

it "uses a downcased class name" do
assert_equal "multiWord", MultiWord.graphql_name
end

it "has a comment" do
assert_equal "MultiWord comment", MultiWord.comment
end

module DirectiveTest
class Secret < GraphQL::Schema::Directive
argument :top_secret, Boolean
Expand Down Expand Up @@ -248,19 +253,26 @@ class Schema < GraphQL::Schema
end

it "runs things twice when they're in with-directive and without-directive parts of the query" do
# TODO: Improve parser to support original inline comment positions
query_str = <<-GRAPHQL
{
t1: thing { name } # name_resolved_count = 1
t2: thing { name } # name_resolved_count = 2
# name_resolved_count = 1
t1: thing { name }
# name_resolved_count = 2
t2: thing { name }
... @countFields {
t1: thing { name } # name_resolved_count = 3
t3: thing { name } # name_resolved_count = 4
# name_resolved_count = 3
t1: thing { name }
# name_resolved_count = 4
t3: thing { name }
}
t3: thing { name } # name_resolved_count = 5
# name_resolved_count = 5
t3: thing { name }
... {
t2: thing { name @countFields } # This is merged back into `t2` above
# This is merged back into `t2` above
t2: thing { name @countFields }
}
}
GRAPHQL
Expand Down

0 comments on commit 75b4f32

Please sign in to comment.