tgql
v0.0.1-alpha.7
Published
TypeScript-first GraphQL schema builder and companion client
Downloads
37
Readme
tGQL
Description
tGQL is a TypeScript-first GraphQL schema builder heavily inspired by tRPC and Zod (and drizzle).
Example
Look in the packages/server/tests
folder for more examples.
Define types
import { tgql } from 'tgql';
const Food = tgql.object('Food', {
id: tgql.id(),
name: tgql.string(),
calories: tgql.int().nullable(),
});
const User = tgql
.object('User', {
id: tgql.id(),
firstName: tgql.string(),
lastName: tgql.string(),
age: tgql.int().description('Age in years'),
weight: tgql.float().nullable(),
favoriteFoods: tgql.list(Food).nullable(),
})
.fieldResolvers((builder) => ({
fullName: builder.fieldResolver(tgql.string(), (user) => `${user.firstName} ${user.lastName}`),
}));
type UserType = tgql.Infer<typeof User>;
/**
* {
* weight?: number | undefined;
* favoriteFoods?: {
* calories?: number | undefined;
* id: string;
* name: string;
* }[] | undefined;
* id: string;
* firstName: string;
* lastName: string;
* age: number;
* fullName: string;
* }
*/
Define queries
const user = tgql
.query<{ currentUser: string }>()
.returns(User)
.args({ id: tgql.id() })
.resolver(async ({ args: { id }, context }) => {
return {
id,
firstName: 'first',
lastName: 'last',
age: 10,
favoriteNumbers: [1, 2],
};
});
const schema = tgql.registerResolvers({ user }).createSchema();