Sha256: 931a1b9f48fa2ca274ae13d25d354e654822c4489f16eddcbf528278c727cfae

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'graphql_preload_queries/extensions/preload'

Rails.application.config.to_prepare do
  Types::BaseObject.class_eval do
    class << self
      def preloads
        @preloads ||= {}
      end

      # @param key (Symbol|String)
      # @param preload (Symbol|String or Symbol|String|Hash)
      # @Sample:
      ## key argument supports for multiple query names
      #    add_preload('users|allUsers', :users)
      ## preload argument indicates the association name to be preloaded
      #    add_preload(:allUsers, :users)
      ## preload argument supports for nested associations
      #    add_preload(:inactiveUsers, 'inactivated_users.user')
      ## "preload" key should be specified to indicate the association name
      #    add_preload(:allUsers, { preload: :users, 'allComments|comments' => :comments } })
      ## preload key can be omitted to use the same name as the key
      #    add_preload(:users, { 'allComments|comments' => :comments } })
      def add_preload(key, preload)
        preload ||= key
        raise('Invalid preload query key') if [String, Symbol].exclude?(key.class)
        raise('Invalid preload preload key') if [String, Symbol, Hash].exclude?(preload.class)

        preload[:preload] ||= key if preload.is_a?(Hash)
        key = GraphQL::Schema::Member::BuildType.camelize(key.to_s)
        preloads[key] = preload
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql_preload_queries-0.2.0 config/initializers/add_preload_field.rb