Skip to content

Commit

Permalink
Merge pull request #1338 from tenderlove/immutable-nodes
Browse files Browse the repository at this point in the history
Make language nodes immutable
  • Loading branch information
Robert Mosolgo authored and rmosolgo committed Sep 13, 2018
1 parent 132e274 commit dd52ddc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
11 changes: 7 additions & 4 deletions lib/graphql/language/document_from_schema_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,19 @@ def build_scalar_type_node(scalar_type)
end

def build_argument_node(argument)
if argument.default_value?
default_value = build_default_value(argument.default_value, argument.type)
else
default_value = nil
end

argument_node = GraphQL::Language::Nodes::InputValueDefinition.new(
name: argument.name,
description: argument.description,
type: build_type_name_node(argument.type),
default_value: default_value,
)

if argument.default_value?
argument_node.default_value = build_default_value(argument.default_value, argument.type)
end

argument_node
end

Expand Down
75 changes: 32 additions & 43 deletions lib/graphql/language/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def scalars
end
end

attr_accessor :line, :col, :filename
attr_reader :line, :col, :filename

# Initialize a node by extracting its position,
# then calling the class's `initialize_node` method.
Expand Down Expand Up @@ -69,7 +69,7 @@ def to_query_string(printer: GraphQL::Language::Printer.new)

# Base class for non-null type names and list type names
class WrapperType < AbstractNode
attr_accessor :of_type
attr_reader :of_type

def initialize_node(of_type: nil)
@of_type = of_type
Expand All @@ -82,18 +82,17 @@ def scalars

# Base class for nodes whose only value is a name (no child nodes or other scalars)
class NameOnlyNode < AbstractNode
attr_reader :name
include Scalars::Name

attr_accessor :name

def initialize_node(name: nil)
@name = name
end
end

# A key-value pair for a field's inputs
class Argument < AbstractNode
attr_accessor :name, :value
attr_reader :name, :value

# @!attribute name
# @return [String] the key for this argument
Expand All @@ -116,9 +115,8 @@ def children
end

class Directive < AbstractNode
attr_reader :name, :arguments
include Scalars::Name

attr_accessor :name, :arguments
alias :children :arguments

def initialize_node(name: nil, arguments: [])
Expand All @@ -128,10 +126,9 @@ def initialize_node(name: nil, arguments: [])
end

class DirectiveDefinition < AbstractNode
attr_reader :name, :arguments, :locations, :description
include Scalars::Name

attr_accessor :name, :arguments, :locations, :description

def initialize_node(name: nil, arguments: [], locations: [], description: nil)
@name = name
@arguments = arguments
Expand Down Expand Up @@ -165,7 +162,7 @@ class DirectiveLocation < NameOnlyNode; end
# document.to_query_string(printer: VariableSrubber.new)
#
class Document < AbstractNode
attr_accessor :definitions
attr_reader :definitions
alias :children :definitions

# @!attribute definitions
Expand All @@ -187,7 +184,7 @@ class NullValue < NameOnlyNode; end

# A single selection in a GraphQL query.
class Field < AbstractNode
attr_accessor :name, :alias, :arguments, :directives, :selections
attr_reader :name, :alias, :arguments, :directives, :selections

# @!attribute selections
# @return [Array<Nodes::Field>] Selections on this object (or empty array if this is a scalar field)
Expand All @@ -212,7 +209,7 @@ def children

# A reusable fragment, defined at document-level.
class FragmentDefinition < AbstractNode
attr_accessor :name, :type, :directives, :selections
attr_reader :name, :type, :directives, :selections

# @!attribute name
# @return [String] the identifier for this fragment, which may be applied with `...#{name}`
Expand All @@ -237,9 +234,8 @@ def scalars

# Application of a named fragment in a selection
class FragmentSpread < AbstractNode
attr_reader :name, :directives
include Scalars::Name

attr_accessor :name, :directives
alias :children :directives

# @!attribute name
Expand All @@ -253,7 +249,7 @@ def initialize_node(name: nil, directives: [])

# An unnamed fragment, defined directly in the query with `... { }`
class InlineFragment < AbstractNode
attr_accessor :type, :directives, :selections
attr_reader :type, :directives, :selections

# @!attribute type
# @return [String, nil] Name of the type this fragment applies to, or `nil` if this fragment applies to any type
Expand All @@ -275,7 +271,7 @@ def scalars

# A collection of key-value inputs which may be a field argument
class InputObject < AbstractNode
attr_accessor :arguments
attr_reader :arguments
alias :children :arguments

# @!attribute arguments
Expand Down Expand Up @@ -325,7 +321,7 @@ class NonNullType < WrapperType; end
# May be anonymous or named.
# May be explicitly typed (eg `mutation { ... }`) or implicitly a query (eg `{ ... }`).
class OperationDefinition < AbstractNode
attr_accessor :operation_type, :name, :variables, :directives, :selections
attr_reader :operation_type, :name, :variables, :directives, :selections

# @!attribute variables
# @return [Array<VariableDefinition>] Variable definitions for this operation
Expand Down Expand Up @@ -361,7 +357,7 @@ class TypeName < NameOnlyNode; end

# An operation-level query variable
class VariableDefinition < AbstractNode
attr_accessor :name, :type, :default_value
attr_reader :name, :type, :default_value

