Class: GraphQL::Execution::Interpreter::Runtime::GraphQLResultHash

Inherits:
Object
  • Object
show all
Includes:
GraphQLResult
Defined in:
lib/graphql/execution/interpreter/runtime/graphql_result.rb

Instance Attribute Summary collapse

Attributes included from GraphQLResult

#ast_node, #base_path, #graphql_application_value, #graphql_arguments, #graphql_dead, #graphql_field, #graphql_is_eager, #graphql_is_non_null_in_parent, #graphql_parent, #graphql_result_data, #graphql_result_name, #graphql_result_type, #graphql_selections

Instance Method Summary collapse

Methods included from GraphQLResult

#build_path, #depth, #path

Constructor Details

#initialize(_result_name, _result_type, _application_value, _parent_result, _is_non_null_in_parent, _selections, _is_eager, _ast_node, _graphql_arguments, graphql_field) ⇒ GraphQLResultHash

rubocop:disable Metrics/ParameterLists



66
67
68
69
70
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 66

def initialize(_result_name, _result_type, _application_value, _parent_result, _is_non_null_in_parent, _selections, _is_eager, _ast_node, _graphql_arguments, graphql_field) # rubocop:disable Metrics/ParameterLists
  super
  @graphql_result_data = {}
  @ordered_result_keys = nil
end

Instance Attribute Details

#graphql_merged_into ⇒ Object

Returns the value of attribute graphql_merged_into.



76
77
78
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 76

def graphql_merged_into
  @graphql_merged_into
end

#ordered_result_keys ⇒ Object

Returns the value of attribute ordered_result_keys.



72
73
74
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 72

def ordered_result_keys
  @ordered_result_keys
end

Instance Method Details

#[](k) ⇒ Object



138
139
140
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 138

def [](k)
  (@graphql_metadata || @graphql_result_data)[k]
end

#collect_result(result_name, result_value) ⇒ Object

hook for breadth-first implementations to signal when collecting results.



174
175
176
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 174

def collect_result(result_name, result_value)
  false
end

#delete(key) ⇒ Object



121
122
123
124
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 121

def delete(key)
  @graphql_metadata && @graphql_metadata.delete(key)
  @graphql_result_data.delete(key)
end

#each ⇒ Object



126
127
128
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 126

def each
  (@graphql_metadata || @graphql_result_data).each { |k, v| yield(k, v) }
end

#fix_result_order ⇒ Object



165
166
167
168
169
170
171
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 165

def fix_result_order
  @ordered_result_keys.each do |k|
    if @graphql_result_data.key?(k)
      @graphql_result_data[k] = @graphql_result_data.delete(k)
    end
  end
end

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 134

def key?(k)
  @graphql_result_data.key?(k)
end

#merge_into(into_result) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 142

def merge_into(into_result)
  self.each do |key, value|
    case value
    when GraphQLResultHash
      next_into = into_result[key]
      if next_into
        value.merge_into(next_into)
      else
        into_result.set_child_result(key, value)
      end
    when GraphQLResultArray
      # There's no special handling of arrays because currently, there's no way to split the execution
      # of a list over several concurrent flows.
      into_result.set_child_result(key, value)
    else
      # We have to assume that, since this passed the `fields_will_merge` selection,
      # that the old and new values are the same.
      into_result.set_leaf(key, value)
    end
  end
  @graphql_merged_into = into_result
end

#set_child_result(key, value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 104

def set_child_result(key, value)
  if (t = @graphql_merged_into)
    t.set_child_result(key, value)
  end
  before_size = @graphql_result_data.size
  @graphql_result_data[key] = value.graphql_result_data
  after_size = @graphql_result_data.size
  if after_size > before_size && @ordered_result_keys[before_size] != key
    fix_result_order
  end

  # If we encounter some part of this response that requires metadata tracking,
  # then create the metadata hash if necessary. It will be kept up-to-date after this.
  (@graphql_metadata ||= @graphql_result_data.dup)[key] = value
  value
end

#set_leaf(key, value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 78

def set_leaf(key, value)
  # This is a hack.
  # Basically, this object is merged into the root-level result at some point.
  # But the problem is, some lazies are created whose closures retain reference to _this_
  # object. When those lazies are resolved, they cause an update to this object.
  #
  # In order to return a proper top-level result, we have to update that top-level result object.
  # In order to return a proper partial result (eg, for a directive), we have to update this object, too.
  # Yowza.
  if (t = @graphql_merged_into)
    t.set_leaf(key, value)
  end

  before_size = @graphql_result_data.size
  @graphql_result_data[key] = value
  after_size = @graphql_result_data.size
  if after_size > before_size && @ordered_result_keys[before_size] != key
    fix_result_order
  end

  # keep this up-to-date if it's been initialized
  @graphql_metadata && @graphql_metadata[key] = value

  value
end

#values ⇒ Object



130
131
132
# File 'lib/graphql/execution/interpreter/runtime/graphql_result.rb', line 130

def values
  (@graphql_metadata || @graphql_result_data).values
end