RoR Graphql Types::BaseObject NameError

huangapple go评论55阅读模式
英文:

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 &lt; 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 &quot;/graphql&quot; 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 `&lt;module:Types&gt;&#39;
app/graphql/types/query_type.rb:1:in `&lt;main&gt;&#39;
...

This is the QueryType class:

module Types
  class QueryType &lt; 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 &lt; 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 &lt; 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 &lt; 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 &lt; 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 &lt; 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 &lt; GraphQL::Schema::Object
    field_class Types::BaseField
  end
end

huangapple
  • 本文由 发表于 2023年4月6日 19:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定