# @!attribute default_value
# @return [String, Integer, Float, Boolean, Array, NullValue] A Ruby value to use if no other value is provided
Expand All @@ -387,7 +383,7 @@ def scalars
class VariableIdentifier < NameOnlyNode; end

class SchemaDefinition < AbstractNode
attr_accessor :query, :mutation, :subscription, :directives
attr_reader :query, :mutation, :subscription, :directives

def initialize_node(query: nil, mutation: nil, subscription: nil, directives: [])
@query = query
Expand All @@ -404,7 +400,7 @@ def scalars
end

class SchemaExtension < AbstractNode
attr_accessor :query, :mutation, :subscription, :directives
attr_reader :query, :mutation, :subscription, :directives

def initialize_node(query: nil, mutation: nil, subscription: nil, directives: [])
@query = query
Expand All @@ -421,9 +417,8 @@ def scalars
end

class ScalarTypeDefinition < AbstractNode
attr_reader :name, :directives, :description
include Scalars::Name

attr_accessor :name, :directives, :description
alias :children :directives

def initialize_node(name:, directives: [], description: nil)
Expand All @@ -434,7 +429,7 @@ def initialize_node(name:, directives: [], description: nil)
end

class ScalarTypeExtension < AbstractNode
attr_accessor :name, :directives
attr_reader :name, :directives
alias :children :directives

def initialize_node(name:, directives: [])
Expand All @@ -444,10 +439,9 @@ def initialize_node(name:, directives: [])
end

class ObjectTypeDefinition < AbstractNode
attr_reader :name, :interfaces, :fields, :directives, :description
include Scalars::Name

attr_accessor :name, :interfaces, :fields, :directives, :description

def initialize_node(name:, interfaces:, fields:, directives: [], description: nil)
@name = name
@interfaces = interfaces || []
Expand All @@ -462,7 +456,7 @@ def children
end

class ObjectTypeExtension < AbstractNode
attr_accessor :name, :interfaces, :fields, :directives
attr_reader :name, :interfaces, :fields, :directives

def initialize_node(name:, interfaces:, fields:, directives: [])
@name = name
Expand All @@ -477,7 +471,7 @@ def children
end

class InputValueDefinition < AbstractNode
attr_accessor :name, :type, :default_value, :directives,:description
attr_reader :name, :type, :default_value, :directives,:description
alias :children :directives

def initialize_node(name:, type:, default_value: nil, directives: [], description: nil)
Expand All @@ -494,7 +488,7 @@ def scalars
end

class FieldDefinition < AbstractNode
attr_accessor :name, :arguments, :type, :directives, :description
attr_reader :name, :arguments, :type, :directives, :description

def initialize_node(name:, arguments:, type:, directives: [], description: nil)
@name = name
Expand All @@ -514,10 +508,9 @@ def scalars
end

class InterfaceTypeDefinition < AbstractNode
attr_reader :name, :fields, :directives, :description
include Scalars::Name

attr_accessor :name, :fields, :directives, :description

def initialize_node(name:, fields:, directives: [], description: nil)
@name = name
@fields = fields
Expand All @@ -531,7 +524,7 @@ def children
end

class InterfaceTypeExtension < AbstractNode
attr_accessor :name, :fields, :directives
attr_reader :name, :fields, :directives

def initialize_node(name:, fields:, directives: [])
@name = name
Expand All @@ -545,10 +538,9 @@ def children
end

class UnionTypeDefinition < AbstractNode
attr_reader :name, :types, :directives, :description
include Scalars::Name

attr_accessor :name, :types, :directives, :description


def initialize_node(name:, types:, directives: [], description: nil)
@name = name
@types = types
Expand All @@ -562,7 +554,7 @@ def children
end

class UnionTypeExtension < AbstractNode
attr_accessor :name, :types, :directives
attr_reader :name, :types, :directives

def initialize_node(name:, types:, directives: [])
@name = name
Expand All @@ -576,10 +568,9 @@ def children
end

class EnumTypeDefinition < AbstractNode
attr_reader :name, :values, :directives, :description
include Scalars::Name

attr_accessor :name, :values, :directives, :description

def initialize_node(name:, values:, directives: [], description: nil)
@name = name
@values = values
Expand All @@ -593,7 +584,7 @@ def children
end

class EnumTypeExtension < AbstractNode
attr_accessor :name, :values, :directives
attr_reader :name, :values, :directives

def initialize_node(name:, values:, directives: [])
@name = name
Expand All @@ -607,9 +598,8 @@ def children
end

class EnumValueDefinition < AbstractNode
attr_reader :name, :directives, :description
include Scalars::Name

attr_accessor :name, :directives, :description
alias :children :directives

def initialize_node(name:, directives: [], description: nil)
Expand All @@ -620,10 +610,9 @@ def initialize_node(name:, directives: [], description: nil)
end

class InputObjectTypeDefinition < AbstractNode
attr_reader :name, :fields, :directives, :description
include Scalars::Name

attr_accessor :name, :fields, :directives, :description

def initialize_node(name:, fields:, directives: [], description: nil)
@name = name
@fields = fields
Expand All @@ -637,7 +626,7 @@ def children
end

class InputObjectTypeExtension < AbstractNode
attr_accessor :name, :fields, :directives
attr_reader :name, :fields, :directives

def initialize_node(name:, fields:, directives: [])
@name = name
Expand Down

0 comments on commit dd52ddc

Please sign in to comment.