JSONAPI Resources Anchor

Types

Note: The TypeScript type expression is derived from Anchor::TypeScript::Serializer for TypeScript schema generation.

Legend

  • T can be any of the types defined in the table + Anchor::Types::Relationship (e.g. Anchor::Types::String)
    • in the type expression column it's the serialized version (e.g. string)
  • Ts: T[]
  • props: Array<Anchor::Types::Property>
Anchor::TypesTypeScript type expression
Stringstring
Integernumber
Floatnumber
BigDecimalstring
Booleanboolean
Nullnull
Unknownunknown
Maybe.new(T)Maybe<T>
Array.new(T)Array<T>
RecordRecord<string, unknown>
Record.new(T)Record<string, T>
Reference.new(name)name (directly used as type identifier)
Literal.new(value)"#{value}" if string, else value.to_s
EnumEnum.anchor_schema_name (directly used as type identifier)
Union.new(Ts)Ts[0] | Ts[1] | ...
Object.new(props){ [props[0].name]: props[0].type, [props[1].name]: props[1].type, ... }

Anchor::Types::Relationship

module Anchor::Types
  # @!attribute [r] resource
  #   @return [JSONAPI::Resource, Anchor::Types::Reference, NilClass] the associated resource
  # @!attribute [r] resources
  #   @return [Array<JSONAPI::Resource>, Array<Anchor::Types::Reference>, NilClass] union of associated resources
  # @!attribute [r] null
  #   @return [Boolean] whether the relationship can be `null`
  # @!attribute [r] null_elements
  #   @return [Boolean] whether the elements in a _many_ relationship can be `null`
  Relationship = Struct.new(:resource, :resources, :null, :null_elements, keyword_init: true)
end

Anchor::Types::Object

class CustomPayload < Anchor::Types::Object
  property :id, Anchor::Types::String, optional: true, description: "ID of payload."
end
serlialization output
{
  /** ID of payload. */
  id?: string;
}

.property(name, type, optional: nil, description: nil)

  • name: String
  • type: member of Anchor::Types
  • optional: Boolean
    • whether the property is optional, e.g. if true, { name?: string }
      • as opposed to asserting Anchor::Types::Maybe.new(Anchor::Types::String) to get { name: Maybe<string> } serialized
  • description: String
    • serialized as a comment in TypeScript

Anchor::Types::Enum

class UserRoleEnum < Anchor::Types::Enum
  anchor_schema_name "UserRole" # optional. default is demodulized name minus Enum
 
  # First argument is the enum member identifier that gets camelized in TypeScript serializer
  # Second argument is the value
  value :admin, "admin"
  value :content_creator, "content_creator"
  value :external, "external"
  value :guest, "guest"
  value :system, "system"
end

Alternatively, you can define it dynamically

class User < ApplicationRecord
  enum :role, {
    admin: "admin",
    conent_creator: "content_creator",
    external: "external",
    guest: "guest",
    system: "system",
  }
end
 
class UserRoleEnum < Anchor::Types::Enum
  User.roles.each { |key, val| value key, val }
end
serlialization output
export enum UserRole {
  Admin = "admin",
  ContentCreator = "content_creator",
  External = "external",
  Guest = "guest",
  System = "system",
}

Very similar to rmosolgo/graphql-ruby enums.

On this page