[英]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.