diff --git a/src/lib/pothos/builder.ts b/src/lib/pothos/builder.ts new file mode 100644 index 0000000..1428ea6 --- /dev/null +++ b/src/lib/pothos/builder.ts @@ -0,0 +1,24 @@ +import { prisma } from '$lib/prisma'; +import type { Context } from '$lib/yoga'; +import SchemaBuilder from '@pothos/core'; +import PrismaPlugin, { type PrismaTypesFromClient } from '@pothos/plugin-prisma'; + +type ContextType = ReturnType; + +export const builder = new SchemaBuilder<{ + Context: ContextType; + PrismaTypes: PrismaTypesFromClient; +}>({ + 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' + } +}); \ No newline at end of file diff --git a/src/lib/pothos/index.ts b/src/lib/pothos/index.ts index c8ee3d6..d6bd62d 100644 --- a/src/lib/pothos/index.ts +++ b/src/lib/pothos/index.ts @@ -1,65 +1,3 @@ -import { prisma } from '$lib/prisma'; -import { Context } from '$lib/yoga/context'; -import SchemaBuilder from '@pothos/core'; -import PrismaPlugin, { type PrismaTypesFromClient } from '@pothos/plugin-prisma'; - -type ContextType = ReturnType; - -export const builder = new SchemaBuilder<{ - Context: ContextType; - PrismaTypes: PrismaTypesFromClient; -}>({ - 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' - } -}); - -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') - }) -}); - -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(); - } - }) - }) -}); +import { builder } from './builder'; export const Schema = builder.toSchema(); \ No newline at end of file diff --git a/src/lib/pothos/schema.ts b/src/lib/pothos/schema.ts new file mode 100644 index 0000000..4abddae --- /dev/null +++ b/src/lib/pothos/schema.ts @@ -0,0 +1,41 @@ +import { prisma } from '$lib/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 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(); + } + }) + }) +}); \ No newline at end of file diff --git a/src/lib/yoga/index.ts b/src/lib/yoga/index.ts index b008b24..b489ffb 100644 --- a/src/lib/yoga/index.ts +++ b/src/lib/yoga/index.ts @@ -1,14 +1,2 @@ -import { yogaLogger } from '$lib/logger'; -import { Schema } from '$lib/pothos'; -import type { RequestEvent } from '@sveltejs/kit'; -import { createYoga } from 'graphql-yoga'; -import { Context } from './context'; - -export const Yoga = createYoga({ - context: Context, - schema: Schema, - graphqlEndpoint: '/api/graphql', - // Let Yoga use sveltekit's Response object - fetchAPI: { Response }, - logging: yogaLogger -}); \ No newline at end of file +export * from './context'; +export * from './server'; \ No newline at end of file diff --git a/src/lib/yoga/server.ts b/src/lib/yoga/server.ts new file mode 100644 index 0000000..b008b24 --- /dev/null +++ b/src/lib/yoga/server.ts @@ -0,0 +1,14 @@ +import { yogaLogger } from '$lib/logger'; +import { Schema } from '$lib/pothos'; +import type { RequestEvent } from '@sveltejs/kit'; +import { createYoga } from 'graphql-yoga'; +import { Context } from './context'; + +export const Yoga = createYoga({ + context: Context, + schema: Schema, + graphqlEndpoint: '/api/graphql', + // Let Yoga use sveltekit's Response object + fetchAPI: { Response }, + logging: yogaLogger +}); \ No newline at end of file