0

我可以通过像这样传入输入直接调用graphql api

mutation {
  createUser(input: {
    email: "user+2@example.com",
    password: "Password123!",
    passwordconfirmation: "Password123!",
    firstName: "Richard",
    lastName: "James"
  }){
    user{
      firstName
      lastName
      email
    }
  }
}

但是如果我使用这样的输入参数传递它

mutation createUser($input: CreateUserInput!) {
  createUser(input: $input){
    user{
      firstName
      lastName
      email
    }
  }
}

我收到这个错误

{
  "errors": [
    {
      "message": "Variable $input of type CreateUserInput! was provided invalid value for first_name (Field is not defined on CreateUserInput), last_name (Field is not defined on CreateUserInput)",
      "locations": [
        {
          "line": 1,
          "column": 21
        }
      ],
      "extensions": {
        "value": {
          "passwordconfirmation": "Password123!",
          "password": "Password123!",
          "email": "user@example.com",
          "first_name": "Richard",
          "last_name": "James"
        },
        "problems": [
          {
            "path": [
              "first_name"
            ],
            "explanation": "Field is not defined on CreateUserInput"
          },
          {
            "path": [
              "last_name"
            ],
            "explanation": "Field is not defined on CreateUserInput"
          }
        ]
      }
    }
  ]
}

创建用户突变

module Mutations
  class CreateUser < Mutations::BaseMutation
    argument :email, String, required: true
    argument :password, String, required: true
    argument :passwordconfirmation, String, required: true
    argument :first_name, String, required: false
    argument :last_name, String, required: false

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

    def resolve(args)
      user = User.new(password: args[:password], password_confirmation: args[:passwordconfirmation], email: args[:email])
      user.build_profile
      user.save
      if args[:first_name] || args[:last_name]
        user.profile.first_name = args[:first_name]
        user.profile.last_name = args[:last_name]
        user.profile.save
      end

      # current_user needs to be set so authenticationToken can be returned
      context[:current_user] = user

      MutationResult.call(
        obj: { user: user },
        success: user.persisted?,
        errors: user.errors
      )
    rescue ActiveRecord::RecordInvalid => e
      GraphQL::ExecutionError.new(
        "Invalid Attributes for #{e.record.class.name}: " \
        "#{e.record.errors.full_messages.join(', ')}"
      )
    end
  end
end

创建用户输入

input: CreateUserInput!
Autogenerated input type of CreateUser

type CreateUserInput {
  email: String!
  password: String!
  passwordconfirmation: String!
  firstName: String
  lastName: String
  clientMutationId: String
}
4

1 回答 1

0

您的错误消息已经告诉您发生了什么:)

{
  "errors": [
    {
      "message": "Variable $input of type CreateUserInput! was provided invalid value for first_name (Field is not defined on CreateUserInput), last_name (Field is not defined on CreateUserInput)",**
    }
    ...

这意味着,您的CreateUserInput类型与您的有效负载不匹配。您尚未发布您的CreateUserInput,但它似乎既不包含first_name也不包含last_name。我假设CreateUserInput看起来像这样:

class CreateUserInput < BaseInput 
  argument :email, String, required: true
  argument :password, String, required: true
  argument :password_confirmation, String, required: true, camelize: true
  argument :first_name, String, required: false, camelize: true
  argument :last_name, String, required: false, camelize: true
end 

请注意,这camelize: true是默认设置的,并且会生成 和firstName等字段lastName。因此,您的输入变量现在必须如下所示:

input: {
  "firstName": "First",
  "lastName": "Last",
  ...
}

如果你想坚持下去,first_name你必须通过camelize: false并相应地改变你的有效载荷。

使用文档资源管理器检查您的 GraphQL API,以查看您的 API 需要什么样的字段以及应该如何命名它们。

于 2020-11-17T11:57:58.743 回答