【问题标题】:Graphql-ruby Bulding Mutation/query Without the need of user authenticationGraphql-ruby Bulding Mutation/query 无需用户认证
【发布时间】:2022-10-20 16:09:35
【问题描述】:

嘿,我有一个 graphql 突变,需要在用户登录之前实现。到目前为止,我只有在用户完全通过身份验证后才使用 graphql 端点。由于 graphql 控制器继承了实现 before_action :authenticate_user! 回调的应用程序控制器,因此我总是需要一个有效的用户才能使用 graphql 端点。有没有办法将某些 graphql 端点配置为没有有效用户。

我应该怎么做?

【问题讨论】:

  • 您始终可以在 authenticate_user! 方法中添加一个条件,以在进行具有特定参数/标志的调用时跳过身份验证
  • 另外,请考虑为您的问题和迄今为止尝试过的事情添加更多详细信息。

标签: ruby ruby-on-rails-3 graphql graphql-ruby


【解决方案1】:

您可以在检查异常的 GraphQlController 的执行方法中添加逻辑。

例如,我们希望跳过“createSession”查询的授权,该查询应该为有效的用户名/密码组合生成 JWT 令牌。诀窍是创建“查询”对象,您可以在其中轻松访问正在调用的查询并确定它是否在跳过列表中。请原谅它是第一次通过的代码,就像概念证明一样。

#class GraphqlController < Application Controller

 @skips_authorization = ["createSession"]

  def execute
    variables = prepare_variables(params[:variables])
    query = params[:query]
    operation_name = params[:operationName]
    current_user = AuthorizeApiRequest.call(request.headers).result

    context = {
      current_user: current_user,
    }
    query_parse = GraphQL::Query.new(ApiSchema, query_string = params[:query])
    result = ApiSchema.execute(query, variables: variables, context: context, operation_name: operation_name)

    if current_user.present? || @skips_authorization.include?(query_parse.selected_operation.selections[0].name)
      render json: result
    else
      render json: {}, status: 401
    end
  rescue StandardError => e
    raise e unless Rails.env.development?
    handle_error_in_development(e)
  end

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 2021-05-30
    • 2016-05-06
    • 2018-10-20
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2021-11-10
    • 2017-09-01
    相关资源
    最近更新 更多