Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module PolicyObject | ||
PostType = GraphQL::ObjectType.define do | ||
name "Post" | ||
field :id, !types.ID | ||
field :title, types.String | ||
end | ||
case ENV['GRAPHQL_RUBY_VERSION'] | ||
when '1_7' | ||
PostType = GraphQL::ObjectType.define do | ||
name "Post" | ||
field :id, !types.ID | ||
field :title, types.String | ||
end | ||
|
||
QueryType = GraphQL::ObjectType.define do | ||
name "Query" | ||
field :posts, !types[!PostType] do | ||
argument :userId, !types.ID | ||
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:userId]) } | ||
QueryType = GraphQL::ObjectType.define do | ||
name "Query" | ||
field :posts, !types[!PostType] do | ||
argument :userId, !types.ID | ||
resolve ->(_obj, args, _ctx) { Post.where(user_id: args[:userId]) } | ||
end | ||
end | ||
end | ||
|
||
class GraphqlPolicy | ||
RULES = { | ||
QueryType => { | ||
posts: ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id } | ||
}, | ||
PostType => { | ||
'*': ->(_post, args, ctx) { ctx[:current_user].admin? } | ||
class GraphqlPolicy | ||
RULES = { | ||
QueryType => { | ||
posts: ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id } | ||
}, | ||
PostType => { | ||
'*': ->(_post, args, ctx) { ctx[:current_user].admin? } | ||
} | ||
} | ||
} | ||
|
||
def self.guard(type, field) | ||
RULES.dig(type, field) | ||
def self.guard(type, field) | ||
RULES.dig(type, field) | ||
end | ||
end | ||
end | ||
|
||
Schema = GraphQL::Schema.define do | ||
query QueryType | ||
use GraphQL::Guard.new(policy_object: GraphqlPolicy) | ||
Schema = GraphQL::Schema.define do | ||
query QueryType | ||
use GraphQL::Guard.new(policy_object: GraphqlPolicy) | ||
end | ||
when '1_8' | ||
class PostType < GraphQL::Schema::Object | ||
field :id, ID, null: false | ||
field :title, String, null: true | ||
end | ||
|
||
class QueryType < GraphQL::Schema::Object | ||
field :posts, [PostType], null: false do | ||
argument :user_id, ID, required: true | ||
end | ||
|
||
def posts(user_id:) | ||
Post.where(user_id: user_id) | ||
end | ||
end | ||
|
||
class GraphqlPolicy | ||
RULES = { | ||
QueryType => { | ||
posts: ->(_obj, args, ctx) { args[:userId] == ctx[:current_user].id } | ||
}, | ||
PostType => { | ||
'*': ->(_post, args, ctx) { ctx[:current_user].admin? } | ||
} | ||
} | ||
|
||
def self.guard(type, field) | ||
RULES.dig(type.metadata[:type_class], field) | ||
end | ||
end | ||
|
||
class Schema < GraphQL::Schema | ||
query QueryType | ||
use GraphQL::Guard.new(policy_object: GraphqlPolicy) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters