Skip to content

Commit

Permalink
Also freeze children arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 13, 2018
1 parent a27a807 commit bc940ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
8 changes: 2 additions & 6 deletions lib/graphql/compatibility/schema_parser_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,31 +595,27 @@ def test_it_parses_whole_definition_with_descriptions

assert_equal 6, document.definitions.size

schema_definition = document.definitions.shift
schema_definition, directive_definition, enum_type_definition, object_type_definition, input_object_type_definition, interface_type_definition = document.definitions

assert_equal GraphQL::Language::Nodes::SchemaDefinition, schema_definition.class

directive_definition = document.definitions.shift
assert_equal GraphQL::Language::Nodes::DirectiveDefinition, directive_definition.class
assert_equal 'This is a directive', directive_definition.description

enum_type_definition = document.definitions.shift
assert_equal GraphQL::Language::Nodes::EnumTypeDefinition, enum_type_definition.class
assert_equal "Multiline comment\n\nWith an enum", enum_type_definition.description

assert_nil enum_type_definition.values[0].description
assert_equal 'Not a creative color', enum_type_definition.values[1].description

object_type_definition = document.definitions.shift
assert_equal GraphQL::Language::Nodes::ObjectTypeDefinition, object_type_definition.class
assert_equal 'Comment without preceding space', object_type_definition.description
assert_equal 'And a field to boot', object_type_definition.fields[0].description

input_object_type_definition = document.definitions.shift
assert_equal GraphQL::Language::Nodes::InputObjectTypeDefinition, input_object_type_definition.class
assert_equal 'Comment for input object types', input_object_type_definition.description
assert_equal 'Color of the car', input_object_type_definition.fields[0].description

interface_type_definition = document.definitions.shift
assert_equal GraphQL::Language::Nodes::InterfaceTypeDefinition, interface_type_definition.class
assert_equal 'Comment for interface definitions', interface_type_definition.description
assert_equal 'Amount of wheels', interface_type_definition.fields[0].description
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql/language/document_from_schema_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def build_field_node(field)
)

if field.deprecation_reason
field_node.directives << GraphQL::Language::Nodes::Directive.new(
field_node = field_node.merge_directive(
name: GraphQL::Directive::DeprecatedDirective.name,
arguments: [GraphQL::Language::Nodes::Argument.new(name: "reason", value: field.deprecation_reason)]
)
Expand Down Expand Up @@ -107,7 +107,7 @@ def build_enum_value_node(enum_value)
)

if enum_value.deprecation_reason
enum_value_node.directives << GraphQL::Language::Nodes::Directive.new(
enum_value_node = enum_value_node.merge_directive(
name: GraphQL::Directive::DeprecatedDirective.name,
arguments: [GraphQL::Language::Nodes::Argument.new(name: "reason", value: enum_value.deprecation_reason)]
)
Expand Down
8 changes: 5 additions & 3 deletions lib/graphql/language/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def merge_#{method_name.to_s.sub(/s$/, "")}(node_opts)
else
module_eval <<-RUBY, __FILE__, __LINE__
def children
@children ||= #{children_of_type.keys.map { |k| "@#{k}" }.join(" + ")}
@children ||= (#{children_of_type.keys.map { |k| "@#{k}" }.join(" + ")}).freeze
end
RUBY
end
Expand Down Expand Up @@ -200,7 +200,7 @@ def scalar_methods(*method_names)
attr_reader #{method_names.map { |m| ":#{m}"}.join(", ")}
def scalars
@scalars ||= [#{method_names.map { |k| "@#{k}" }.join(", ")}]
@scalars ||= [#{method_names.map { |k| "@#{k}" }.join(", ")}].freeze
end
RUBY
end
Expand All @@ -224,7 +224,9 @@ def generate_initialize_node
arguments = scalar_method_names.map { |m| "#{m}: nil"} +
@children_methods.keys.map { |m| "#{m}: []" }

assignments = all_method_names.map { |m| "@#{m} = #{m}"}
assignments = scalar_method_names.map { |m| "@#{m} = #{m}"} +
@children_methods.keys.map { |m| "@#{m} = #{m}.freeze" }

module_eval <<-RUBY, __FILE__, __LINE__
def initialize_node #{arguments.join(", ")}
#{assignments.join("\n")}
Expand Down

0 comments on commit bc940ba

Please sign in to comment.