SchemaSerializable
Anchor::SchemaSerializable
Include this concern in your JSONAPI::Resource
classes to enable type annotations and serialization via an Anchor schema generator.
class ApplicationResource < JSONAPI::Resource
abstract
include Anchor::SchemaSerializable
end
.anchor_meta_schema
Example
class UserResource < ApplicationResource
class MetaSchema < Anchor::Types::Object
property :comments_count, Anchor::Types::Integer
property :posts_count, Anchor::Types::Integer
end
anchor_meta_schema MetaSchema
end
.anchor_links_schema
Example
class UserResource < ApplicationResource
class LinkSchema < Anchor::Types::Object
property :self, Anchor::Types::String
property :some_url, Anchor::Types::String
end
anchor_links_schema LinkSchema
end
.attribute
JSONAPI::Resource.attribute
.Adds an optional second argument to annotate it with a type from Anchor::Types
.
Options hash becomes third argument if annotation included. Original JSONAPI::Resource
signature remains the same otherwise.
Example
class UserResource < ApplicationResource
attribute :name
attribute :computed_string, Anchor::Types::String
end
.relationship
JSONAPI::Resource.relationship
.Adds an optional second argument to annotate it with an
Anchor::Types::Relationship
.
Options hash becomes third argument if annotation included. Original JSONAPI::Resource
signature remains the same otherwise.
Example
class UserResource < ApplicationResource
relationship :posts, to: :many
relationship :comments, Anchor::Types::Relationship.new(resource: CommentResource), to: :many
end
.anchor_fetchable_fields
- Type:
Any -> Array<Symbol>
Determines the fields to be serialized for a resource. By default, JSONAPI::Resource.fields
is used.
Useful when you want to generate a context specific schema.
Example
class UserResource < ApplicationResource
attribute :name
attribute :address
def self.anchor_fetchable_fields(context)
if context[:role] == :admin
fields
else
[:name]
end
end
end
Anchor::TypeScript::SchemaGenerator.call(register: Schema.register, context: { role: :admin })
.anchor_schema_name
- Type:
String | (String -> String)
The identifier used when referencing the resource during serialization.
Example
class UserResource < ApplicationResource
anchor_schema_name "CustomUser"
end
UserResource.anchor_schema_name # => "CustomUser"
By default, the demodulized name of the class minus "Resource" is used as the schema name for the resource, e.g. SomeModule::UserResource => User
.
Relevant Configs
use_type_as_schema_name
- When
true
, theString#classify
'dtype
thatJSONAPI::Resource
determines from the resource will be used.
- When
.anchor_exclude_from_schema
- Type:
({ reason: String } -> String) | String
Whether to exclude this resource from the schema. By default, if not defined on the resource, the value is "Abstract."
if JSONAPI::Resource._abstract
is true
, nil
otherwise.
Example
class CommentableResource < ApplicationResource
anchor_exclude_from_schema reason: :polymorphic
end
CommentableResource.anchor_exclude_from_schema # => :polymoprhic
CommentableResource.anchor_excluded_from_schema? # => true
# implementation: anchor_exclude_from_schema.present?
Useful for when you want to generate the schema dynamically.