Blueprinter
This guide covers using Typelizer with Blueprinter.
Setup
class ApplicationBlueprint < Blueprinter::Base
include Typelizer::DSL
endFields and Associations
Define fields and associations as usual:
class UserBlueprint < ApplicationBlueprint
identifier :id
fields :name, :email
field :created_at, if: ->(_field, _user, options) { options[:admin] }
association :posts, blueprint: PostBlueprint
endTypelizer infers field types from the model (UserBlueprint → User). Use typelize_from when the model name can't be inferred. Fields and associations with if:, unless:, or exclude_if_nil: are optional.
Blueprinter renders an association as a list or a single object depending on the value, so Typelizer asks the model: a has_many association becomes an array. Without a model, set it with typelize:
typelize items: [ItemBlueprint, multi: true]
association :items, blueprint: ItemBlueprintComputed Fields
Annotate block fields with typelize:
class UserBlueprint < ApplicationBlueprint
typelize :string
field :full_name do |user, _options|
"#{user.first_name} #{user.last_name}"
end
endSee Manual Typing for full details on the typelize method.
Views
Each view becomes its own type, named after the blueprint and the view:
class UserBlueprint < ApplicationBlueprint
identifier :id
fields :name
view :extended do
field :email
end
endtype User = {
id: number;
name: string;
}
type UserExtendedView = {
id: number;
email: string;
name: string;
}An association rendered with a view (association :posts, blueprint: PostBlueprint, view: :summary) uses that view's type.
Limitations
- Transformers (
transform,default_transformers) can rename keys at render time, and Typelizer can't see them. For camelCase keys, use Typelizer'sproperties_transformer. default:,field_default, andassociation_defaultreplacenilat render time, but the types still include| null.- An association whose blueprint is a proc, or that uses the
:identifierview, is typed asunknown. root:andmeta:are render options, so the generated type describes the blueprint itself, not the wrapper.