creating graphql mutation
//index.ts
import 'dotenv/config'
import { createYoga } from 'graphql-yoga';
import { createServer } from 'node:http';
import { buildSchema } from 'drizzle-graphql';
import { db } from './config/db';
const { schema } = buildSchema(db);
const yoga = createYoga({ schema });
const server = createServer(yoga);
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql');
});
//index.ts
import 'dotenv/config'
import { createYoga } from 'graphql-yoga';
import { createServer } from 'node:http';
import { buildSchema } from 'drizzle-graphql';
import { db } from './config/db';
const { schema } = buildSchema(db);
const yoga = createYoga({ schema });
const server = createServer(yoga);
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql');
});
//schema.ts
import { pgTable,uuid,varchar} from 'drizzle-orm/pg-core'
export const posts = pgTable("posts",{
id: uuid("id").primaryKey().defaultRandom(),
title: varchar("title",{length:255}).notNull(),
description:varchar("description",{length:255}).notNull(),
content:varchar("content",{length:255}).notNull(),
})
//schema.ts
import { pgTable,uuid,varchar} from 'drizzle-orm/pg-core'
export const posts = pgTable("posts",{
id: uuid("id").primaryKey().defaultRandom(),
title: varchar("title",{length:255}).notNull(),
description:varchar("description",{length:255}).notNull(),
content:varchar("content",{length:255}).notNull(),
})
//db.ts
const queryClient = postgres(EnvConfig.DATABASE_URL)
export const db = drizzle(queryClient,{schema:dbSchema})
//db.ts
const queryClient = postgres(EnvConfig.DATABASE_URL)
export const db = drizzle(queryClient,{schema:dbSchema})
//resolvers.ts
import { posts } from "../drizzle/schema";
import { db } from "../config/db";
import { YogaInitialContext } from "graphql-yoga";
export const resolvers = {
Query: {
posts: async () => {
await db.select().from(posts).unionAll;
},
},
Mutation: {
createPost: async () => {
await db.insert(posts).values({id,title,content,description}).returning();
},
},
};
//resolvers.ts
import { posts } from "../drizzle/schema";
import { db } from "../config/db";
import { YogaInitialContext } from "graphql-yoga";
export const resolvers = {
Query: {
posts: async () => {
await db.select().from(posts).unionAll;
},
},
Mutation: {
createPost: async () => {
await db.insert(posts).values({id,title,content,description}).returning();
},
},
};
0 Replies