Skip to content

Commit a7336ff

Browse files
authored
Examples (#58)
1 parent 24ce5bd commit a7336ff

File tree

16 files changed

+1846
-135
lines changed

16 files changed

+1846
-135
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { gWithContext } from "@graphql-ts/schema";
2+
import { GraphQLSchema } from "graphql";
3+
import { createYoga } from "graphql-yoga";
4+
import { createServer } from "http";
5+
6+
type Context = {};
7+
8+
const g = gWithContext<Context>();
9+
type g<T> = gWithContext.infer<T>;
10+
11+
const Query = g.object()({
12+
name: "Query",
13+
fields: {
14+
hello: g.field({ type: g.String, resolve: () => "world" }),
15+
},
16+
});
17+
18+
const schema = new GraphQLSchema({ query: Query });
19+
20+
const yoga = createYoga({ schema });
21+
const server = createServer(yoga);
22+
server.listen(4000, () => {
23+
console.info("Server is running on http://localhost:4000/graphql");
24+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@graphql-ts/example-basic-graphql-http",
3+
"private": true,
4+
"dependencies": {
5+
"@graphql-ts/schema": "^1.0.1",
6+
"graphql": "^16.3.0",
7+
"graphql-yoga": "^5.13.1"
8+
},
9+
"devDependencies": {
10+
"@types/node": "^22.13.10",
11+
"tsx": "^4.19.3",
12+
"typescript": "^5.8.2"
13+
},
14+
"scripts": {
15+
"dev": "tsx index.ts",
16+
"types": "tsc"
17+
},
18+
"repository": "https://github.com/Thinkmill/graphql-ts/tree/main/examples/basic-graphql-yoga"
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "NodeNext",
5+
"noEmit": true,
6+
"isolatedModules": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
11+
"forceConsistentCasingInFileNames": true
12+
},
13+
"include": ["**/*"],
14+
"exclude": ["**/node_modules**/"]
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.db
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from "drizzle-kit";
2+
export default defineConfig({
3+
dialect: "sqlite",
4+
schema: "./src/db-schema.ts",
5+
dbCredentials: {
6+
url: "./dev.db",
7+
},
8+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@graphql-ts/example-drizzle-graphql-yoga",
3+
"private": true,
4+
"type": "module",
5+
"dependencies": {
6+
"@graphql-ts/schema": "^1.0.1",
7+
"better-sqlite3": "^11.8.1",
8+
"drizzle-orm": "^0.40.0",
9+
"graphql": "^16.3.0",
10+
"graphql-yoga": "^5.13.1"
11+
},
12+
"devDependencies": {
13+
"@types/better-sqlite3": "^7.6.12",
14+
"@types/node": "^22.13.10",
15+
"drizzle-kit": "^0.30.5",
16+
"tsx": "^4.19.3",
17+
"typescript": "^5.8.2"
18+
},
19+
"scripts": {
20+
"dev": "drizzle-kit push && tsx src/server.ts",
21+
"types": "tsc"
22+
},
23+
"repository": "https://github.com/Thinkmill/graphql-ts/tree/main/examples/drizzle-graphql-yoga"
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
2+
3+
export const todo = sqliteTable("todo", {
4+
id: text()
5+
.primaryKey()
6+
.$defaultFn(() => crypto.randomUUID()),
7+
title: text().notNull(),
8+
done: integer({ mode: "boolean" }).notNull().default(false),
9+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { drizzle } from "drizzle-orm/better-sqlite3";
2+
import * as dbSchema from "./db-schema.js";
3+
4+
export type DB = ReturnType<typeof createDb>;
5+
6+
export function createDb(url: string) {
7+
return drizzle(url, { schema: dbSchema });
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { gWithContext } from "@graphql-ts/schema";
2+
import type { DB } from "./db.js";
3+
4+
export type Context = {
5+
db: DB;
6+
};
7+
8+
export const g = gWithContext<Context>();
9+
export type g<T> = gWithContext.infer<T>;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { GraphQLSchema } from "graphql";
2+
import { g } from "./g.js";
3+
import * as dbSchema from "./db-schema.js";
4+
import { eq } from "drizzle-orm";
5+
6+
const Todo = g.object<typeof dbSchema.todo.$inferSelect>()({
7+
name: "Todo",
8+
fields: {
9+
id: g.field({ type: g.nonNull(g.ID) }),
10+
title: g.field({ type: g.nonNull(g.String) }),
11+
done: g.field({ type: g.nonNull(g.Boolean) }),
12+
},
13+
});
14+
15+
const Query = g.object()({
16+
name: "Query",
17+
fields: {
18+
todos: g.field({
19+
type: g.list(g.nonNull(Todo)),
20+
resolve(_root, _args, ctx) {
21+
return ctx.db.query.todo.findMany();
22+
},
23+
}),
24+
todo: g.field({
25+
type: Todo,
26+
args: {
27+
id: g.arg({ type: g.nonNull(g.ID) }),
28+
},
29+
resolve(_root, args, ctx) {
30+
return ctx.db.query.todo.findFirst({
31+
where: eq(dbSchema.todo.id, args.id),
32+
});
33+
},
34+
}),
35+
},
36+
});
37+
38+
const Mutation = g.object()({
39+
name: "Mutation",
40+
fields: {
41+
createTodo: g.field({
42+
type: Todo,
43+
args: {
44+
title: g.arg({ type: g.nonNull(g.String) }),
45+
},
46+
async resolve(_root, args, ctx) {
47+
const inserted = await ctx.db
48+
.insert(dbSchema.todo)
49+
.values({ title: args.title })
50+
.returning();
51+
return inserted[0];
52+
},
53+
}),
54+
updateTodo: g.field({
55+
type: Todo,
56+
args: {
57+
id: g.arg({ type: g.nonNull(g.ID) }),
58+
title: g.arg({ type: g.String }),
59+
done: g.arg({ type: g.Boolean }),
60+
},
61+
async resolve(_root, args, ctx) {
62+
const updated = await ctx.db
63+
.update(dbSchema.todo)
64+
.set({
65+
title: args.title ?? undefined,
66+
done: args.done ?? undefined,
67+
})
68+
.where(eq(dbSchema.todo.id, args.id))
69+
.returning();
70+
return updated[0];
71+
},
72+
}),
73+
deleteTodo: g.field({
74+
type: Todo,
75+
args: {
76+
id: g.arg({ type: g.nonNull(g.ID) }),
77+
},
78+
async resolve(_root, args, ctx) {
79+
const deleted = await ctx.db
80+
.delete(dbSchema.todo)
81+
.where(eq(dbSchema.todo.id, args.id))
82+
.returning();
83+
return deleted[0];
84+
},
85+
}),
86+
},
87+
});
88+
89+
export const schema = new GraphQLSchema({ query: Query, mutation: Mutation });

0 commit comments

Comments
 (0)