module Types::ProgramType
include Types::BaseInterface
description "Objects which inherit from Program"
graphql_name "Program"
orphan_types Types::SomeProgramType, Types::AnotherProgramType, Types::ThirdProgramType
field :id, ID, null: false
field :type, Integer, null: false
field :name, String, null: false
definition_methods do
def self.resolve_type(object, _context)
case object.class
when SomeProgram then SomeProgramType
when AnotherProgram then AnotherProgramType
when ThirdProgram then ThirdProgramType
else
raise "Unknown program type"
end
end
end
end
module Types
class SomeProgramType < Types::BaseObject
implements Types:ProgramType
field :description, String, null: false
end
end
I also added queries for SomeProgram types in the query_type file. I was under the impression that adding "implement " to the object types, would allow them to inherit the fields from the interface (per this link: https://graphql-ruby.org/type_definitions/interfaces) and I would be able to query like this:
query {
someProgram(id: 1) {
name
type
description
}
}
But I am getting errors in graphiql, like "Field 'name' doesn't exist on type 'SomeProgram'". What am I missing?
Update:
My QueryType class:
class QueryType < Types::BaseObject
# Add `node(id: ID!) and `nodes(ids: [ID!]!)`
include GraphQL::Types::Relay::HasNodeField
include GraphQL::Types::Relay::HasNodesField
field :some_programs, [SomeProgramType], null: false,
description: "all some programs"
def some_programs
SomeProgram.all
end
field :some_program, SomeProgramType, null: false do
argument :id, ID, required: true
end
def some_program(id:)
SomeProgram.find(id)
end
end
CodePudding user response:
Can you also share how you expose someProgram
in your query type?
You can also debug this by looking into the schema if you are using GraphiQL app. You can see the return type of someProgram
which should be type of ProgramType
.
You can also update your query to include __typename
so maybe start with
query {
someProgram(id: 1) {
__typename
}
}
First to see what is the return type and if it's being properly handled in the resolve_type
CodePudding user response:
I figured out the issue. I had put implements Types:SomeProgram
instead of implements Types::SomeProgram
. I was missing a colon.