Graphql突变错误:“字段'createUser'缺少必需的参数:输入”

时间:2020-02-28 19:25:36

标签: ruby-on-rails graphql graphiql

我正在尝试遵循本文,了解如何使用GraphQl https://www.howtographql.com/graphql-ruby/4-authentication

在Rails服务器上创建变异

但是,我被困在CreateUser Mutation步骤中,在GraphiQL中尝试它时,我得到了跟随错误哈希值:

{
  "errors": [
    {
      "message": "Field 'createUser' is missing required arguments: input",
      "locations": [
        {
          "line": 45,
          "column": 3
        }
      ],
      "path": [
        "mutation CreateUser",
        "createUser"
      ],
      "extensions": {
        "code": "missingRequiredArguments",
        "className": "Field",
        "name": "createUser",
        "arguments": "input"
      }
    },
    {
      "message": "Field 'createUser' doesn't accept argument 'username'",
      "locations": [
        {
          "line": 46,
          "column": 5
        }
      ],
      "path": [
        "mutation CreateUser",
        "createUser",
        "username"
      ],
      "extensions": {
        "code": "argumentNotAccepted",
        "name": "createUser",
        "typeName": "Field",
        "argumentName": "username"
      }
    },
    {
      "message": "Field 'createUser' doesn't accept argument 'authProvider'",
      "locations": [
        {
          "line": 47,
          "column": 5
        }
      ],
      "path": [
        "mutation CreateUser",
        "createUser",
        "authProvider"
      ],
      "extensions": {
        "code": "argumentNotAccepted",
        "name": "createUser",
        "typeName": "Field",
        "argumentName": "authProvider"
      }
    },
    {
      "message": "Variable $username is declared by CreateUser but not used",
      "locations": [
        {
          "line": 44,
          "column": 1
        }
      ],
      "path": [
        "mutation CreateUser"
      ],
      "extensions": {
        "code": "variableNotUsed",
        "variableName": "username"
      }
    },
    {
      "message": "Variable $email is declared by CreateUser but not used",
      "locations": [
        {
          "line": 44,
          "column": 1
        }
      ],
      "path": [
        "mutation CreateUser"
      ],
      "extensions": {
        "code": "variableNotUsed",
        "variableName": "email"
      }
    },
    {
      "message": "Variable $password is declared by CreateUser but not used",
      "locations": [
        {
          "line": 44,
          "column": 1
        }
      ],
      "path": [
        "mutation CreateUser"
      ],
      "extensions": {
        "code": "variableNotUsed",
        "variableName": "password"
      }
    }
  ]
}

我只是遵循文章中的代码,即我的文件:

create_user.rb

module Mutations
  class CreateUser < BaseMutation
    # often we will need input types for specific mutation
    # in those cases we can define those input types in the mutation class itself
    class AuthProviderSignupData < Types::BaseInputObject
      argument :credentials, Types::AuthProviderCredentialsInput, required: false
    end

    argument :username, String, required: true
    argument :auth_provider, AuthProviderSignupData, required: false

    type Types::UserType

    def resolve(username: nil, auth_provider: nil)
      User.create!(
        username: username,
        email: auth_provider&.[](:credentials)&.[](:email),
        password: auth_provider&.[](:credentials)&.[](:password)
      )
    end
  end
end

user_type.rb

module Types
  class UserType < BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :username, String, null: false
    field :photo, String, null: true
    field :phone, String, null: false
    field :island, IslandType, null: false, method: :island
    field :archipel, ArchipelType, null: false, method: :archipel

    field :created_at, String, null: false
    field :updated_at, String, null: false
  end
end

我不知道这种“输入”东西来自哪里。

1 个答案:

答案 0 :(得分:6)

在没有意识到的情况下,我使用一个使用Relay的配置使我的项目无效。

通过在我的** _ schema.rb文件中注释此代码,它再次起作用。

  # Opt in to the new runtime (default in future graphql-ruby versions)
  # use GraphQL::Execution::Interpreter
  # use GraphQL::Analysis::AST

  # Add built-in connections for pagination
  # use GraphQL::Pagination::Connections

以及base_mutation.rb中的这些行,并替换为这些行。

  # class BaseMutation < GraphQL::Schema::RelayClassicMutation
  #   argument_class Types::BaseArgument
  #   field_class Types::BaseField
  #   input_object_class Types::BaseInputObject
  #   object_class Types::BaseObject
  # end

  class BaseMutation < GraphQL::Schema::Mutation
    null false
  end