Type Inference
Attributes
An attribute's type is inferable if:
- the underlying ActiveRecord model of the resource has a type definition for the attribute
- the method for the attribute is not overridden in the model
- the method for the attribute is not overridden in the resource
Example
JSONAPI::Resource
attributes are inferred via introspection of the resource's
underlying ActiveRecord model.
ActiveRecord::Base.columns_hash[attribute]
is used to get the SQL type and is
then mapped to an Anchor::Type
in
Anchor::Types::Inference::ActiveRecord::SQL.from
.
Relevant Configs
use_active_record_validations
- use ActiveModel validations on the ActiveRecord model to determine nullability
ar_column_to_type
- custom type mapping
Relationships
Example
The .anchor_schema_name
of the related resource is used as the type identifier in the TypeScript serializer.
The nullability and cardinality are determined by the underlying ActiveRecord model's reflections, ActiveRecord::Base.reflections[name]
.
ActiveRecord Association | Inferred Anchor::Type (TypeScript expression) |
---|---|
belongs_to :relation | Relation |
belongs_to :relation, optional: true | Maybe<Relation> |
has_one :relation | Maybe<Relation> |
has_many :relations | Array<Relation> |
has_and_belogs_to_many :relations | Array<Relation> |
Relevant Configs
infer_nullable_relationships_as_optional
- whether to use e.g.
{ relation?: Relation }
or{ relation: Maybe<Relation> }
- whether to use e.g.