From ea090cb5cab5d9f867106503826679f4af0da33e Mon Sep 17 00:00:00 2001 From: Vivek Date: Tue, 29 Oct 2024 00:19:02 +0530 Subject: [PATCH] Pass authorize as an option --- lib/phantom/graphql/field_extensions.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/phantom/graphql/field_extensions.rb b/lib/phantom/graphql/field_extensions.rb index 65d1148..59765fd 100644 --- a/lib/phantom/graphql/field_extensions.rb +++ b/lib/phantom/graphql/field_extensions.rb @@ -3,7 +3,7 @@ module Phantom::Graphql::FieldExtensions class AuthorizeExtension < GraphQL::Schema::FieldExtension def resolve(context:, object:, arguments:, **_rest) - name, action = options.split("#") + name, action = options[:authorize].split("#") authorized = name.constantize.new(context[:current_session]).send(action) @@ -17,9 +17,11 @@ class PreloadExtension < GraphQL::Schema::FieldExtension def resolve(context:, object:, arguments:, **rest) # rubocop:disable Metrics/AbcSize BatchLoader::GraphQL.for(object).batch(key: field) do |records, loader| scope = options[:scope].constantize.new(context[:current_session]).scope if options[:scope].present? + ActiveRecord::Associations::Preloader.new( records: records.map(&:object), associations: options[:preload], scope: scope ).call + records.each { |r| loader.call(r, super(context: context, object: r, arguments: arguments, **rest)) } end end @@ -28,7 +30,9 @@ def resolve(context:, object:, arguments:, **rest) # rubocop:disable Metrics/Abc def initialize(*args, authorize: nil, preload: nil, scope: nil, **kwargs, &block) extensions = (kwargs[:extensions] ||= []) - extensions << { Phantom::Graphql::FieldExtensions::AuthorizeExtension => authorize } if authorize.present? + if authorize.present? + extensions << { Phantom::Graphql::FieldExtensions::AuthorizeExtension => { authorize: authorize } } + end if preload.present? extensions << { Phantom::Graphql::FieldExtensions::PreloadExtension => { preload: preload, scope: scope } }