Schema
TIP
We suggest to use @gqfn/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 {
DefineSchema,
EnumType,
Field,
Input,
InputObjectType,
InterfaceType,
ObjectType,
ScalarType,
UnionType
} from '@gqfn/core/schema'
export type Schema = DefineSchema<{
// Define your schema here
}>
Scalar
To define a scalar type, you can use the ScalarType
helper type:
ts
type Scalar_Date = ScalarType<'Date', string, 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 RoleEnum
= | 'ADMIN'
| 'USER'
| 'GUEST'
type Enum_RoleEnum = EnumType<'RoleEnum', RoleEnum>
graphql
enum RoleEnum {
ADMIN
USER
GUEST
}
Input Object
ts
type UserInput = InputObjectType<'UserInput', {
name: Input<'String!', Scalar_String>
role: Input<'RoleEnum!', Enum_RoleEnum>
email: Input<'String', Scalar_String>
}>
graphql
input UserInput {
name: String!
role: RoleEnum!
email: String
}
Interface, ObjectType and Union
ts
type Query = ObjectType<'Query', {
users: Field<'[User!]!', User>
todo: Field<'Todo!', Todo, {
id: Input<'ID!', Scalar_ID>
}>
allIdItems: Field<'[ItemWithId!]!', ItemWithId>
allData: Field<'[Data!]!', Data>
}>
type Mutation = ObjectType<'Mutation', {
addTodo: Field<'Todo!', Todo, {
content: Input<'String!', Scalar_String>
}>
}>
type Subscription = ObjectType<'Subscription', {
watchTodo: Field<'Todo', Todo, {
id: Input<'ID!', Scalar_ID>
}>
}>
type ItemWithId = InterfaceType<'ItemWithId', {
id: Field<'Int!', Scalar_Int>
}, {
User: User
Todo: Todo
}>
type Data = UnionType<'Data', {
User: User
Todo: Todo
}>
type User = ObjectType<'User', {
id: Field<'ID!', Scalar_ID>
name: Field<'String!', Scalar_String>
email: Field<'String', Scalar_String>
todo: Field<'[Todo!]!', Todo>
}>
type Todo = ObjectType<'Todo', {
id: Field<'ID!', Scalar_ID>
title: Field<'String', Scalar_String>
content: Field<'String!', Scalar_String>
isDone: Field<'Boolean!', Scalar_Boolean>
owner: Field<'User!', User>
}>
export type Schema = DefineSchema<{
ItemWithId: ItemWithId
Data: Data
Query: Query
Mutation: Mutation
Subscription: Subscription
User: User
Todo: Todo
}>
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!
}