class GraphQL::FragmentCache::CacheKeyBuilder

Builds cache key for fragment

Extends key builder to use .expand_cache_key in Rails

Attributes

object[R]
path[R]
query[R]
schema[R]

Public Class Methods

call(**options) click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 115
def call(**options)
  new(**options).build
end
new(object: nil, query:, path:, **options) click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 122
def initialize(object: nil, query:, path:, **options)
  @object = object
  @query = query
  @schema = query.schema
  @path = path
  @options = options
end

Public Instance Methods

build() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 130
def build
  [
    GraphQL::FragmentCache.namespace,
    implicit_cache_key,
    object_cache_key
  ].compact.join("/")
end

Private Instance Methods

implicit_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 140
def implicit_cache_key
  Digest::SHA1.hexdigest("#{schema_cache_key}/#{query_cache_key}")
end
object_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 189
def object_cache_key
  @options[:object_cache_key] || object_key(object)
end
object_key(obj) click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 193
def object_key(obj)
  obj&._graphql_cache_key
end
path_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 164
def path_cache_key
  @options.fetch(:path_cache_key) do
    lookahead = query.lookahead

    path.map { |field_name|
      # Handle cached fields inside collections:
      next field_name if field_name.is_a?(Integer)

      lookahead = lookahead.selection_with_alias(field_name)
      raise "Failed to look ahead the field: #{field_name}" if lookahead.is_a?(::GraphQL::Execution::Lookahead::NullLookahead)

      next lookahead.field.name if lookahead.arguments.empty?

      args = lookahead.arguments.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
      "#{lookahead.field.name}(#{args})"
    }.join("/")
  end
end
query_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 148
def query_cache_key
  @options.fetch(:query_cache_key) { "#{path_cache_key}[#{selections_cache_key}]" }
end
schema_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 144
def schema_cache_key
  @options.fetch(:schema_cache_key) { schema.schema_cache_key }
end
selections_cache_key() click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 152
def selections_cache_key
  current_root =
    path.reduce(query.lookahead) { |lkhd, field_name|
      # Handle cached fields inside collections:
      next lkhd if field_name.is_a?(Integer)

      lkhd.selection_with_alias(field_name)
    }

  current_root.selections.to_selections_key
end
traverse_argument(argument) click to toggle source
# File lib/graphql/fragment_cache/cache_key_builder.rb, line 183
def traverse_argument(argument)
  return argument unless argument.is_a?(GraphQL::Schema::InputObject)

  "{#{argument.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
end