Class: ElasticGraph::GraphQL::Resolvers::NestedRelationships

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

Overview

Responsible for loading nested relationships that are stored as separate documents in the datastore. We use ‘QuerySource` for the datastore queries to avoid the N+1 query problem (giving us one datastore query per layer of our graph).

Most of the logic for this lives in ElasticGraph::Schema::RelationJoin.

Instance Method Summary collapse

Constructor Details

#initialize(schema_element_names:, logger:) ⇒ NestedRelationships

Returns a new instance of NestedRelationships.



20
21
22
23
# File 'lib/elastic_graph/graphql/resolvers/nested_relationships.rb', line 20

def initialize(schema_element_names:, logger:)
  @schema_element_names = schema_element_names
  @logger = logger
end

Instance Method Details

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/elastic_graph/graphql/resolvers/nested_relationships.rb', line 25

def can_resolve?(field:, object:)
  !!field.relation_join
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elastic_graph/graphql/resolvers/nested_relationships.rb', line 29

def resolve(object:, field:, context:, lookahead:, **)
  log_warning = ->(**options) { log_field_problem_warning(field: field, **options) }
  join = field.relation_join
  id_or_ids = join.extract_id_or_ids_from(object, log_warning)
  filters = [
    build_filter(join.filter_id_field_name, nil, join.foreign_key_nested_paths, Array(id_or_ids)),
    join.additional_filter
  ].reject(&:empty?)
  query = yield.merge_with(filters: filters)

  response =
    case id_or_ids
    when nil, []
      join.blank_value
    else
      initial_response = QuerySource.execute_one(query, for_context: context)
      join.normalize_documents(initial_response) do |problem|
        log_warning.call(document: {"id" => id_or_ids}, problem: "got #{problem} from the datastore search query")
      end
    end

  RelayConnection.maybe_wrap(response, field: field, context: context, lookahead: lookahead, query: query)
end