Documentation for LLMs on building APIs with the Apia framework
Apia provides built-in scalar types for common data types and supports defining custom scalars.
Reference these by symbol in field and argument definitions:
| Symbol | Ruby Type | Input Parsing | Output Cast | Description |
|---|---|---|---|---|
:string |
String | .to_s |
.to_s |
Text values |
:integer |
Integer | Parsed from string via regex /\A-?\d+\z/ |
.to_i |
Whole numbers |
:boolean |
TrueClass/FalseClass | true, "true", "yes", 1, "1" are true; false, "false", "no", 0, "0" are false |
true/false | Boolean values |
:decimal |
Float | Parsed from float/integer/string | .to_f |
Decimal numbers |
:unix_time |
Time | Time.at(integer) |
.to_i |
Unix timestamps (seconds since epoch) |
:date |
Date | Parsed from ISO 8601 yyyy-mm-dd |
.strftime('%Y-%m-%d') |
Calendar dates |
:base64 |
String | Base64.decode64 |
Base64.encode64 |
Base64-encoded binary |
field :id, :string
field :name, :string
field :age, :integer
field :active, :boolean
field :score, :decimal
field :created_at, :unix_time
field :birth_date, :date
field :avatar, :base64
argument :name, :string, required: true
argument :age, :integer
argument :active, :boolean, default: true
argument :amount, :decimal
argument :since, :unix_time
argument :date, :date
Wrap any type in brackets for arrays:
field :tags, [:string]
field :scores, [:integer]
argument :ids, [:string]
Apia has a unified type system. Anywhere a type is expected, you can use:
:string, :integer, etc.)Objects::User)Enums::StatusEnum)ArgumentSets::UserProperties)| Type | Usable as Field? | Usable as Argument? |
|---|---|---|
| Scalar | Yes | Yes |
| Enum | Yes | Yes |
| Object | Yes | No |
| ArgumentSet | No | Yes |
| Polymorph | Yes | No |
You can define custom scalar types with parse, cast, and validation logic:
class MoneyScalar < Apia::Scalar
name "Money"
description "A monetary value in cents"
parse do |value|
value.to_i
end
cast do |value|
value.to_i
end
validator do |value|
value.is_a?(Integer) && value >= 0
end
end
parse(&block) - Convert input (from JSON/params) to the Ruby typecast(&block) - Convert the Ruby value to the output formatvalidator(&block) - Return true if the value is validBy default, fields are non-nullable. Use null: true to indicate a field may return nil:
field :email, :string, null: true
field :deleted_at, :unix_time, null: true
field :parent_id, :string, null: true