The full name of GQLoom
is GraphQL Loom.
The silk is the basic material of the loom, and it reflects both GraphQL types and TypeScript types.
At development time, we use the Schema of an existing schema library as the thread, and eventually GQLoom
will weave the silk into the GraphQL Schema.
We can create a simple scalar silk using the silk
function:
We can construct GraphQL objects directly using graphql.js:
In the above code: we define an ICat
interface and a silk named Cat
using the silk
function.
The silk
function accepts ICat
as a generic parameter and also accepts a GraphQLObjectType
instance to elaborate the structure of Cat
in GraphQL.
Cat
will be presented in GraphQL as:
You may have noticed that using graphql.js
to create silk requires the declaration of both the ICat
interface and the GraphQLObjectType
, which means that we have created two definitions for Cat
.
Duplicate definitions cost the code simplicity and increased maintenance costs.
The good thing is that we have schema libraries like Valibot, Zod that create Schemas that will carry TypeScript types and remain typed at runtime.
GQLoom
can use these Schemas directly as silks without duplicating definitions.
In the code above, we use Valibot to create some simple schemas as silk. You can learn more about how to create more complex types with Valibot in the Valibot Integration chapter.
The GQLoom
core library follows the Standard Schema Specification. Thanks to Valibot
also adhering to this specification, we can use Valibot schemas as silk without any additional wrapper functions.
In the future, Zodwill also support this specification.
In the code above, we have created some simple Schema as silk using Zod, you can learn how to create more complex types using Zod integration section to learn how to create more complex types using Zod.