ruby-on-rails RoR Graphql Types::BaseObject NameError

ctzwtxfj  于 2023-04-08  发布在  Ruby
关注(0)|答案(1)|浏览(129)

当向我的rails后端发出graphql API请求时,我在app/graphql/types/query_type.rb中得到以下错误:

Started POST "/graphql" for 127.0.0.1 at 2023-04-06 13:27:07 +0200
Processing by GraphqlController#execute as */*
  Parameters: {...}
Completed 500 Internal Server Error in 266ms (ActiveRecord: 0.0ms | Allocations: 3660)


NameError (uninitialized constant Types::BaseObject):

app/graphql/types/query_type.rb:2:in `<module:Types>'
app/graphql/types/query_type.rb:1:in `<main>'
...

这是QueryType类:

module Types
  class QueryType < Types::BaseObject
    field :discord_user, DiscordUserType, null: true do
      argument :id, ID, required: true
    end

    def discord_user(id:)
      DiscordUser.find(id)
    end

    field :discord_users, [DiscordUserType], null: false

    def discord_users
      DiscordUser.all
    end
  end
end

我自己没有发现命名约定的错误,因为BaseObject是由graphql gem提供的,我不知道为什么会有错误。

pinkon5k

pinkon5k1#

你好像嵌套错了。

module Types # Types here 
  class QueryType < Types::BaseObject # Types
    field :discord_user, DiscordUserType, null: true do
      argument :id, ID, required: true
    end

    def discord_user(id:)
      DiscordUser.find(id)
    end

    field :discord_users, [DiscordUserType], null: false

    def discord_users
      DiscordUser.all
    end
  end
end

尝试

module Types  
  class QueryType < BaseObject 
    field :discord_user, DiscordUserType, null: true do
      argument :id, ID, required: true
    end

    def discord_user(id:)
      DiscordUser.find(id)
    end

    field :discord_users, [DiscordUserType], null: false

    def discord_users
      DiscordUser.all
    end
  end
end

相关问题