Sha256: 36878aa030527a95c64d2f4e5c8610fd6469e7e783bf9dd30d71563eb5e5cba6

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'graphql'
require 'graphql/batch'
require 'promise.rb'

module GraphQL
  # Provides a GraphQL::Field definition to preload ActiveRecord::Associations
  module Preload
    autoload :Instrument, 'graphql/preload/instrument'
    autoload :Loader, 'graphql/preload/loader'
    autoload :VERSION, 'graphql/preload/version'

    module SchemaMethods
      def enable_preloading
        instrument(:field, GraphQL::Preload::Instrument.new)
      end
    end

    module FieldMetadata
      def initialize(*args, preload: nil, preload_scope: nil, **kwargs, &block)
        if preload
          @preload ||= []
          @preload.concat Array.wrap preload
        end
        if preload_scope
          @preload_scope = preload_scope
        end
        super(*args, **kwargs, &block)
      end

      def to_graphql
        field_defn = super
        field_defn.metadata[:preload] = @preload
        field_defn.metadata[:preload_scope] = @preload_scope
        field_defn
      end
    end
  end

  Schema.extend Preload::SchemaMethods
  Schema::Field.prepend Preload::FieldMetadata
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-preload-2.1.0 lib/graphql/preload.rb