“未定义的方法`[] = nil:NilClass”与graphql

里卡多·莱瓦·西基奇

当我尝试通过 graphiql 登录时遇到此问题,控制台中出现的错误在以下行中:

突变/sign_in_user.rb:27:in `resolve'" context[:session][:token] = token

登录服务的配置如下:

    module Mutations
        class SignInUser < BaseMutation
        null true

        argument :credentials, Types::AuthProviderCredentialsInput, required: false

        field :token, String, null: true
        field :user, Types::UserType, null: true

        def resolve(credentials: nil)
            # basic validation
            return unless credentials

            user = User.find_by email: credentials[:email]
            puts '---userrr------- ' + user.email.to_s
            return Errors unless user

            # ensures we have the correct user
            return unless user
            return unless user.authenticate(credentials[:password])

            # use Ruby on Rails - ActiveSupport::MessageEncryptor, to build a token
            crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base.byteslice(0..31))
            token = crypt.encrypt_and_sign("user-id:#{ user.id }")
            puts '---------------- ' + token

            context[:session][:token] = token

            { user: user, token: token }
        end
        end
    end

当我第一次在 Rails 5.1.7 上运行但现在在 6.0 上运行时,我做了同样的事情,我想知道我现在使用的新版本是否会影响此配置。

里卡多·莱瓦·西基奇

:D,谢谢你们的帮助!我的错误是我没有将上下文定义到控制器/graphql -> 所以在文件中我只是添加了这一行来定义上下文对象。

  def execute
    variables = ensure_hash(params[:variables])
    query = params[:query]
    operation_name = params[:operationName]
    context = {
      # we need to provide session and current user
      session: session,
      current_user: current_user
    }
    result = AppOneSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
    render json: result
  rescue => e
    raise e unless Rails.env.development?
    handle_error_in_development e
  end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章