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 UnionTypes.
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, which is used to parse incoming data to python typesserialize, which is used to serialize python data to GraphQL accepted types
For Undine's additional built-in scalars, these functions are single dispatch generic functions. This means that you can register different implementations for the functions which are used depending on the type of the input value. Think of them like a dynamic switch statement. This allows you to replace or extend the behavior of a scalar depending on your use case.
For example, you might want to use the whenever library instead
or in addition to python's built-in datetime. To do this, you can register a new
implementation for the parse function of the DateTime scalar.
Custom scalars๐
You can also define your 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, you 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 your scalar for Fields,
Inputs, Filters, and Orders.
More on this in the "Hacking Undine" section.