I want to make a query return a union type
My union type:
class LeadType < ::Types::BaseUnion
description "Lead"
possible_types WebsiteLeadType
def self.resolve_type(object, _context)
case object.lead_type
when "website"
WebsiteLeadType
else
raise "Can't find type for lead"
end
end
end
I tried to use the type in a query:
class LeadQuery < ::Queries::BaseQuery
argument :lead_id, ID, required: true, description: "Lead ID"
type Types::LeadType, null: true
def resolve(lead_id:, token:)
lead = Lead.find(lead_id)
end
end
But I get an error
NameError:
uninitialized constant Types::BaseEdge
edge_type_class(Types::BaseEdge)
^^^^^^^^^^
Did you mean? Types::BaseField
# ./app/graphql/types/base_object.rb:3:in `<class:BaseObject>'
The query is made available from a query type:
class QueryType < ::Types::BaseObject
field :lead, resolver: LeadQuery
end
I want to reuse the LeadType union type in other queries and mutations, so the resolve_type logic needs to be reusable and in one file.
A fix is to add the type when adding the query to the main type and remove it from the resolver class