Skip to content

Commit

Permalink
Merge d758382 into 71dc649
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jun 29, 2018
2 parents 71dc649 + d758382 commit faf6f05
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ rvm:
env:
- CI=true
before_install: gem install bundler -v 1.15.2
matrix:
include:
- gemfile: graphql-1.7.gemfile
env: GRAPHQL_RUBY=1_7
- gemfile: graphql-1.8.gemfile
env: GRAPHQL_RUBY=1_8
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "pry"
gem 'coveralls', require: false

# Specify your gem's dependencies in graphql-guard.gemspec
gemspec
8 changes: 8 additions & 0 deletions graphql-1.7.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "pry"
gem 'coveralls'

gem "graphql", "~> 1.7.14"

gemspec
8 changes: 8 additions & 0 deletions graphql-1.8.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "pry"
gem 'coveralls'

gem "graphql", "~> 1.8.4"

gemspec
16 changes: 16 additions & 0 deletions lib/graphql/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
GraphQL::Field.accepts_definitions(guard: GraphQL::Define.assign_metadata_key(:guard))
GraphQL::Field.accepts_definitions(mask: GraphQL::Define.assign_metadata_key(:mask))

module GraphQL
class Schema
class Object
accepts_definition :guard
accepts_definition :mask

field_class(
Class.new(GraphQL::Schema::Field) {
accepts_definition :guard
accepts_definition :mask
}
)
end
end
end

module GraphQL
class Guard
ANY_FIELD_NAME = :'*'
Expand Down
14 changes: 14 additions & 0 deletions lib/graphql/guard/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ def field_with_guard(field_name, policy_object = nil)
end
end
end

class Schema
class Object
def self.field_with_guard(field_name, policy_object = nil)
field = fields[field_name]
return unless field

field.to_graphql.clone.tap do |f|
f.__policy_object = policy_object
f.__guard_type = self.to_graphql
end
end
end
end
end
52 changes: 46 additions & 6 deletions spec/fixtures/inline_schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Inline
# Schema in legacy-style API
PostType = GraphQL::ObjectType.define do
name "Post"
guard ->(_post, _args, ctx) { ctx[:current_user].admin? }
Expand All @@ -11,15 +12,15 @@ module Inline
QueryType = GraphQL::ObjectType.define do
name "Query"
field :posts, !types[!PostType] do
argument :user_id, !types.ID
guard ->(_obj, args, ctx) { args[:user_id] == ctx[:current_user].id }
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:user_id]) }
argument :userId, !types.ID
guard ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id }
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:userId]) }
end

field :posts_with_mask, !types[!PostType] do
argument :user_id, !types.ID
field :postsWithMask, !types[!PostType] do
argument :userId, !types.ID
mask ->(ctx) { ctx[:current_user].admin? }
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:user_id]) }
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:userId]) }
end
end

Expand All @@ -34,4 +35,43 @@ module Inline
GraphQL::ExecutionError.new("Not authorized to access #{type}.#{field}")
})
end

# Schema in class-based API
class ClassBasedPost < GraphQL::Schema::Object
guard ->(_post, _args, ctx) { ctx[:current_user].admin? }
field :id, ID, null: false
field :title, String, null: true
end

class ClassBasedQuery < GraphQL::Schema::Object
field :posts, [ClassBasedPost], null: false do
argument :user_id, ID, required: true
guard ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id }
end

def posts(user_id:)
Post.where(user_id: user_id)
end

field :posts_with_mask, [ClassBasedPost], null: false do
argument :user_id, ID, required: true
mask ->(ctx) { ctx[:current_user].admin? }
end

def posts_with_mask(user_id:)
Post.where(user_id: user_id)
end
end

class ClassBasedSchema < GraphQL::Schema
query ClassBasedQuery
use GraphQL::Guard.new
end

class ClassBasedSchemaWithoutExceptions < GraphQL::Schema
query ClassBasedQuery
use GraphQL::Guard.new(not_authorized: ->(type, field) {
GraphQL::ExecutionError.new("Not authorized to access #{type}.#{field}")
})
end
end
6 changes: 3 additions & 3 deletions spec/fixtures/policy_object_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ module PolicyObject
QueryType = GraphQL::ObjectType.define do
name "Query"
field :posts, !types[!PostType] do
argument :user_id, !types.ID
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:user_id]) }
argument :userId, !types.ID
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:userId]) }
end
end

class GraphqlPolicy
RULES = {
QueryType => {
posts: ->(_obj, args, ctx) { args[:user_id] == ctx[:current_user].id }
posts: ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id }
},
PostType => {
'*': ->(_post, args, ctx) { ctx[:current_user].admin? }
Expand Down
Loading

0 comments on commit faf6f05

Please sign in to comment.