Skip to content

Commit

Permalink
Pass authorize as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekmiyani committed Oct 28, 2024
1 parent 85b9a03 commit ea090cb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/phantom/graphql/field_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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 } }
Expand Down

0 comments on commit ea090cb

Please sign in to comment.