在GraphQL ruby中的任何查询之前运行函数
我想在GraphQL上运行任何查询之前运行一个函数。我想控制一些条件,并抛出一个GraphQL::ExecutionError,以防它捕获错误。我知道在任何查询之前都会运行一个基本查询,但需要检查用户是否已登录或是否要运行查询,在此处抛出错误会停止执行,我不认为是在正确的位置进行查询。 在进行任何查询之前,如何执行函数? 这就是我到目前为止所尝试的:在GraphQL ruby中的任何查询之前运行函数,graphql,graphql-subscriptions,graphql-ruby,Graphql,Graphql Subscriptions,Graphql Ruby,我想在GraphQL上运行任何查询之前运行一个函数。我想控制一些条件,并抛出一个GraphQL::ExecutionError,以防它捕获错误。我知道在任何查询之前都会运行一个基本查询,但需要检查用户是否已登录或是否要运行查询,在此处抛出错误会停止执行,我不认为是在正确的位置进行查询。 在进行任何查询之前,如何执行函数? 这就是我到目前为止所尝试的: module Queries class BaseQuery < GraphQL::Schema::Resolver def
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
模块查询
类BaseQuery
您不应该在BaseQuery
if context[:current_user].nil?
raise GraphQL::ExecutionError, "Only logged users can run this query"
而不是使用gem进行授权。例如,您可以很好地支持GraphQL
if (today_created == false && not_updated == false)
# raise GraphQL::ExecutionError, "Error"
end
如果需要为每个请求检查此项,请使用BaseResolver
因此,您应该为代码使用其他应用程序层