从这个guide中,我不明白我们如何在graphql-ruby中的字段上添加授权。
我理解我们如何拒绝对整个对象的访问,但我不明白我们如何阻止对对象的一个字段的访问。在我的用例中,我想阻止一些查询来访问我的UserType中的String field。
有可能吗?使用field帮助器?
发布于 2019-07-31 17:42:22
我用的是gem graphql-guard。
GUARD = lambda do |obj, args, ctx|
...some logics...
end
field :some_field, String, null: false, guard: GUARD当我只想使用像lambda do |_, args, _|这样的一些参数时
发布于 2019-03-14 20:32:55
您可以在GraphQL-ruby中使用scoping,也可以使用gem graphql-guard。
要使作用域起作用,您应该使用Pundit scope或CanCan accessible_by。
仅供参考,我还没有在任何地方使用过它,但在我看来,这两个都可以解决你的问题。
发布于 2021-07-06 23:12:48
下面是一个例子:
module Types
class User < Types::BaseObject
field :private_string, String, null: true do
def authorized?(object, args, context)
# Do whatever logic you want and return true if the user is authorized.
object.id == context[:current_user].id
end
end
end
end
class MySchema < GraphQL::Schema
# You'll need this in your schema to handle an unauthorized field.
def self.unauthorized_field(error)
raise GraphQL::ExecutionError, "The field #{error.field.graphql_name} on an object of type #{error.type.graphql_name} was hidden due to permissions"
end
endhttps://stackoverflow.com/questions/55075434
复制相似问题