英文:
RoR Graphql Types::BaseObject NameError
问题
以下是您提供的内容的翻译部分:
当向我的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 (未初始化的常量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提供,我不知道为什么会出现错误。
英文:
When making a graphql API request to my rails backend I get the following error in my 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>'
...
This is the QueryType class:
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
I myself did not find an error with the naming conventions and since the BaseObject is provided by the graphql
gem I don't know why there should be an error with it.
答案1
得分: 0
你的代码中似乎嵌套错了。
module Types # 这里的 Types
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
英文:
you seem to have nested it wrongly.
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
try
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
答案2
得分: 0
Did you define BaseObject?
Make sure you have BaseObject defined in
-> app/graphql/types/base_object.rb
module Types
class BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
end
end
英文:
Did you define BaseObject?
Make sure you have BaseObject defined in
-> app/graphql/types/base_object.rb
module Types
class BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
end
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论