Types
Overview
Types are used to describe the data to be processed. They constrain the range of value that can be accepted as input data or expected as result on each computation running in a runtime.
All the type definition functions are available under the t
namespace.
import { t } from "@typegraph/sdk";
Scalar types
Type | GraphQL type | Description |
---|---|---|
t.integer() | Int | Represents signed 32-bit integers. |
t.float() | Float | Represents signed double-precision values as specified by IEEE 754. |
t.boolean() | Boolean | Represents true or false . |
t.string() | String | Represents textual data as UTF-8 character sequences. |
t.file() | — | Represents a file for upload. |
The following scalar types are aliases to a t.string()
type with a specific format.
t.uuid()
t.json()
t.email()
t.uri()
t.hostname()
t.ean()
t.phone()
t.date()
t.datetime()
Non-scalar types
Type | GraphQL type | Description |
---|---|---|
t.optional() | nullable | Represents a value that may be null . |
t.list() | list | Represents a list of values. |
t.struct() | interface | Represents a structured data value, consisting of fields which map to typed values. |
t.union() | union | Represents a value which can be one of a set of specified types. |
t.either() | union | Represents a value which can match one and only one of a set of specified types. |
t.func | — | Represents an operation that has to be performed on the typegate. |
Type constraints
Type constraints define an additional narrowing of the range of values that can be accepted for the type.
They can be passed in an object after the last required parameter on the type definition.
See the reference for each type below for the list of constraints available.
Example: The min
constraint on the type t.integer()
// represents integers greater than or equal to `12`
t.integer({ min: 12 });
Enumerations
t.integer()
, t.float()
, t.string()
, t.struct(...)
can have a list of enumerated values.
In this case the only valid values for the type are the ones that are listed in the enumeration.
Examples:
t.integer().enum([1, 2, 3])
t.float().enum([1.2, 3.6, 12.4])
t.string().enum(["hello", "halo", "salut"])
t.struct({ a: t.string(), b: t.integer() })
.enum([{ a: "one", b: 1 }, { a: "two", b: 2 }])
t.enum(array)
is an alias to t.string().enum(array)
.
Names and type references
Each type has a unique name. If none is set, a random name will be generated during typegraph serialization.
Injection
Types
t.boolean()
The t.boolean()
type represents boolean values, true
or false
.
t.integer()
The t.integer()
type represents 32-bit integers.
t.integer([constraints]);
Constraints
Constraint | Description |
---|---|
min | The minimum value of the integer. |
max | The maximum value of the integer. |
x_min | The minimum value of the integer, exclusive. |
x_max | The maximum value of the integer, exclusive. |
multiple_of | The integer must be a multiple of this value. |
Examples
// non-negative integer
const nonNegative = t.integer({ min: 0 });
// an integer in the range [18, 120)
const adultAge = t.integer({ min: 18, x_max: 120 });
// an even integer
const even = t.integer({ multiple_of: 2 });
t.float()
t.float([constraints]);
The t.float()
type represents numbers, stored in double precision floating-point format (IEEE 754).
Constraints
The t.float()
type has the same constraints as t.integer()
. See integer constraints.
t.string()
t.string([constraints]);
The t.string()
type represents textual data represented as UTF-8 character sequences.
Constraints
Constraint | Type | Description |
---|---|---|
min | Integer | Minimum length of the string. |
max | Integer | Maximum length of the string. |
pattern | String | Regular expression pattern that the string must match. |
format | String | JSON schema format that the string must match. See below for the list of supported formats. |
Supported formats
Here is the list of supported formats:
uuid
json
email
uri
hostname
ean
phone
date
date-time
Examples
// a non-empty string of maximum 64 characters
t.string({ min: 1, max: 64 });
// an email address
t.string({ format: "email" });
// a json data
t.string({ format: "json" });
Aliases
Alias | Equivalent declaration |
---|---|
t.uuid() | t.string({ format: "uuid" }) |
t.email() | t.string({ format: "email" }) |
t.uri() | t.string({ format: "uri" }) |
t.json | t.string({ format: "json" }) |
t.ean() | t.string({ format: "ean" }) |
t.phone() | t.string({ format: "phone" }) |
t.date() | t.string({ format: "date" }) |
t.datetime() | t.string({ format: "date-time" }) |
t.file()
t.file([constraints]);
The t.file()
represents files for upload.
Type Constraints
Constraint | Type | Description |
---|---|---|
min | Integer | Minimum size of the file in bytes. |
max | Integer | Maximum size of the file in bytes. |
allow | Array of strings | List of allowed content-type s |
Examples
// A file of a minimum size of 1KB
t.file({ min: 1024 });
// A JPEG or PNG file less than 2KB
t.file({ max: 2048, allow: ["image/jpeg", "image/png"] });
t.optional()
t.optional(item_type);
item_type.optional(); // equivalent syntactic sugar
Default value
If the type is used as an input type, the default value can be specified in the type definition.
t.string().optional({ defaultItem: "default value" });
t.list()
t.list(item_type, [constraints]);
The t.list()
type represents a sequence of values of the same type.
Constraints
Constraint | Type | Description |
---|---|---|
min | Integer | Minimum number of items. |
max | Integer | Maximum number of items. |
unique_items | Boolean | Whether the items must be unique. |
Examples
# A list of strings
t.list(t.string())
# A list of unique strings
t.list(t.string(), { uniqueItems: true })
# A list of strings with at least 3 items
# and at most 10 items
t.list(t.string(), { min: 3, max: 10 })
t.struct()
t.struct(properties, [constraints]);
The t.struct()
type represents structured data, consisting of nemed properties with pre-defined types.
All the prperies are required unless the corresponding type is optional. In that case, the field is allowed to be missing from the value or be null
.
Constraints
Constraint | Type | Description |
---|---|---|
min | Integer | Minimum number of fields. |
max | Integer | Maximum number of fields. |
Examples
// A user profile
const user = t.struct({
id: t.uuid({ as_id: true }),
email: t.email(),
username: t.string({ min: 3, max: 64 }),
});
// A user profile with an optional `name
const user = t.struct({
id: t.uuid({ as_id: true }),
email: t.email(),
username: t.string({ min: 3, max: 64 }),
name: t.string({ min: 3, max: 64 }).optional(),
});
t.union()
and t.either()
t.union(variants);
t.either(variants);
The t.union
type represents a value that can be of any of the specified variants. The t.either
type represents a value that must be of one and only one of the specified variants.
The variants
parameter is an array of types.
t.func()
The t.func()
type represents an operation to be performed on the typegate with the specified configuration associated to it.
Usually, the functions are not defined explicitly, but rather created with the runtime instance.
Parameters
Parameter | Type | Description |
---|---|---|
input type | t.struct() | The type of the input data. |
output type | any type | The type of the output data. |
function | Function ~ | The abstraction to use to perform the operation/computation. |
Methods
Method | Purpose | Reference page |
---|---|---|
rate | Rate limiting | Rate limiting |
reduce | Parameter transformation | reduce |
apply | Parameter transformation | apply |