Graphql context in Policies?
marlosirapuan opened this issue · 2 comments
marlosirapuan commented
Hi, there is a way to pass Graphql context to the Policies? Example:
# frozen_string_literal: true
module Types
class QueryType < Types::BaseObject
field :user,
resolver: Queries::User::Fetch,
preauthorize: { to: :user?, with: Queries::UsersPolicy }
end
end
module Queries
class UsersPolicy < ApplicationPolicy
authorize :user, allow_nil: true
def initialize(user, record)
# context here
variables = record.context.query.provided_variables
end
def user?
user.admin?
end
end
end
I need the query variables from graphql
palkan commented
You can add context
as an additional authorization context. Something like this:
# in GraphQL
class Types::BaseObject
authorize :graphql_context, through: :context
end
module Queries
class ApplicationPolicy < ActionPolicy::Base
authorize :graphql_context
private
def variables
graphql_context.query.provided_variables
end
end
end
marlosirapuan commented
@palkan yes! it worked. Thank you! 🍻