@@ -32,17 +32,19 @@ def trace(key, data)
3232 trace_field = true # implemented with instrumenter
3333 else
3434 field = data [ :field ]
35- cache = platform_key_cache ( data . fetch ( :query ) . context )
36- platform_key = cache . fetch ( field ) do
37- cache [ field ] = platform_field_key ( data [ :owner ] , field )
38- end
39-
4035 return_type = field . type . unwrap
4136 trace_field = if return_type . kind . scalar? || return_type . kind . enum?
4237 ( field . trace . nil? && @trace_scalars ) || field . trace
4338 else
4439 true
4540 end
41+
42+ platform_key = if trace_field
43+ context = data . fetch ( :query ) . context
44+ cached_platform_key ( context , field ) { platform_field_key ( data [ :owner ] , field ) }
45+ else
46+ nil
47+ end
4648 end
4749
4850 if platform_key && trace_field
@@ -53,20 +55,16 @@ def trace(key, data)
5355 yield
5456 end
5557 when "authorized" , "authorized_lazy"
56- cache = platform_key_cache ( data . fetch ( :context ) )
5758 type = data . fetch ( :type )
58- platform_key = cache . fetch ( type ) do
59- cache [ type ] = platform_authorized_key ( type )
60- end
59+ context = data . fetch ( :context )
60+ platform_key = cached_platform_key ( context , type ) { platform_authorized_key ( type ) }
6161 platform_trace ( platform_key , key , data ) do
6262 yield
6363 end
6464 when "resolve_type" , "resolve_type_lazy"
65- cache = platform_key_cache ( data . fetch ( :context ) )
6665 type = data . fetch ( :type )
67- platform_key = cache . fetch ( type ) do
68- cache [ type ] = platform_resolve_type_key ( type )
69- end
66+ context = data . fetch ( :context )
67+ platform_key = cached_platform_key ( context , type ) { platform_resolve_type_key ( type ) }
7068 platform_trace ( platform_key , key , data ) do
7169 yield
7270 end
@@ -119,13 +117,19 @@ def transaction_name(query)
119117
120118 attr_reader :options
121119
122- def platform_key_cache ( ctx )
123- ctx . namespace ( self . class ) [ :platform_key_cache ] ||= { }
124- end
125-
126- # TODO migrate to this
127- def cached_platform_key ( ctx , cache_name , key )
128- cache = ctx . namespace ( self . class ) [ cache_name ] ||= { }
120+ # Different kind of schema objects have different kinds of keys:
121+ #
122+ # - Object types: `.authorized`
123+ # - Union/Interface types: `.resolve_type`
124+ # - Fields: execution
125+ #
126+ # So, they can all share one cache.
127+ #
128+ # If the key isn't present, the given block is called and the result is cached for `key`.
129+ #
130+ # @return [String]
131+ def cached_platform_key ( ctx , key )
132+ cache = ctx . namespace ( self . class ) [ :platform_key_cache ] ||= { }
129133 cache . fetch ( key ) { cache [ key ] = yield }
130134 end
131135 end
0 commit comments