我想在我在 GraphQL 上运行任何查询之前运行一个函数。我想控制一些条件并抛出 GraphQL:: ExecutionError 以防它捕获错误。我知道有一个基本查询在任何查询之前运行,但需要检查用户是否登录或不运行查询并在此处抛出错误会停止执行,我不认为放置它的正确位置上。如何在进行任何查询之前执行函数?这是我到目前为止所尝试的:
module Queries
class BaseQuery < GraphQL::Schema::Resolver
def authorized?(**args)
if context[:current_user].nil?
raise GraphQL::ExecutionError, "Only logged users can run this query"
elsif context[:current_user].orders.any?
today_created = false
not_updated = false
context[:current_user].orders.each do |sp|
if sp.time_opened.to_date == Date.today.to_date
today_created = true
end
if sp.time_opened.to_date != Date.today.to_date && sp.time_closed == nil
not_updated = true
end
end
if (today_created == false && not_updated == false)
# raise GraphQL::ExecutionError, "Error"
end
true
else
# Return true to continue the query:
true
end
end
end
end