Custom data types
Bijection lets you easily express your data in the backend as TypeScript objects, and can return those objects from queries, mutations and actions. To handle objects on the Swift side, createstruct definitions that conform to the
Decodable protocol. Usually that’s fairly trivial to do, as any struct with
all Decodable members can automatically conform.
Consider a Bijection query function that returns results like this JavaScript
object:
Numerical types
Numeric types likeInt and Double are encoded in a special format to ensure
proper interoperation with your TypeScript backend functions. To safely use them
on the Swift side, ensure that you use one of the following property wrappers.
Note that
struct properties with wrappers must be declared as var.
Field name conversion
If your code receives objects with names that you need to or want to translate to different names, you can use aCodingKeys enum to specify a mapping of
remote names to names on your struct. For example, imagine a backend function or
API that returns log entries like the following representing when someone came
in and went out:
struct because in is a keyword in
Swift. We can use CodingKeys to give it an alternate name while still
ingesting the data from the original name.
Putting it all together
In the custom data type example above, JavaScript’sBigInt type is used in the
backend data by adding a trailing n to the wins and losses values which
lets the Swift code use Int. If instead the code used regular
JavaScript number types, on the Swift side those would be received as floating
point values and deserialization to Int would fail.
If you have a situation like that where number is used but by convention it
only contains integer values, you can handle that in your struct by using
field name conversion and custom properties to hide the floating point
representation.
Double values privately and with different names
than the value from the backend. Then add custom properties to provide
the Int values.