GQLoom

Silk

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.

Simple scalar silk

We can create a simple scalar silk using the silk function:

import {  } from "@gqloom/core"
import { , ,  } from "graphql"
 
const  = ()
const  = ()
 
const  = (new ())
const  = .()

Object silk

We can construct GraphQL objects directly using graphql.js:

import {  } from "@gqloom/core"
import {
  ,
  ,
  ,
  ,
} from "graphql"
 
interface ICat {
  : string
  : number
}
 
const  = <ICat>(
  new ({
    : "Cat",
    : {
      : { : new () },
      : { : new () },
    },
  })
)

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:

GraphQL Schema
type Cat {
  name: String!
  age: Int!
}

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.

Creating silk with schema libraries

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.

GQLoom has currently integrated Schemas from the following libraries:

Use Valibot to create silk

import * as  from "valibot"
 
const  = .()
 
const  = .()
 
const  = .({
  : .("Cat"),
  : .(),
  : .(),
})

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.

Use Zod to create silk

import {  } from "zod"
 
const  = .()
 
const  = .()
 
const  = .({
  : .("Cat"),
  : .(),
  : .(),
})

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.

The core library of GQLoom follows the Standard Schema specification. Thanks to Valibot and Zod also following this specification, we don't need to use additional wrapper functions to use the Schema from Valibot and Zod as Silk.

On this page