Skip to main content

Types

Overview

Types are used to describe the data to be processed. They constrains the range of value that can be accepted as input data or expected as result on each computation running in a runtime.

Scalar types

TypeGraphQL typeDescription
t.integer()IntRepresents signed 32-bit integers.
t.number()FloatRepresents signed double-precision values as specified by IEEE 754.
t.float()FloatAlias to t.number().
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.

Example: The min constraint on the type t.integer()

from typegraph import t

# represents integers greater than or equal to `12`
t.integer().min(12)

Names and type references

Injection

Types

t.boolean()

from typegraph import t

t.boolean()

The t.boolean() type represents boolean values, true or false.

t.integer()

The t.integer() type represents 32-bit integers.

from typegraph import t

t.integer()

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

from typegraph import t

# non-negative integer
t.integer().min(0)

# an integer in the rage [18, 120)
adult_age = t.integer().min(18).x_max(120)

# an even integer
t.integer().multiple_of(2)

t.number()

from typegraph import t

t.number()

The t.number() type represents numbers, stored in double precision floating-point format (IEEE 754).

Constraints

The t.number() type has the same constraints as t.integer(). See integer constraints.

Aliases

The following types are aliases to the t.number() type:

  • t.float()

t.string()

from typegraph import t

t.string()

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

from typegraph import t

# a non-empty string of maximum 64 characters
t.string().min(1).max(64)

# a 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()

from typegraph import t

t.file()

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

from typegraph import t

# 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()

from typegraph import t

t.optional(t.string())
t.string().optional() # equivalent syntactic sugar

Default value

If the type is used as an input type, the default value can be specified using the .default() method.

from typegraph import t

t.string().optional().default("default value")

t.list()

from typegraph import t

t.list(t.string())

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

from typegraph import t

# A list of strings
t.list(t.string())

# A list of unique strings
t.list(t.string()).unique_items()

# A list of strings with at least 3 items
# and at most 10 items
t.list(t.string()).min(3).max(10)

t.struct()

from typegraph import t

user = t.struct({
"id": t.uuid().as_id,
"email": t.email(),
"username": t.string().min(3).max(64),
})

The t.struct() type represents structured data, consisting of fields which map to typed values.

All the fields are required unless the corresponding type is wrapped in t.optional(). In that case, the field is allowed to be missing from the data or be null.

Constraints

ConstraintTypeDescription
minIntegerMinimum number of fields.
maxIntegerMaximum number of fields.

Examples

from typegraph import t

# A user profile
user = t.struct({
"id": t.uuid().as_id,
"email": t.email(),
"username": t.string().min(3).max(64),
})

# A user profile with an optional `name` field
user = t.struct({
"id": t.uuid().as_id,
"email": t.email(),
"username": t.string().min(3).max(64),
"name": t.optional(t.string().min(3).max(64)),
})

t.union() and t.either()

from typegraph import t

t.union([t.string(), t.integer()])
t.either([t.string(), t.integer()])

The t.union type represents a value that can be of any of the specified types. The t.either type represents a value that must be of one and only one of the specified 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.
materializerMaterializerThe materializer to use to perform the operation/computation.

Methods

MethodPurposeReference page
rateRate limitingRate limiting
reduceParameter transformationreduce
applyParameter transformationapply

Examples

from typegraph import t
from typegraph.runtimes.deno import FunMat
from typegraph.providers.prisma.runtimes import PrismaRuntime

with TypeGraph("math") as g:
add = t.func(
t.struct({
"a": t.integer(),
"b": t.integer(),
}),
t.integer(),
FunMat("(({a, b}) => a + b")
)

g.expose(add=add)

db = PrismaRuntime("main-db", "POSTGRES")

user = t.struct({
"id": t.uuid().as_id,
"email": t.email(),
"username": t.string().min(3).max(64),
})

g.expose(createUser=db.create(user))