【问题标题】:Unable to call Graphql API using input parameters无法使用输入参数调用 Graphql API
【发布时间】:2020-11-16 23:47:41
【问题描述】:

我可以通过像这样传入输入直接调用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
}

【问题讨论】:

    标签: ruby graphql graphql-ruby


    【解决方案1】:

    你的错误信息已经告诉你发生了什么:)

    {
      "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 是默认设置的,并且会生成 firstNamelastName 等字段。因此,您的输入变量现在必须如下所示:

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

    如果您想坚持使用first_name,您必须传递camelize: false 并相应地更改您的有效负载。

    使用文档浏览器检查您的 GraphQL API,以了解您的 API 需要什么样的字段以及应如何命名。

    【讨论】:

    • 我没有将它作为first_name 传递,而是作为firstName 在自动生成的CreateUserInput 中传递。我会在上面发帖
    • @AntarrByrd 您发布的错误消息清楚地表明您发送first_namelast_name 而不是firstNamelastName。查看值字段...
    • 我明白这一点,但我不明白。
    猜你喜欢
    • 2020-12-20
    • 2019-11-25
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2021-11-06
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多