Class: GraphQL::StaticValidation::FieldsWillMergeError

Inherits:
StaticValidation::Error
  • Object
show all
Defined in:
lib/graphql/static_validation/rules/fields_will_merge_error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, field_name:) ⇒ FieldsWillMergeError

: (kind: Symbol, field_name: String) -> void



13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 13

def initialize(kind:, field_name:)
  super(nil)

  @field_name = field_name #: String
  @kind = kind #: Symbol
  @conflicts = [] #: Array[String]
  # Indexes of `@nodes`, so that `add_conflict` doesn't compare `node` with each one
  @node_ids = {}.compare_by_identity #: Hash[GraphQL::Language::Nodes::AbstractNode, bool]
  @nodes_by_location = nil #: Hash[Integer?, Hash[Integer?, Array[GraphQL::Language::Nodes::AbstractNode]]]?
end

Instance Attribute Details

#field_name ⇒ Object (readonly)

: String



8
9
10
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 8

def field_name
  @field_name
end

#kind ⇒ Object (readonly)

: Symbol



10
11
12
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 10

def kind
  @kind
end

#message ⇒ Object

: -> String



25
26
27
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 25

def message
  @message || "Field '#{field_name}' has #{kind == :argument ? 'an' : 'a'} #{kind} conflict: #{conflicts}?"
end

Instance Method Details

#add_conflict(node, conflict_str) ⇒ Object

: (GraphQL::Language::Nodes::Field node, String conflict_str) -> void



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 43

def add_conflict(node, conflict_str)
  # Check if we already have an error for this exact node.
  # Use object identity first (fast path), then fall back to
  # value + location comparison for duplicate AST nodes.
  return if @node_ids.key?(node)

  nodes_at_line = (@nodes_by_location ||= {})[node.line] ||= {}
  nodes_at_location = nodes_at_line[node.col]
  if nodes_at_location
    # already have an error for this node
    return if nodes_at_location.any? { |n| n == node }
    nodes_at_location << node
  else
    nodes_at_line[node.col] = [node]
  end

  @node_ids[node] = true
  @nodes << node
  @conflicts << conflict_str
end

#code ⇒ Object

: -> String



79
80
81
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 79

def code
  "fieldConflict"
end

#conflicts ⇒ Object

: -> String



38
39
40
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 38

def conflicts
  @conflicts.join(' or ')
end

#path ⇒ Object

: -> Array[String | Integer]



33
34
35
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 33

def path
  []
end

#to_h ⇒ Object

A hash representation of this Message : -> Hash[String, untyped]



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/graphql/static_validation/rules/fields_will_merge_error.rb', line 66

def to_h
  extensions = {
    "code" => code,
    "fieldName" => field_name,
    "conflicts" => conflicts
  }

  super.merge({
    "extensions" => extensions
  })
end