From aafe132c9beacd487d859d2d674ee5268c22a49a Mon Sep 17 00:00:00 2001 From: Vivek Date: Sat, 15 Jun 2024 01:01:11 +0530 Subject: [PATCH] Add PreloadExtension --- lib/phantom/graphql.rb | 5 +++- lib/phantom/graphql/authorization.rb | 23 ----------------- lib/phantom/graphql/field_extensions.rb | 34 +++++++++++++++++++++++++ phantom.gemspec | 2 +- 4 files changed, 39 insertions(+), 25 deletions(-) delete mode 100644 lib/phantom/graphql/authorization.rb create mode 100644 lib/phantom/graphql/field_extensions.rb diff --git a/lib/phantom/graphql.rb b/lib/phantom/graphql.rb index 2756ed9..3039eb8 100644 --- a/lib/phantom/graphql.rb +++ b/lib/phantom/graphql.rb @@ -1,14 +1,17 @@ # frozen_string_literal: true require "graphql" +require "batch-loader" module Phantom::Graphql end -require "phantom/graphql/authorization" +require "phantom/graphql/field_extensions" require "phantom/graphql/enum" require "phantom/graphql/response" +GraphQL::Schema.use(BatchLoader::GraphQL) + GraphQL::Schema::InputObject.extend(Phantom::Graphql::Enum) GraphQL::Schema::Object.extend(Phantom::Graphql::Enum) GraphQL::Schema::RelayClassicMutation.extend(Phantom::Graphql::Enum) diff --git a/lib/phantom/graphql/authorization.rb b/lib/phantom/graphql/authorization.rb deleted file mode 100644 index da9e170..0000000 --- a/lib/phantom/graphql/authorization.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module Phantom::Graphql::Authorization - class AuthorizeExtension < GraphQL::Schema::FieldExtension - def resolve(context:, object:, arguments:, **_rest) - name, action = options.split("#") - - authorized = name.constantize.new(context[:current_session]).send(action) - - raise GraphQL::ExecutionError, "Unauthorized" unless authorized - - yield object, arguments - end - end - - def initialize(*args, authorize: nil, **kwargs, &block) - extensions = (kwargs[:extensions] ||= []) - - extensions << { Phantom::Graphql::Authorization::AuthorizeExtension => authorize } if authorize.present? - - super(*args, **kwargs, &block) - end -end diff --git a/lib/phantom/graphql/field_extensions.rb b/lib/phantom/graphql/field_extensions.rb new file mode 100644 index 0000000..161c9f5 --- /dev/null +++ b/lib/phantom/graphql/field_extensions.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Phantom::Graphql::FieldExtensions + class AuthorizeExtension < GraphQL::Schema::FieldExtension + def resolve(context:, object:, arguments:, **_rest) + name, action = options.split("#") + + authorized = name.constantize.new(context[:current_session]).send(action) + + raise GraphQL::ExecutionError, "Unauthorized" unless authorized + + yield object, arguments + end + end + + class PreloadExtension < GraphQL::Schema::FieldExtension + def resolve(context:, object:, arguments:, **rest) + 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]).call + records.each { |r| loader.call(r, super(context: context, object: r, arguments: arguments, **rest)) } + end + end + end + + def initialize(*args, authorize: nil, preload: nil, scope: nil, **kwargs, &block) + extensions = (kwargs[:extensions] ||= []) + + extensions << { Phantom::Graphql::FieldExtensions::AuthorizeExtension => authorize } if authorize.present? + extensions << { Phantom::Graphql::FieldExtensions::PreloadExtension => { preload: preload, scope: scope } } if preload.present? + + super(*args, **kwargs, &block) + end +end diff --git a/phantom.gemspec b/phantom.gemspec index fe7041f..3082791 100644 --- a/phantom.gemspec +++ b/phantom.gemspec @@ -14,6 +14,6 @@ Gem::Specification.new do |s| s.metadata["rubygems_mfa_required"] = "true" s.required_ruby_version = ">= 3.1" + s.add_dependency "batch-loader" s.add_dependency "graphql" - s.add_dependency "minitest" end