3

所以我将 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

两者的问题相同。

4

1 回答 1

0

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

您的命名空间有问题,Rails 期望找到一个与产生user_mutations.rb的错误相匹配的文件名,NameError: uninitialized constant UserMutations因为它无法找到它,您只需将导轨引导到文件位置。

我也在使用 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

或以上两者。

于 2019-06-15T12:39:00.517 回答