Skip to main content

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

TypeGraphQL typeDescription
t.integer()IntRepresents signed 32-bit integers.
t.float()FloatRepresents signed double-precision values as specified by IEEE 754.
t.boolean()BooleanRepresents true or false.
t.string()StringRepresents 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

TypeGraphQL typeDescription
t.optional()nullableRepresents a value that may be null.
t.list()listRepresents a list of values.
t.struct()interfaceRepresents a structured data value, consisting of fields which map to typed values.
t.union()unionRepresents a value which can be one of a set of specified types.
t.either()unionRepresents a value which can match one and only one of a set of specified types.
t.funcRepresents 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 });

Names and type references

Each type has a unique name. If none is setImmediate, 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

ConstraintDescription
minThe minimum value of the integer.
maxThe maximum value of the integer.
x_minThe minimum value of the integer, exclusive.
x_maxThe maximum value of the integer, exclusive.
multiple_ofThe 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

ConstraintTypeDescription
minIntegerMinimum length of the string.
maxIntegerMaximum length of the string.
patternStringRegular expression pattern that the string must match.
formatStringJSON 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

AliasEquivalent declaration
t.uuid()t.string({ format: "uuid" })
t.email()t.string({ format: "email" })
t.uri()t.string({ format: "uri" })
t.jsont.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

ConstraintTypeDescription
minIntegerMinimum size of the file in bytes.
maxIntegerMaximum size of the file in bytes.
allowArray of stringsList of allowed content-types

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

ConstraintTypeDescription
minIntegerMinimum number of items.
maxIntegerMaximum number of items.
unique_itemsBooleanWhether 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

ConstraintTypeDescription
minIntegerMinimum number of fields.
maxIntegerMaximum 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 materializer.

Usually, the functions are not defined explicitly, but rather created with the runtime instance.

=======

Parameters

ParameterTypeDescription
input typet.struct()The type of the input data.
output typeany typeThe type of the output data.
materializerMaterializer~The materializer to use to perform the operation/computation.

Methods

MethodPurposeReference page
rateRate limitingRate limiting
reduceParameter transformationreduce
applyParameter transformationapply