Class: ElasticGraph::Apollo::GraphQL::EntitiesFieldResolver
- Inherits:
-
Object
- Object
- ElasticGraph::Apollo::GraphQL::EntitiesFieldResolver
- Defined in:
- lib/elastic_graph/apollo/graphql/entities_field_resolver.rb
Overview
GraphQL resolver for the Apollo ‘Query._entities` field. For details on this field, see:
www.apollographql.com/docs/federation/subgraph-spec/#resolve-requests-for-entities
Defined Under Namespace
Classes: RepresentationWithId, RepresentationWithoutId, RepresentationWithoutIndex
Instance Method Summary collapse
- #can_resolve?(field:, object:) ⇒ Boolean
-
#initialize(datastore_query_builder:, schema_element_names:) ⇒ EntitiesFieldResolver
constructor
A new instance of EntitiesFieldResolver.
- #resolve(field:, object:, args:, context:, lookahead:) ⇒ Object
Constructor Details
#initialize(datastore_query_builder:, schema_element_names:) ⇒ EntitiesFieldResolver
Returns a new instance of EntitiesFieldResolver.
20 21 22 23 |
# File 'lib/elastic_graph/apollo/graphql/entities_field_resolver.rb', line 20 def initialize(datastore_query_builder:, schema_element_names:) @datastore_query_builder = datastore_query_builder @schema_element_names = schema_element_names end |
Instance Method Details
#can_resolve?(field:, object:) ⇒ Boolean
25 26 27 |
# File 'lib/elastic_graph/apollo/graphql/entities_field_resolver.rb', line 25 def can_resolve?(field:, object:) field.parent_type.name == :Query && field.name == :_entities end |
#resolve(field:, object:, args:, 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/elastic_graph/apollo/graphql/entities_field_resolver.rb', line 29 def resolve(field:, object:, args:, context:, lookahead:) schema = context.fetch(:elastic_graph_schema) representations = args.fetch("representations").map.with_index do |rep, index| try_parse_representation(rep, schema) do |error_description| context.add_error(::GraphQL::ExecutionError.new("Representation at index #{index} #{error_description}.")) end end representations_by_adapter = representations.group_by { |rep| rep&.adapter } # The query attributes that are based on the requested subfields are the same across all representations, # so we build the hash of those attributes once here. query_attributes = ElasticGraph::GraphQL::QueryAdapter::RequestedFields .new(schema) .query_attributes_for(field: field, lookahead: lookahead) .merge(monotonic_clock_deadline: context[:monotonic_clock_deadline]) # Build a separate query per adapter instance since each adapter instance is capable of building # a single query that handles all representations assigned to it. query_by_adapter = representations_by_adapter.to_h do |adapter, reps| query = build_query(adapter, reps, query_attributes) if adapter [adapter, query] end responses_by_query = ElasticGraph::GraphQL::Resolvers::QuerySource.execute_many(query_by_adapter.values.compact, for_context: context) indexed_search_hits_by_adapter = query_by_adapter.to_h do |adapter, query| indexed_search_hits = query ? adapter.index_search_hits(responses_by_query.fetch(query)) : {} # : ::Hash[::String, ElasticGraph::GraphQL::DatastoreResponse::Document] [adapter, indexed_search_hits] end representations.map.with_index do |representation, index| next unless (adapter = representation&.adapter) indexed_search_hits = indexed_search_hits_by_adapter.fetch(adapter) adapter.identify_matching_hit(indexed_search_hits, representation, context: context, index: index) end end |