Class: ElasticGraph::GraphQL::Resolvers::QueryAdapter
- Inherits:
-
Object
- Object
- ElasticGraph::GraphQL::Resolvers::QueryAdapter
- Defined in:
- lib/elastic_graph/graphql/resolvers/query_adapter.rb
Overview
Responsible for taking raw GraphQL query arguments and transforming them into a DatastoreQuery object.
Instance Method Summary collapse
- #build_query_from(field:, args:, lookahead:, context: {}) ⇒ Object
-
#initialize(datastore_query_builder:, datastore_query_adapters:) ⇒ QueryAdapter
constructor
A new instance of QueryAdapter.
Constructor Details
#initialize(datastore_query_builder:, datastore_query_adapters:) ⇒ QueryAdapter
Returns a new instance of QueryAdapter.
15 16 17 18 |
# File 'lib/elastic_graph/graphql/resolvers/query_adapter.rb', line 15 def initialize(datastore_query_builder:, datastore_query_adapters:) @datastore_query_builder = datastore_query_builder @datastore_query_adapters = datastore_query_adapters end |
Instance Method Details
#build_query_from(field:, args:, lookahead:, context: {}) ⇒ Object
20 21 22 23 24 25 26 27 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 53 54 55 56 57 58 59 |
# File 'lib/elastic_graph/graphql/resolvers/query_adapter.rb', line 20 def build_query_from(field:, args:, lookahead:, context: {}) monotonic_clock_deadline = context[:monotonic_clock_deadline] # Building an `DatastoreQuery` is not cheap; we do a lot of work to: # # 1) Convert the `args` to their schema form. # 2) Reduce over our different query builders into a final `Query` object # 3) ...and those individual query builders often do a lot of work (traversing lookaheads, etc). # # So it is beneficial to avoid re-creating the exact same `DatastoreQuery` object when # we are resolving the same field in the context of a different object. For example, # consider a query like: # # query { # widgets { # components { # id # parts { # id # } # } # } # } # # Here `components` and `parts` are nested relation fields. If we load 50 of each collection, # this `build_query_from` method will be called 50 times for the `Widget.components` field, # and 2500 times (50 * 50) for the `Component.parts` field...but for a given field, the # built `DatastoreQuery` will be exactly the same. # # Therefore, it is beneficial to memoize the `DatastoreQuery` to avoid re-doing the same work # over and over again, provided we can do so safely. # # `context` is a hash-like `GraphQL::Query::Context` object. Each executed query gets its own # instance, so we can safely cache things in it and trust that it will not "leak" to another # query execution. We carefully build a cache key below to ensure that we only ever reuse # the same `DatastoreQuery` in a situation that would produce the exact same `DatastoreQuery`. context[:datastore_query_cache] ||= {} context[:datastore_query_cache][cache_key_for(field, args, lookahead)] ||= build_new_query_from(field, args, lookahead, context, monotonic_clock_deadline) end |