Scalars๐
In this section, we'll cover how GraphQL scalars work in Undine. Scalars are GraphQL types that represent concrete data types like strings, numbers, and booleans.
Built-in Scalars๐
In addition to GraphQL's built-in scalars of
Int
,
Float
,
String
,
Boolean
,
and ID
,
Undine provides its own scalars that are useful for representing common data types
in Python.
Any
๐
Represent any value accepted by GraphQL. Used for e.g. for union types.
Base16
๐
Represents a base16-encoded string as defined in RFC 4648.
Base32
๐
Represents a base32-encoded string as defined in RFC 4648.
Base64
๐
Represents a base64-encoded string as defined in RFC 4648.
Date
๐
Represents a date value as specified by ISO 8601.
Maps to the Python datetime.date
type.
See RFC 3339.
DateTime
๐
Represents a date and time value as specified by ISO 8601.
Maps to the Python datetime.datetime
type.
See RFC 3339.
Decimal
๐
Represents a number as a string for correctly rounded floating point arithmetic.
Maps to the Python decimal.Decimal
type.
Duration
๐
Represents a duration of time in seconds.
Maps to the Python datetime.timedelta
type.
Email
๐
Represents a valid email address. See RFC 5322.
File
๐
Represents any kind of file. See the file upload section.
IP
๐
Represents a valid IPv4 or IPv6 address. See RFC 8200. and RFC 791.
IPv4
๐
Represents a valid IPv4 address. See RFC 791.
IPv6
๐
Represents a valid IPv6 address. See RFC 8200.
Image
๐
Represents an image file. See the file upload section.
JSON
๐
Represents a JSON serializable object.
Maps to the Python dict
type.
See RFC 8259.
Null
๐
Represents represents an always null value.
Maps to the Python None
value.
Time
๐
Represents a time value as specified by ISO 8601.
Maps to the Python datetime.time
type.
See RFC 3339.
URL
๐
Represents a valid URL. See RFC 3986.
UUID
๐
Represents a universally unique identifier string.
Maps to Python's uuid.UUID
type.
See RFC 9562.
Modifying existing scalars๐
All scalars have two functions that define its operation: parse
and serialize
These are used to
parse incoming data to python types and serialize python data to GraphQL accepted types respectively.
In Undine's additional built-in scalars, these functions are single dispatch generic functions. This means that we can register different implementations for the functions which are called depending on the type of the input value โ think of it like a dynamic switch statement. This allows us to replace or extend the behavior of a scalar depending on our use case.
For example, we might want to use the whenever library instead
or in addition to python's built-in datetime
. To do this, we can register a new
implementation for the parse
function of the DateTime
scalar.
Custom scalars๐
We can also define our own scalars to represent types that cannot be represented
by any of Undine's built-in scalars. Let's create a new scalar named Vector3
that represents a 3D vector using a tuple of three integers.
If Vector3
corresponds to a Django model field, we could also let Undine know
about it by registering it for its many built-in converters. This way a model field
can be converted automatically to our scalar for QueryType
Fields
and MutationType
Inputs
.
More on this in the "Hacking Undine" section.