将Ruby与GraphQL一起使用可以解决NameError的问题:未初始化的常量UserMutations?



所以我将 GraphQL与 Ruby on Rails 一起使用,并试图让 GraphiQL 工作,但我最终得到了 NameError:未初始化的常量 UserMutations。在我的mutation_type.rb中,我有以下内容:

Types::MutationType = GraphQL::ObjectType.define do
name 'Mutation'
field :signUp, field: UserMutations::SignUp
field :updateUser, field: UserMutations::Update
field :changePassword, field: UserMutations::ChangePassword
field :signIn, field: AuthMutations::SignIn
field :removeToken, field: AuthMutations::RemoveToken
end

然后在我的 user_mutations.rb 突变文件夹中,我有:

module Mutations::UserMutations
SignUp = GraphQL::ObjectType.define do
name 'signUp'
description 'Signing up a user'
input_field :first_name, types.String
input_field :last_name, types.String
input_field :email, types.String
input_field :password, types.String
input_field :password_confirmation, types.String
return_field :user, Types::UserType
return_field :messages, types[FieldErrorType]
resolve lambda(obj, inputs, ctx) do
user = User.new(inputs.to_params)
if user.save
user.update_tracked_fields(ctx[:request])
user.generate_access_token!
{ user: user }
else
{ messages: user.fields_errors }
end
end
end
Update = GraphQL::ObjectType.define do
name 'updateUser'
description 'Updating a user account'
input_field :first_name, types.String
input_field :last_name, types.String
input_field :email, types.String
return_field :user, Types::UserType
return_field :messages, types[FieldErrorType]
resolve lambda { |_obj, inputs, ctx|
current_user = ctx[:current_user]
if current_user.update(inputs.to_params)
{ user: current_user }
else
{ messages: current_user.fields_errors }
end
}
end
ChangePassword = GraphQL::ObjectType.define do
name 'changePassword'
description 'Changing the user password'
input_field :password, types.String
input_field :password_confirmation, types.String
input_field :current_password, types.String
return_field :user, Types::UserType
return_field :messages, types[FieldErrorType]
resolve lambda { |_obj, inputs, ctx|
params_with_password = inputs.to_h.symbolize_keys
current_user = ctx[:current_user]
if current_user.update_with_password(params_with_password)
{ user: current_user }
else
{ messages: current_user.fields_errors }
end
}
end
end

最初我有 user_mutations.rb:

module UserMutations
SignUp = GraphQL::Relay::Mutations.define do

但是发现我们没有使用 React,而是使用 Vue。如何正确获取我的mutation_type文件从user_mutations文件中获取信息?

编辑: 我还在我的mutation_type文件中尝试了以下内容:

Types::MutationType = GraphQL::ObjectType.define do
name 'Mutation'
field :signUp, Mutations: UserMutations::SignUp.field

field :signUp, field: UserMutations::SignUp.field

两者的问题相同。

我仍然想知道前端ReactVue与您的后端有什么关系。

您的命名空间有问题,Rails 希望找到user_mutations.rb错误NameError: uninitialized constant UserMutations匹配的文件名,因为它无法找到它,您只需要将 rails 引导到文件位置。

我也在使用 GraphQL,我在这个 url 中遵循这个命名空间:https://mattboldt.com/2019/01/07/rails-and-graphql/

无论哪种方式,请尝试以下操作:

field :signUp, field: Mutations::UserMutations::SignUp

module Mutations
class UserMutations # if it didn't work, try `< Types::MutationType`
SignUp = GraphQL::ObjectType.define do
# your SignUp block
end
end
end

或以上两者兼而有之。

就我而言,此错误是一个副作用,我将服务器日志滚动到山上,并在其中一个解析器中发现语法错误

最新更新