Class: ElasticGraph::GraphQL::Resolvers::GetRecordFieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/resolvers/get_record_field_value.rb

Overview

Responsible for fetching a single field value from a document.

Instance Method Summary collapse

Constructor Details

#initialize(schema_element_names:) ⇒ GetRecordFieldValue

Returns a new instance of GetRecordFieldValue.



18
19
20
# File 'lib/elastic_graph/graphql/resolvers/get_record_field_value.rb', line 18

def initialize(schema_element_names:)
  @schema_element_names = schema_element_names
end

Instance Method Details

#can_resolve?(field:, object:) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/elastic_graph/graphql/resolvers/get_record_field_value.rb', line 22

def can_resolve?(field:, object:)
  object.is_a?(DatastoreResponse::Document) || object.is_a?(::Hash)
end

#resolve(field:, object:, args:, context:, lookahead:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elastic_graph/graphql/resolvers/get_record_field_value.rb', line 26

def resolve(field:, object:, args:, context:, lookahead:)
  field_name = field.name_in_index.to_s
  data =
    case object
    when DatastoreResponse::Document
      object.payload
    else
      object
    end

  value = Support::HashUtil.fetch_value_at_path(data, field_name) do
    field.type.list? ? [] : nil
  end

  if field.type.relay_connection?
    RelayConnection::ArrayAdapter.build(value, args, @schema_element_names, context)
  else
    value
  end
end