Skip to content

Commit

Permalink
feat: support behaviour for resolvers
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
palkan committed Aug 8, 2019
1 parent e39e666 commit 9b92fb4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## master (unreleased)

- Add support for resolvers. ([@palkan][])

Now it's possible to `include ActionPolicy::GraphQL::Behaviour` into resolver class to use
Action Policy helpers there.

## 0.1.0 (2019-05-20)

- Initial version. ([@palkan][])
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ end
class Types::BaseMutation < GraphQL::Schema::Mutation
include ActionPolicy::GraphQL::Behaviour
end

# For using authorization helpers in resolvers
class Types::BaseResolver < GraphQL::Schema::Resolver
include ActionPolicy::GraphQL::Behaviour
end
```

### `authorize: *`
Expand Down
6 changes: 4 additions & 2 deletions lib/action_policy/graphql/behaviour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def self.included(base)
base.include ActionPolicy::Behaviours::Memoized
base.include ActionPolicy::Behaviours::Namespaced

base.field_class.prepend(ActionPolicy::GraphQL::AuthorizedField)
base.authorize :user, through: :current_user

base.include ActionPolicy::GraphQL::Fields
if base.respond_to?(:field_class)
base.field_class.prepend(ActionPolicy::GraphQL::AuthorizedField)
base.include ActionPolicy::GraphQL::Fields
end
end

def current_user
Expand Down
19 changes: 19 additions & 0 deletions spec/action_policy/graphql/authorized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,24 @@
.with(Me::PostPolicy)
end
end

context "with resolver" do
let(:query) do
%({
resolvedPost {
title
}
})
end

before do
allow(Me).to receive(:post) { post }
end

it "is authorized" do
expect { subject }.to be_authorized_to(:show?, post)
.with(PostPolicy)
end
end
end
end
19 changes: 19 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def current_user
end
end

class BaseResolver < ::GraphQL::Schema::Resolver
include ActionPolicy::GraphQL::Behaviour

def current_user
context.fetch(:user, :user)
end
end

class PostType < BaseType
field :title, String, null: false

Expand Down Expand Up @@ -131,6 +139,16 @@ class << self
attr_accessor :posts, :post
end

class PostResolver < BaseResolver
type PostType, null: false

def resolve
Schema.post.tap do |post|
authorize! post, to: :show?
end
end
end

query(Class.new(BaseType) do
def self.name
"Query"
Expand All @@ -139,6 +157,7 @@ def self.name
field :me, Me::RootType, null: false

field :post, PostType, null: false
field :resolved_post, resolver: PostResolver
field :auth_post, PostType, null: false, authorize: true
field :non_raising_post, PostType, null: true, authorize: {raise: false}
field :another_post, PostType, null: false, authorize: {to: :preview?, with: AnotherPostPolicy}
Expand Down

0 comments on commit 9b92fb4

Please sign in to comment.