Sha256: a9c61831c9ca1f2eb34fc4fcff519a070ba7d854fa12f32a9288642c6d837701

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

module GraphQL
  module ResultCache
    class Key
      def initialize obj:, args:, ctx:, key: nil
        @obj = obj
        @args = args
        @ctx = ctx
        @key = key
      end

      def to_s
        @to_s ||= [
            ::GraphQL::ResultCache.namespace,
            path_clause,
            args_clause,
            object_clause,
            client_hash_clause
        ].flatten.compact.join(':')
      end

      private

      def path_clause
        @ctx.path.join('.')
      end

      def args_clause
        @args.to_h.to_a.flatten
      end

      def object_clause
        case @key
          when Symbol
            @obj.public_send(@key)
          when Proc
            @key.call(@obj, @args, @ctx)
          when NilClass
            guess_id
          else
            @key
        end
      end

      def client_hash_clause
        clause = ::GraphQL::ResultCache.client_hash
        clause.is_a?(Proc) ? clause.call : clause
      end

      def guess_id
        object = @obj.object
        return unless object
        return object.cache_key if object.respond_to?(:cache_key)
        return object.id if object.respond_to?(:id)
        object.object_id
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
graphql-result_cache-0.1.8 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.7 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.6 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.5 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.4 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.3 lib/graphql/result_cache/key.rb
graphql-result_cache-0.1.2 lib/graphql/result_cache/key.rb