Skip to content

Schema

TIP

We suggest to use Schema Loader CLI to load the schema.

This page is written for those who want to manually write the schema type.

Reference

Here is a empty schema type, you can get start with it:

ts
import type {
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
} from '@gqfn/core/schema'

export type  = <{
  // Define your schema here
}>

type < extends string> = <, >
type < extends string> = <, >

Scalar

To define a scalar type, you can use the ScalarType helper type:

TIP

Most custom scalar types uses string as input and output type in client-side.

ts
export type  = <{
  : {
    : <'Date', string>
  }
}>
graphql
"""
A date-time string at UTC, such as 2007-12-03T10:15:30Z,
compliant with the `date-time` format outlined in section 5.6
of the RFC 3339 profile of the ISO 8601 standard for
representation of dates and times using the Gregorian calendar.
"""
scalar Date

Enum

ts
export type  =
  | 'ADMIN'
  | 'USER'
  | 'GUEST'

export type  = <{
  : {
    : <'RoleEnum', >
  }
}>
graphql
enum RoleEnum {
  ADMIN
  USER
  GUEST
}

Input Object

ts
type  = <'UserInput', {
  : <'String!'>
  : <'RoleEnum!'>
  : <'String'>
}>

export type  = <{
  : {
    : 
  }
}>
graphql
input UserInput {
  name: String!
  role: RoleEnum!
  email: String
}

Interface, ObjectType and Union

ts
type  = <'Query', {
  : <'users', <'User!'>>
  : <'todo', <'Todo!'>, {
    : <'ID!'>
  }>
  : <'allIdItems', <'[ItemWithId!]!'>>
  : <'allData', <'[Data!]!'>>
}>

type  = <'Mutation', {
  : <'addTodo', <'Todo!'>, {
    : <'string!'>
  }>
}>

type  = <'Subscription', {
  : <'watchTodo', <'Todo'>, {
    : <'ID!'>
  }>
}>

type  = <'ItemWithId', {
  : <'id', <'Int!'>>
}, {
  : 
  : 
}>

type  = <'Data', {
  : 
  : 
}>

type  = <'User', {
  : <'id', <'ID!'>>
  : <'name', <'String!'>>
  : <'email', <'String'>>
  : <'todo', <'[Todo!]!'>>
}>

type  = <'Todo', {
  : <'id', <'ID!'>>
  : <'title', <'String'>>
  : <'content', <'String!'>>
  : <'isDone', <'Boolean!'>>
  : <'User!', <'User!'>>
}>

export type  = <{
  : {
    : 
  }
  : {
    : 
  }
  : {
    : 
    : 
    : 

    : 
    : 
  }
}>
graphql
interface ItemWithId {
  id: Int!
}

union Data = User | Todo

type Query {
  users: [User!]!
  todo(id: ID!): Todo!
  allIdItems: [ItemWithId!]!
  allData: [Data!]!
}

type Mutation {
  addTodo(input: String!): Todo!
}

type Subscription {
  watchTodo(id: ID!): Todo
}

type User implements ItemWithId {
  id: ID!
  name: String!
  email: String
  todo: [Todo!]!
}

type Todo implements ItemWithId {
  id: ID!
  title: String
  content: String!
  isDone: Boolean!
  owner: User!
}

Type Definition

ts
export interface ScalarType<
  Name extends string,
  Input = unknown,
  Output = Input,
> { /* internal */ }

export interface EnumType<
  Name extends string,
  Values extends string,
> { /* internal */ }

export interface InputObject<
  Name extends string,
  Fields extends Record<
    string,
    ArgOf<any, string>
  >,
> { /* internal */ }

export interface InterfaceObject<
  Name extends string,
  Fields extends Record<
    string,
    Field<string, any, any>
  >,
  Implements extends Record<string, TypeObject<string, any>>,
> { /* internal */ }

export interface TypeObject<
  Name extends string,
  Fields extends Record<
    string,
    Field<string, any, any>
  >,
  Types extends Record<string, TypeObject<string, any>> = EmptyRecord,
> { /* internal */ }

export interface Union<
  Name extends string,
  Types extends Record<string, TypeObject<string, any>>,
> { /* internal */ }

export interface Field<
  Name extends string,
  Return extends ResOf<any, string>,
  Argument extends Record<string, ArgOf<any, string>> = EmptyRecord,
> { /* internal */ }

export type ArgOf<
  Schema extends DefineSchema<any>,
  TKey extends string,
> = internal // It converts GraphQL input type to TypeScript type

export type ResOf<
  Schema extends DefineSchema<any>,
  TKey extends string,
> = internal // It converts GraphQL output type to TypeScript type