Class: ElasticGraph::GraphQL::QueryAdapter::RequestedFields

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ RequestedFields

Returns a new instance of RequestedFields.



17
18
19
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 17

def initialize(schema)
  @schema = schema
end

Instance Method Details

#call(field:, query:, lookahead:, args:, context:) ⇒ Object



21
22
23
24
25
26
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 21

def call(field:, query:, lookahead:, args:, context:)
  return query if field.type.unwrap_fully.indexed_aggregation?

  attributes = query_attributes_for(field: field, lookahead: lookahead)
  query.merge_with(**attributes)
end

#query_attributes_for(field:, lookahead:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/elastic_graph/graphql/query_adapter/requested_fields.rb', line 28

def query_attributes_for(field:, lookahead:)
  attributes =
    if field.type.relay_connection?
      {
        individual_docs_needed: pagination_fields_need_individual_docs?(lookahead),
        requested_fields: requested_fields_under(relay_connection_node_from(lookahead))
      }
    else
      {
        requested_fields: requested_fields_under(lookahead)
      }
    end

  attributes.merge(total_document_count_needed: query_needs_total_document_count?(lookahead))
end