繁体   English   中英

如何向 graphql-ruby 中的错误数组添加多个错误?

[英]How do I add more than one error to the errors array in graphql-ruby?

GraphQl-Ruby 的文档指出,在引发如下异常时,顶级错误会添加到errors中:

raise GraphQL::ExecutionError, "Can't continue with this query"

哪个产生:

{
  "errors" => [
    {
      "message" => "Can't continue with this query",
      "locations" => [
        {
          "line" => 2,
          "column" => 10,
        }
      ],
      "path" => ["user", "login"],
    }
  ]
}

但我想继续并向错误数组添加多个错误。 我该怎么做(完全没有黑客攻击)

我在rescue_from执行的某个地方找到了答案(不记得在哪里)。

基本上,您需要推送到context.errors 您可以在输入类型或现场的准备方法中执行此操作。

像这样:

class MyInputObject < GraphQL::Schema::InputObject
  def prepare
    context.errors << GraphQL::ExecutionError.new('bummer!') if bummer?
    context.errors << GraphQL::ExecutionError.new('darn it!') if darn_it?

    super
  end
end

对我来说,错误是自动出现的,我不需要在我的实现中的任何地方有任何rescue_from

您可能需要引发执行错误。

这将为您提供一系列错误作为响应。 如果您的错误处理有点复杂,则非常有用。

看:
https://graphql-ruby.org/api-doc/2.0.0/GraphQL/Schema.html#rescue_from-class_method https://graphql-ruby.org/api-doc/2.0.0/GraphQL/Query/Context .html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM