Class: ElasticGraph::GraphQL::QueryAdapter::RequestedFields
- Inherits:
-
Object
- Object
- ElasticGraph::GraphQL::QueryAdapter::RequestedFields
- Defined in:
- lib/elastic_graph/graphql/query_adapter/requested_fields.rb
Overview
Query adapter that populates the ‘requested_fields` attribute of an `DatastoreQuery` in order to limit what fields we fetch from the datastore to only those that we actually need to satisfy the GraphQL query. This results in more efficient datastore queries, similar to doing `SELECT f1, f2, …` instead of `SELECT *` for a SQL query.
Defined Under Namespace
Classes: WithoutSchema
Instance Method Summary collapse
- #call(field:, query:, lookahead:, args:, context:) ⇒ Object
-
#initialize(schema) ⇒ RequestedFields
constructor
A new instance of RequestedFields.
- #query_attributes_for(field:, lookahead:) ⇒ Object
Constructor Details
#initialize(schema) ⇒ RequestedFields
Returns a new instance of RequestedFields.
34 35 36 |
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 34 def initialize(schema) @schema = schema end |
Instance Method Details
#call(field:, query:, lookahead:, args:, context:) ⇒ Object
38 39 40 41 |
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 38 def call(field:, query:, lookahead:, args:, context:) attributes = query_attributes_for(field: field, lookahead: lookahead) query.merge_with(**attributes) end |
#query_attributes_for(field:, lookahead:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 43 def query_attributes_for(field:, lookahead:) index_field_paths = field.type .unwrap_fully .search_index_definitions .flat_map { |index_def| index_def.fields_by_path.keys } .to_set attributes = if field.type.relay_connection? highlights = lookahead .selection(@schema.element_names.edges) .selection(@schema.element_names.highlights) { individual_docs_needed: pagination_fields_need_individual_docs?(lookahead) || relay_connection_node_from(lookahead).selected?, requested_fields: requested_fields_under(relay_connection_node_from(lookahead), index_field_paths), request_all_highlights: requesting_all_highlights?(lookahead), requested_highlights: requested_fields_under(highlights, index_field_paths) } else { requested_fields: requested_fields_under(lookahead, index_field_paths) } end attributes.merge(total_document_count_needed: query_needs_total_document_count?(lookahead)) end |