Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Commit 75e20a9

Browse files
Add first pass for wrapping connection cache data
1 parent 75a0d4e commit 75e20a9

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

lib/graphql/cache/fetcher.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,50 @@ def cached_resolve(type, field)
3232
lambda do |obj, args, ctx|
3333
key = cache_key(obj, args, type, field)
3434

35-
Marshal[key].read(field.metadata[:cache], force: ctx[:force_cache]) do
35+
cached_value = Marshal[key].read(
36+
field.metadata[:cache], force: ctx[:force_cache]
37+
) do
3638
old_resolve_proc.call(obj, args, ctx)
3739
end
40+
41+
wrap_connections(cached_value, args, field, obj, ctx)
42+
end
43+
end
44+
45+
# @private
46+
def wrap_connections(cached_value, args, field, obj, ctx)
47+
# check for a connection implementation for this cached value
48+
conn_class = cached_value.class.ancestors.find do |ancestor|
49+
!connection_for(ancestor.name).nil?
50+
end
51+
52+
if conn_class
53+
create_connection(conn_class, cached_value, args, field, obj, ctx)
54+
else
55+
# if we get here, this is not a connection
56+
# value and should be returned as is
57+
cached_value
3858
end
3959
end
60+
61+
# @private
62+
def connection_for(name)
63+
GraphQL::Relay::BaseConnection::CONNECTION_IMPLEMENTATIONS[name]
64+
end
65+
66+
# rubocop:disable Metrics/ParameterLists
67+
68+
# @private
69+
def create_connection(conn_class, value, args, field, obj, ctx)
70+
connection_for(conn_class.name).new(
71+
value,
72+
args,
73+
field: field,
74+
parent: obj,
75+
context: ctx
76+
)
77+
end
78+
# rubocop:enable Metrics/ParameterLists
4079
end
4180
end
4281
end

0 commit comments

Comments
 (0)