Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Add alias resolve for resolve_field method #95

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion graphql-pundit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'graphql', '>= 1.6.4', '< 1.10.0'
spec.add_dependency 'graphql', '>= 1.9.0', '< 1.10.0'
spec.add_dependency 'pundit', '~> 1.1.0'

spec.add_development_dependency 'bundler', '~> 1.14'
Expand Down
21 changes: 16 additions & 5 deletions lib/graphql-pundit/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,26 @@ def authorize!(*args, record: nil, policy: nil)
end

def resolve_field(obj, args, ctx)
resolve_helper(obj, args, ctx) { super(obj, args, ctx) }
end

def resolve(obj, args, ctx)
resolve_helper(obj, args, ctx) { super(obj, args, ctx) }
end

private

def resolve_helper(obj, args, ctx)
raise ::Pundit::NotAuthorizedError unless do_authorize(obj, args, ctx)
super(obj, args, ctx)

yield
rescue ::Pundit::NotAuthorizedError
if @do_raise
raise GraphQL::ExecutionError, "You're not authorized to do this"
end
raise_graphql_error if @do_raise
end

private
def raise_graphql_error
raise GraphQL::ExecutionError, "You're not authorized to do this"
end

def do_authorize(root, arguments, context)
return true unless @authorize
Expand Down
6 changes: 6 additions & 0 deletions lib/graphql-pundit/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def resolve_field(obj, args, ctx)
apply_scope(@after_scope, field_return, args, ctx)
end

def resolve(obj, args, ctx)
before_scope_return = apply_scope(@before_scope, obj, args, ctx)
field_return = super(before_scope_return, args, ctx)
apply_scope(@after_scope, field_return, args, ctx)
end

private

def apply_scope(scope, root, arguments, context)
Expand Down
5 changes: 2 additions & 3 deletions spec/graphql-pundit/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
let(:record) { nil }
let(:policy) { nil }
let(:field_value) { Car.first.name }
let(:result) { field.resolve(Car.first, {}, {}) }
let(:result) { field.resolve(Car.first, {}, spec_context) }

context 'one-line field definition' do
let(:field) do
Expand All @@ -104,8 +104,7 @@
authorize: (do_raise ? nil : query),
record: record,
policy: policy,
null: true).
to_graphql
null: true)
end

include_examples 'auth methods'
Expand Down
14 changes: 7 additions & 7 deletions spec/graphql-pundit/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
let(:authorize) { true } if with_authorization

context 'before_scope' do
let(:resolve) { ->(cars, _, _) { cars.to_a.map(&:name) } }
let(:resolve) { :names }

context 'inferred scope' do
let(:before_scope) { true }
let(:expected_result) do
Expand Down Expand Up @@ -59,7 +60,7 @@
end

context 'after_scope' do
let(:resolve) { ->(scope, _, _) { scope.where { |c| c.name.length > 5 } } }
let(:resolve) { :longer_then_five }

context 'inferred scope' do
let(:scope_class) do
Expand Down Expand Up @@ -120,7 +121,7 @@ def resolve
let(:before_scope) { nil }
let(:after_scope) { nil }
let(:authorize) { nil }
let(:result) { field.resolve(Car, {}, {}) }
let(:result) { field.resolve(Car, {}, spec_context) }
context 'one-line field definition' do
let(:field) do
Field.new(name: :name,
Expand All @@ -129,8 +130,7 @@ def resolve
before_scope: before_scope,
after_scope: after_scope,
null: true,
resolve: resolve).
to_graphql
resolver_method: resolve)
end

context 'with failing authorization' do
Expand All @@ -147,10 +147,10 @@ def resolve
type: [String],
authorize: authorize,
null: true,
resolve: resolve)
resolver_method: resolve)
field.before_scope before_scope
field.after_scope after_scope
field.to_graphql
field
end

context 'with failing authorization' do
Expand Down
28 changes: 28 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@

Field = GraphQL::Pundit::Field

class BaseObject < GraphQL::Schema::Object
field_class GraphQL::Pundit::Field
end

class Query < BaseObject
field :test, Int, null: true
end

class Schema < GraphQL::Schema
query(Query)
end

def spec_context
GraphQL::Query::Context.new(query: Query, schema: Schema, object: {}, values: {})
end

class CarDataset
attr_reader :cars

Expand Down Expand Up @@ -52,6 +68,10 @@ def to_a
def model
Car
end

def names
self.to_a.map(&:name)
end
end

class Car
Expand All @@ -73,6 +93,14 @@ def self.first(&block)
where(&block).first
end

def object
self
end

def self.longer_then_five
self.where { |c| c.name.length > 5 }
end

def initialize(name, country)
@name = name
@country = country
Expand Down