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(elasticgraph_graphql:, config:) ⇒ NestedRelationships

Returns a new instance of NestedRelationships.



22
23
24
25
26
# File 'lib/elastic_graph/graphql/resolvers/nested_relationships.rb', line 22

def initialize(elasticgraph_graphql:, config:)
  @schema_element_names = elasticgraph_graphql..schema_element_names
  @logger = elasticgraph_graphql.logger
  @monotonic_clock = elasticgraph_graphql.monotonic_clock
end

Instance Method Details

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



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

def resolve(field:, object:, args:, 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)
  query = yield

  response =
    case id_or_ids
    when nil, []
      join.blank_value
    else
      initial_response = try_synthesize_response_from_ids(field, id_or_ids, query) ||
        NestedRelationshipsSource.execute_one(
          Array(id_or_ids).to_set,
          query:, join:, context:,
          monotonic_clock: @monotonic_clock
        )

      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