【发布时间】:2020-04-29 02:17:46
【问题描述】:
我正在使用 Rails 中的 GraphQL 并发送一个突变以在 Graphiql 中创建一个用户。我得到的只是错误。
我正在以下文件中设置突变类型和逻辑。我将只显示我的 createUser 突变,因为那是我无法通过的突变。
create_user.rb
class Mutations::User::CreateUser < Mutations::BaseMutation
argument :attributes, Types::UserInputType, required: true
field :user, Types::UserType, null: false
field :errors, [String], null: false
def resolve(attributes:)
user = User.new(attributes.to_kwargs)
if user.save
{
user: user,
errors: []
}
else
{
user: nil,
errors: user.errors.full_messages
}
end
end
end
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
field :fname, String, null: true
field :lname, String, null: true
field :email, String, null: true
field :password, String, null: true
field :dpsst, String, null: true
field :reports, [Types::ReportType], null: true
field :reports_count, Integer, null: true
field :token, String, null: false
def reports_count
object.reports.size
end
end
end
user_input_type.rb
description "Attributes to create a user."
argument :fname, String,'Firstname of user', required: true
argument :lname, String,'Lastname of user', required: true
argument :email, String,'Email of user', required: true
argument :password, String,'Password of user', required: true
argument :passwordConfirmation, String, 'Password confirmation', required: true
argument :dpsst, String,'dpsst number of user', required: false
end
mutation_type.rb
module Types
class MutationType < Types::BaseObject
# TODO: remove me
# field :test_field, String, null: false,
# description: "An example field added by the generator"
# def test_field
# "Hello World"
# end
field :create_user, mutation: Mutations::User::CreateUser
field :login, mutation: Mutations::User::Login
field :token_login, mutation: Mutations::User::TokenLogin
field :logout, mutation: Mutations::User::Logout
field :update_user, mutation: Mutations::User::UpdateUser
field :reset_password, mutation: Mutations::User::ResetPassword
field :send_reset_password_instructions, mutation: Mutations::User::SendResetPasswordInstructions
field :unlock, mutation: Mutations::User::Unlock
field :resend_unlock_instructions, mutation: Mutations::User::ResendUnlockInstructions
end
end
下面是我在下面尝试过的几个突变布局。我只是不知道如何写他们100%!或者可能我的 graphiql 搞砸了,因为我正在插入应该根据互联网工作的突变。我怀疑它可能与 to_kwargs 方法有关......
突变:
mutation createUserMutation($userInput: CreateUserInput!){
createUser(input: $userInput) {
user {
id
fname
}
}
}
"userInput": {
"email":"test@gmail.com",
"password": "password",
"fname":"Jake",
"lname":"Tester",
"passwordConfirmation":"password",
"dpsst":"75323",
}
}
mutation createUserMutation($attributes: UserInput!){
createUser(input: {attributes: $attributes}) {
user {
token
id
fname
lname
}
}
}
{
"attributes": {
"email":"test@gmail.com",
"password": "password",
"fname":"Jake",
"lname":"Tester",
"passwordConfirmation":"password",
"dpsst":"75323"
}
}
在最近的尝试中,我收到以下错误:
{
"error": {
"message": "unknown attribute 'passwordConfirmation' for User.",
"backtrace":...(I can post this if you need it)
请教我如何进行正确的突变调用,或者至少为我指明正确的方向。谢谢!
【问题讨论】:
-
Types::UserType? -
上面添加了用户类型!
-
删除 passwordConfirmation 时,我收到错误 ``` "errors": [ { "message": "Variable $attributes of type UserInput! was provided invalid value for passwordConfirmation (Expected value to not be null )",```
-
因为需要在输入类型中定义
标签: ruby-on-rails ruby graphql