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