JSONAPI Resources Anchor

SchemaGenerator

Anchor::TypeScript::SchemaGenerator

Example

class Schema < Anchor::Schema
  resource UserResource
  resource PostResource
end
 
content = Anchor::TypeScript::SchemaGenerator.call(
  register: Schema.register,
  context: {},
  include_all_fields: true,
  exclude_fields: nil,
)
path = Rails.root.join("schema.ts")
File.open(path, "w") { |f| f.write(content) }

Anchor::TypeScript::SchemaGenerator.call returns a String of the TypeScript schema.

TypeScript type expressions for Anchor types can be found in Anchor::Types. Type expressions are generated by Anchor::TypeScript::Serializer.

See Quick Start for a full example.

.call Parameters

register

context

  • Type: Any
  • Default: {}

context passed to Resource.anchor_fetchable_fields.

include_all_fields

  • Type: Boolean
  • Default: nil

Use JSONAPI::Resource.fields irrespective of Resource.anchor_fetchable_fields.

exclude_fields

  • Type: Hash{Symbol=> Array<Symbol>}
    • Map of Resource.anchor_schema_name.to_sym to list of fields, e.g. { User: [:name, :posts] }.
  • Default: {}

Per resource list of fields to exclude from serialization.

On this page