ADVERTISEMENT

How do I specify polymorphic types with graphql-ruby?

That error means you need to define the resolve_type method in your app schema. It should accept an ActiveRecord model and the context, and return a GraphQL type.

AppSchema = GraphQL::Schema.define do
  resolve_type ->(record, ctx) do
    # figure out the GraphQL type from the record (activerecord)
  end
end

You could either implement this example which links a model to a type. Or you could create a class method or attribute on your models that refer to their types. e.g.

class ApplicationRecord < ActiveRecord::Base
  class << self
    attr_accessor :graph_ql_type
  end
end

class Writer < ApplicationRecord
  self.graph_ql_type = WriterType
end

AppSchema = GraphQL::Schema.define do
  resolve_type ->(record, ctx) { record.class.graph_ql_type }
end