ruby-on-railsrubygraphqlgraphql-ruby

Graphql-Ruby: Creating interfaces that objects can inherit fields from


    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

Solution

  • I figured out the issue. I had put implements Types:SomeProgram instead of implements Types::SomeProgram. I was missing a colon.