|
| 1 | +# Copyright 2024 - 2026 Block, Inc. |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style |
| 4 | +# license that can be found in the LICENSE file or at |
| 5 | +# https://opensource.org/licenses/MIT. |
| 6 | +# |
| 7 | +# frozen_string_literal: true |
| 8 | + |
| 9 | +require "elastic_graph/graphql/client" |
| 10 | +require "elastic_graph/graphql/query_details_tracker" |
| 11 | +require "graphql" |
| 12 | + |
| 13 | +module ElasticGraph |
| 14 | + class GraphQL |
| 15 | + # `GraphQL::Query::Context` subclass used by ElasticGraph, providing typed accessors in |
| 16 | + # place of untyped context hash keys. Registered as the `context_class` on the `GraphQL::Schema` |
| 17 | + # built by `Schema`, via `new_class`, so that every query gets an instance closed over the |
| 18 | + # `Schema` and `DatastoreSearchRouter` in use for that schema. |
| 19 | + class QueryContext < ::GraphQL::Query::Context |
| 20 | + def self.elastic_graph_schema |
| 21 | + @elastic_graph_schema |
| 22 | + end |
| 23 | + |
| 24 | + def self.datastore_search_router |
| 25 | + @datastore_search_router |
| 26 | + end |
| 27 | + |
| 28 | + # Builds a `QueryContext` subclass closed over the given `elastic_graph_schema` and |
| 29 | + # `datastore_search_router`, suitable for registering as a `GraphQL::Schema#context_class`. |
| 30 | + def self.new_class(elastic_graph_schema:, datastore_search_router:) |
| 31 | + klass = ::Class.new(self) # : singleton(QueryContext) |
| 32 | + klass.instance_variable_set(:@elastic_graph_schema, elastic_graph_schema) |
| 33 | + klass.instance_variable_set(:@datastore_search_router, datastore_search_router) |
| 34 | + klass |
| 35 | + end |
| 36 | + |
| 37 | + # `elastic_graph_schema` is only `nil` before `new_class` has closed over it, which never |
| 38 | + # happens for a `QueryContext` actually used to execute a query (see `Schema#initialize`). |
| 39 | + def elastic_graph_schema |
| 40 | + self.class.elastic_graph_schema # : Schema |
| 41 | + end |
| 42 | + |
| 43 | + def datastore_search_router |
| 44 | + self.class.datastore_search_router # : DatastoreSearchRouter |
| 45 | + end |
| 46 | + |
| 47 | + # @dynamic monotonic_clock_deadline, elastic_graph_client, http_request |
| 48 | + attr_reader :monotonic_clock_deadline, :elastic_graph_client, :http_request |
| 49 | + |
| 50 | + # Registers the given ElasticGraph-managed values on this context. Intended to be called |
| 51 | + # exactly once, by `Schema#new_graphql_query` callers immediately after building a query and |
| 52 | + # before making it available to resolvers or other user-facing code. Not for use elsewhere. |
| 53 | + def register_elastic_graph_values(monotonic_clock_deadline: nil, elastic_graph_client: Client::ANONYMOUS, http_request: nil) |
| 54 | + @monotonic_clock_deadline = monotonic_clock_deadline |
| 55 | + @elastic_graph_client = elastic_graph_client |
| 56 | + @http_request = http_request |
| 57 | + end |
| 58 | + |
| 59 | + # Lazily builds (and memoizes) the `QueryDetailsTracker` for this query. |
| 60 | + def elastic_graph_query_tracker |
| 61 | + @elastic_graph_query_tracker ||= QueryDetailsTracker.empty |
| 62 | + end |
| 63 | + |
| 64 | + # Caches the result of the given block (a built `DatastoreQuery`) under `cache_key`, scoped to |
| 65 | + # this single query execution. See `Resolvers::QueryAdapter#build_query_from` for why this |
| 66 | + # memoization is beneficial. |
| 67 | + def cache_datastore_query(cache_key) |
| 68 | + datastore_query_cache[cache_key] ||= yield |
| 69 | + end |
| 70 | + |
| 71 | + private |
| 72 | + |
| 73 | + def datastore_query_cache |
| 74 | + @datastore_query_cache ||= {} |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | +end |
0 commit comments