ruby-on-railsrubygraphql

RoR Graphql Types::BaseObject NameError


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.


Solution

  • 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