done (#3)
This commit is contained in:
parent
3f5532a487
commit
c2ce272041
15 changed files with 226 additions and 177 deletions
|
|
@ -1,22 +1,22 @@
|
|||
import { logger } from "@lib/logger";
|
||||
import { z } from "zod";
|
||||
import { logger } from '@lib/logger';
|
||||
import { z } from 'zod';
|
||||
|
||||
export interface Configuration {
|
||||
app_version: string;
|
||||
app_version: string;
|
||||
}
|
||||
|
||||
export const LoadConfig = (): Configuration => {
|
||||
const { success, data, error } = z
|
||||
.object({
|
||||
APP_VERSION: z.string().default("development"),
|
||||
})
|
||||
.safeParse(process.env);
|
||||
const { success, data, error } = z
|
||||
.object({
|
||||
APP_VERSION: z.string().default('development'),
|
||||
})
|
||||
.safeParse(process.env);
|
||||
|
||||
if (!success) {
|
||||
logger.error(error.message);
|
||||
}
|
||||
if (!success) {
|
||||
logger.error(error.message);
|
||||
}
|
||||
|
||||
return {
|
||||
app_version: data!.APP_VERSION,
|
||||
};
|
||||
return {
|
||||
app_version: data!.APP_VERSION,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
20
src/index.ts
20
src/index.ts
|
|
@ -1,15 +1,15 @@
|
|||
import { logger } from "@lib/logger";
|
||||
import { yoga } from "./yoga";
|
||||
import { logger } from '@lib/logger';
|
||||
import { yoga } from './yoga';
|
||||
|
||||
const server = Bun.serve({
|
||||
fetch: yoga.fetch,
|
||||
error: (error) => {
|
||||
logger.error(error.message);
|
||||
return new Response("", {
|
||||
status: 500,
|
||||
statusText: "You fucked the goose",
|
||||
});
|
||||
},
|
||||
fetch: yoga.fetch,
|
||||
error: (error) => {
|
||||
logger.error(error.message);
|
||||
return new Response('', {
|
||||
status: 500,
|
||||
statusText: 'You fucked the goose',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`Server is running on: ${server.url}${yoga.graphqlEndpoint}`);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
export const prisma = new PrismaClient();
|
||||
|
|
|
|||
|
|
@ -1,29 +1,29 @@
|
|||
import type { Configuration } from "@app/config";
|
||||
import { prisma } from "@app/prisma";
|
||||
import SchemaBuilder from "@pothos/core";
|
||||
import type { Configuration } from '@app/config';
|
||||
import { prisma } from '@app/prisma';
|
||||
import SchemaBuilder from '@pothos/core';
|
||||
import PrismaPlugin, {
|
||||
type PrismaTypesFromClient,
|
||||
} from "@pothos/plugin-prisma";
|
||||
import type { YogaInitialContext } from "graphql-yoga";
|
||||
type PrismaTypesFromClient,
|
||||
} from '@pothos/plugin-prisma';
|
||||
import type { YogaInitialContext } from 'graphql-yoga';
|
||||
|
||||
type Context = YogaInitialContext & {
|
||||
config: Configuration;
|
||||
config: Configuration;
|
||||
};
|
||||
|
||||
export const builder = new SchemaBuilder<{
|
||||
Context: Context;
|
||||
PrismaTypes: PrismaTypesFromClient<typeof prisma>;
|
||||
Context: Context;
|
||||
PrismaTypes: PrismaTypesFromClient<typeof prisma>;
|
||||
}>({
|
||||
plugins: [PrismaPlugin],
|
||||
prisma: {
|
||||
client: prisma,
|
||||
// defaults to false, uses /// comments from prisma schema as descriptions
|
||||
// for object types, relations and exposed fields.
|
||||
// descriptions can be omitted by setting description to false
|
||||
exposeDescriptions: false,
|
||||
// use where clause from prismaRelatedConnection for totalCount (defaults to true)
|
||||
filterConnectionTotalCount: true,
|
||||
// warn when not using a query parameter correctly
|
||||
onUnusedQuery: process.env.NODE_ENV === "production" ? null : "warn",
|
||||
},
|
||||
plugins: [PrismaPlugin],
|
||||
prisma: {
|
||||
client: prisma,
|
||||
// defaults to false, uses /// comments from prisma schema as descriptions
|
||||
// for object types, relations and exposed fields.
|
||||
// descriptions can be omitted by setting description to false
|
||||
exposeDescriptions: false,
|
||||
// use where clause from prismaRelatedConnection for totalCount (defaults to true)
|
||||
filterConnectionTotalCount: true,
|
||||
// warn when not using a query parameter correctly
|
||||
onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn',
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { LoadConfig } from "@app/config";
|
||||
import type { YogaInitialContext } from "graphql-yoga";
|
||||
import { LoadConfig } from '@app/config';
|
||||
import type { YogaInitialContext } from 'graphql-yoga';
|
||||
|
||||
export const context = (initialContext: YogaInitialContext) => {
|
||||
const config = LoadConfig();
|
||||
return {
|
||||
...initialContext,
|
||||
config,
|
||||
};
|
||||
const config = LoadConfig();
|
||||
return {
|
||||
...initialContext,
|
||||
config,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { yogaLogger } from "@lib/logger";
|
||||
import { createYoga } from "graphql-yoga";
|
||||
import { context } from "./context";
|
||||
import { schema } from "./schema";
|
||||
import { yogaLogger } from '@lib/logger';
|
||||
import { createYoga } from 'graphql-yoga';
|
||||
import { context } from './context';
|
||||
import { schema } from './schema';
|
||||
|
||||
export const yoga = createYoga({
|
||||
schema,
|
||||
context: context,
|
||||
logging: yogaLogger,
|
||||
schema,
|
||||
context: context,
|
||||
logging: yogaLogger,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,43 +1,43 @@
|
|||
import { prisma } from "@app/prisma";
|
||||
import { builder } from "./builder";
|
||||
import { prisma } from '@app/prisma';
|
||||
import { builder } from './builder';
|
||||
|
||||
const User = builder.prismaObject("User", {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID("id"),
|
||||
email: t.exposeString("email"),
|
||||
name: t.exposeString("name"),
|
||||
posts: t.relation("posts"),
|
||||
}),
|
||||
const User = builder.prismaObject('User', {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID('id'),
|
||||
email: t.exposeString('email'),
|
||||
name: t.exposeString('name'),
|
||||
posts: t.relation('posts'),
|
||||
}),
|
||||
});
|
||||
|
||||
const Post = builder.prismaObject("Post", {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID("id"),
|
||||
title: t.exposeString("title"),
|
||||
content: t.exposeString("content"),
|
||||
published: t.exposeBoolean("published"),
|
||||
author: t.relation("author"),
|
||||
}),
|
||||
const Post = builder.prismaObject('Post', {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID('id'),
|
||||
title: t.exposeString('title'),
|
||||
content: t.exposeString('content'),
|
||||
published: t.exposeBoolean('published'),
|
||||
author: t.relation('author'),
|
||||
}),
|
||||
});
|
||||
|
||||
builder.queryType({
|
||||
fields: (t) => ({
|
||||
version: t.string({
|
||||
resolve: (parent, args, context) => context.config.app_version,
|
||||
}),
|
||||
users: t.prismaField({
|
||||
type: [User],
|
||||
resolve: async () => {
|
||||
return await prisma.user.findMany();
|
||||
},
|
||||
}),
|
||||
posts: t.prismaField({
|
||||
type: [Post],
|
||||
resolve: async () => {
|
||||
return await prisma.post.findMany();
|
||||
},
|
||||
}),
|
||||
}),
|
||||
fields: (t) => ({
|
||||
version: t.string({
|
||||
resolve: (parent, args, context) => context.config.app_version,
|
||||
}),
|
||||
users: t.prismaField({
|
||||
type: [User],
|
||||
resolve: async () => {
|
||||
return await prisma.user.findMany();
|
||||
},
|
||||
}),
|
||||
posts: t.prismaField({
|
||||
type: [Post],
|
||||
resolve: async () => {
|
||||
return await prisma.post.findMany();
|
||||
},
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export const schema = builder.toSchema();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue