module CacheQL

Wrap a resolve function to store its value in Rails.cache This requires `cache_key` as an instance method on the object (usually on ActiveRecord's or scopes),

github.com/Shopify/graphql-batch/blob/master/examples/association_loader.rb

github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/tracing/scout_tracing.rb github.com/rmosolgo/graphql-ruby/blob/master/guides/fields/instrumentation.md help.apm.scoutapp.com/#ruby-custom-instrumentation

Based on github.com/rmosolgo/graphql-batch-example/blob/master/good_schema/polymorphic_key_loader.rb

Via github.com/Shopify/graphql-batch/blob/master/examples/record_loader.rb And github.com/rmosolgo/graphql-batch-example/blob/master/good_schema/find_loader.rb

Constants

VERSION

Public Class Methods

fetch(cacheable_fields, query, variables, &block) click to toggle source

Query-level caching, for any cacheable_fields

# File lib/cacheql.rb, line 14
def self.fetch(cacheable_fields, query, variables, &block)
  document = GraphQL.parse(query)
  cacheables = document.definitions.map { |definition| definition.selections.map(&:name) }.flatten & cacheable_fields

  if cacheables.present?
    cache_key = [
      CacheQL::Railtie.config.global_key,
      'result',
      Digest::SHA256.hexdigest(document.to_query_string + variables.to_s)
    ]
    CacheQL::Railtie.config.cache.fetch(cache_key, expires_in: CacheQL::Railtie.config.expires_range.sample.minutes) do
      block.call(document)
    end
  else
    block.call(document)
  end
end