1

这是我的代码:

application_controller.rb

class ApplicationController < ActionController::API
  include Pundit
end

post_type.rb

Types::QueryType = GraphQL::ObjectType.define do
  name 'Query'

  field :posts, types[Types::PostType] do
    resolve ->(obj, args, ctx) {
      if ctx[:pundit].authorize(Post, :index?)
        Post.all
      else
        nil
      end
    }
  end
end

post_policy.rb

class PostPolicy < ApplicationPolicy
  def index?
    false
  end
end

所以现在,当向 graphql 请求 api 时,它将产生默认消息:

{
    "status": 500,
    "error": "Internal Server Error",
    "exception": "#<Pundit::NotAuthorizedError: not allowed to index? this Post...",
    "traces": {
      ...
    }
}

我想要结果自定义消息,我该怎么做?有人可以帮助我!!!!

4

0 回答 0