Skip to content

Commit 4f352e8

Browse files
committed
Guard QueryContext#[]/#fetch against removed magic context keys
PR-1341 followup (review comment from Ellis): raise a helpful ConfigError instead of silently returning nil if custom resolver code still reads a key that moved to a typed accessor.
1 parent 64fa156 commit 4f352e8

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎elasticgraph-graphql/lib/elastic_graph/graphql/query_context.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#
77
# frozen_string_literal: true
88

9+
require "elastic_graph/errors"
910
require "elastic_graph/graphql/client"
1011
require "elastic_graph/graphql/query_details_tracker"
1112
require "graphql"
@@ -68,8 +69,37 @@ def cache_datastore_query(cache_key)
6869
datastore_query_cache[cache_key] ||= yield
6970
end
7071

72+
# These context hash keys were replaced by typed accessors above. `[]`/`fetch` raise a
73+
# helpful error here instead of silently returning `nil`, in case any custom resolver code
74+
# still reads them as hash keys.
75+
REMOVED_KEY_REPLACEMENTS = {
76+
elastic_graph_schema: "elastic_graph_schema",
77+
datastore_search_router: "datastore_search_router",
78+
datastore_query_cache: "cache_datastore_query",
79+
elastic_graph_query_tracker: "elastic_graph_query_tracker",
80+
monotonic_clock_deadline: "monotonic_clock_deadline",
81+
elastic_graph_client: "elastic_graph_client",
82+
http_request: "http_request"
83+
}
84+
85+
def [](key)
86+
raise_if_removed_key(key)
87+
super
88+
end
89+
90+
def fetch(key, *args, &block)
91+
raise_if_removed_key(key)
92+
super
93+
end
94+
7195
private
7296

97+
def raise_if_removed_key(key)
98+
if (replacement = REMOVED_KEY_REPLACEMENTS[key])
99+
raise Errors::ConfigError, "`context[#{key.inspect}]` is no longer supported; use `context.#{replacement}` instead."
100+
end
101+
end
102+
73103
def datastore_query_cache
74104
@datastore_query_cache ||= {}
75105
end

‎elasticgraph-graphql/sig/elastic_graph/graphql/query_context.rbs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ module ElasticGraph
2727

2828
def cache_datastore_query: (untyped cache_key) { () -> DatastoreQuery } -> DatastoreQuery
2929

30+
REMOVED_KEY_REPLACEMENTS: ::Hash[::Symbol, ::String]
31+
32+
def []: (untyped key) -> untyped
33+
def fetch: (untyped key, *untyped args) ?{ (untyped) -> untyped } -> untyped
34+
3035
private
3136

37+
def raise_if_removed_key: (untyped key) -> void
38+
3239
@datastore_query_cache: ::Hash[untyped, DatastoreQuery]?
3340
def datastore_query_cache: () -> ::Hash[untyped, DatastoreQuery]
3441
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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/errors"
10+
require "elastic_graph/graphql/query_context"
11+
12+
module ElasticGraph
13+
class GraphQL
14+
RSpec.describe QueryContext do
15+
let(:context) do
16+
described_class.new(
17+
query: instance_double(::GraphQL::Query, fingerprint: "GetColors/abc123"),
18+
schema: Class.new(::GraphQL::Schema),
19+
values: {}
20+
)
21+
end
22+
23+
describe "#[] and #fetch" do
24+
it "raises a helpful error for a removed magic context key, directing to the replacement accessor" do
25+
expect {
26+
context[:monotonic_clock_deadline]
27+
}.to raise_error(Errors::ConfigError, a_string_including("context.monotonic_clock_deadline"))
28+
29+
expect {
30+
context.fetch(:monotonic_clock_deadline)
31+
}.to raise_error(Errors::ConfigError, a_string_including("context.monotonic_clock_deadline"))
32+
end
33+
34+
it "still works normally for keys that were never replaced by a typed accessor" do
35+
context[:visibility_profile] = "public"
36+
37+
expect(context[:visibility_profile]).to eq("public")
38+
expect(context.fetch(:visibility_profile)).to eq("public")
39+
end
40+
end
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)