Valibot
Valibot is the open source schema library for TypeScript with bundle size, type safety and developer experience in mind.
@gqloom/valibot
provides integration of GQLoom with Valibot to weave Valibot Schema into GraphQL Schema.
Installation
Defining simple scalars
In GQLoom, you can directly use Valibot schemas as silk:
Weave
To ensure that GQLoom
correctly weaves Valibot schemas into the GraphQL schema, we need to add the ValibotWeaver
from @gqloom/valibot
when using the weave
function.
Defining objects
We can use Valibot to define objects and use them as [silk] (... /silk) to use:
Names and more metadata
Defining names for objects
In GQLoom
we have multiple ways to define names for objects.
Using __typename
literal
In the code above, we used the __typename
literal to define the name for the object. We also set the __typename
literal to nullish
, which means that the __typename
field is optional, and if it exists, it must be “Cat”.
In the code above, we are still using the __typename
literal to define the name for the object, but this time we are setting the __typename
literal to “Cat”, which means that the __typename
field is required and must be “Cat”. The required __typename
will be very useful when using GraphQL interface
and union
.
Using collectNames
In the above code, we are using the collectNames
function to define names for objects. The collectNames
function accepts an object whose key is the name of the object and whose value is the object itself.
In the code above, we use the collectNames
function to define the names for the objects and deconstruct the returned objects into Cat
and export them.
Using asObjectType
In the code above, we used the asObjectType
function to create a metadata and pass it into the Valibot
pipeline to define a name for the object. The asObjectType
function takes the complete GraphQL object type definition and returns a metadata.
Add more data
With the asObjectType
function, we can add more data to the object, such as description
, deprecationReason
, extensions
and so on.
In the above code, we have added a description
metadata to the Cat
object which will be presented in the GraphQL Schema:
We can also use the asField
function to add metadata to a field, such as description
, type
, and so on.
In the above code, we added type
and description
metadata to the age
field and ended up with the following GraphQL Schema:
Declaring Interfaces
We can use the asObjectType
function to define interfaces, for example:
In the above code, we have created an interface Fruit
using the asObjectType
function and declared the Orange
object as an implementation of the Fruit
interface using the interfaces
option.
Omitting Fields
We can also omit fields by setting type
to null
using the asField
function, for example:
The following GraphQL Schema will be obtained:
Defining Union Types
When using Valibot
, we can define union types using variant
or union
.
Using variant
We recommend using variant
to define union types:
In the above code, we have created a union type using the variant
function. In the case of Animal
, it distinguishes the specific type by the __typename
field.
Using union
We can also use union
to define union types:
In the above code, we have created a union type using the union
function. In the case of Animal
, it uses the resolveType
function to differentiate between specific types.
Here, if an animal likes fish, then it is a cat, otherwise it is a dog.
Defining Enumeration Types
We can define enumeration types using v.picklist
or v.enum_
.
Using picklist
In general, we prefer to use v.picklist
to define enumerated types:
Using enum_
We can also use v.enum_
to define enumeration types:
Custom Type Mappings
To accommodate more Valibot types, we can extend GQLoom to add more type mappings.
First we use ValibotWeaver.config
to define the type mapping configuration. Here we import the GraphQLDateTime
, GraphQLJSON
and GraphQLJSONObject
scalars from [graphql-scalars] and map them to the matching GraphQL scalars when encountering the date
, any
and record
types.
Configurations are passed into the weave
function when weaving the GraphQL Schema:
Default Type Mappings
The following table lists the default mappings between Valibot types and GraphQL types in GQLoom:
Valibot types | GraphQL types |
---|---|
v.array() | GraphQLList |
v.bigint() | GraphQLInt |
v.date() | GraphQLString |
v.enum_() | GraphQLEnumType |
v.picklist() | GraphQLEnumType |
v.literal(false) | GraphQLBoolean |
v.literal(0) | GraphQLFloat |
v.literal("") | GraphQLString |
v.looseObject() | GraphQLObjectType |
v.object() | GraphQLObjectType |
v.objectWithRest() | GraphQLObjectType |
v.strict_object() | GraphQLObjectType |
v.nonNullable() | GraphQLNonNull |
v.nonNullish() | GraphQLNonNull |
v.nonOptional() | GraphQLNonNull |
v.number() | GraphQLFloat |
v.pipe(v.number(), v.integer()) | GraphQLInt |
v.string() | GraphQLString |
v.pipe(v.string(), v.cuid2()) | GraphQLID |
v.pipe(v.string(), v.ulid()) | GraphQLID |
v.pipe(v.string(), v.uuid()) | GraphQLID |
v.union() | GraphQLUnionType |
v.variant() | GraphQLUnionType |