Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support behaviour for resolvers #3

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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