ruby-on-rails - 运行 graphQL 突变的正确方法是什么?
问题描述
我正在使用 Rails 中的 GraphQL 并发送突变以在 Graphiql 中创建用户。我得到的只是错误。
我正在以下文件中设置突变类型和逻辑。我将只展示我的 createUser 突变,因为那是我无法克服的突变。
创建用户.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
突变类型.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)
请教我如何进行正确的突变调用,或者至少为我指明正确的方向。谢谢!
解决方案
发现了问题。
由于设计需要密码确认,但我的实际创建用户逻辑不需要它,我可以在使用信息创建用户之前删除该密钥。我修复了它:
创建用户.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:)
userAttributes = attributes.to_kwargs
if userAttributes[:password] == userAttributes[:passwordConfirmation]
userAttributes.delete(:passwordConfirmation)
end
user = User.new(userAttributes)
# userAttributes.attributes.except
if user.save
{
user: user,
errors: []
}
else
{
user: nil,
errors: user.errors.full_messages
}
end
end
end
推荐阅读
- javascript - 谷歌图表上的 ASP.NET MVC 不匹配错误
- r - RSelenium 抓取返回奇怪的结果
- debugging - gdb 错误-“不是可执行格式:文件格式无法识别”
- python - Python 获取随机唯一 N 对
- r - RSelenium:从 Google 搜索中单击 for 循环中的后续链接
- sql-server - SQL Server:图形/层次结构 SQL 结构
- jquery - 使用 jQuery 修剪 itemprop 属性
- angular - 如何使用辅助路由序列化 Angular URL?
- c++ - 无法访问或更新具有分段错误的类成员
- ios - 为什么我们不能把 UIView 的子视图放在它的 CALayer 前面?