【问题标题】:Run function before any query in GraphQL ruby在 GraphQL ruby​​ 中的任何查询之前运行函数
【发布时间】:2020-06-15 17:06:56
【问题描述】:

我想在我在 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


【问题讨论】:

    标签: graphql graphql-subscriptions graphql-ruby


    【解决方案1】:

    你不应该在BaseQuery中运行这样的代码

    if context[:current_user].nil?
      raise GraphQL::ExecutionError, "Only logged users can run this query"
    

    而不是这个使用 gem 进行授权。比如action_policy对GraphQL有很好的支持

    if (today_created == false && not_updated == false)
      # raise GraphQL::ExecutionError, "Error"
    end
    

    如果您需要为每个请求检查此项,请使用 BaseResolver

    因此您应该为您的代码使用其他应用层

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2014-12-09
      • 2019-12-21
      • 2019-06-29
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多