diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..3283edf --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +VITE_APP_VERSION=1.0.0-alpha \ No newline at end of file diff --git a/.gitignore b/.gitignore index a1bdc7b..68204fe 100644 --- a/.gitignore +++ b/.gitignore @@ -18,9 +18,10 @@ Thumbs.db .env.* !.env.example !.env.test +!.env.development # Vite vite.config.js.timestamp-* vite.config.ts.timestamp-* -*storybook.log +*storybook.log \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index ff51ac9..50ee1dc 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index be83b8c..292b9d9 100644 --- a/package.json +++ b/package.json @@ -50,10 +50,12 @@ "dependencies": { "@pothos/core": "^4.3.0", "@tailwindcss/typography": "^0.5.15", + "@types/bun": "^1.1.14", "graphql": "^16.9.0", "graphql-yoga": "^5.10.4", "pino": "^9.5.0", "pino-pretty": "^13.0.0", - "storybook-dark-mode": "^4.0.2" + "storybook-dark-mode": "^4.0.2", + "zod": "^3.24.0" } } diff --git a/src/lib/config/index.ts b/src/lib/config/index.ts new file mode 100644 index 0000000..b87d270 --- /dev/null +++ b/src/lib/config/index.ts @@ -0,0 +1,24 @@ +import { logger } from '$lib/logger'; +import { z } from 'zod'; + +export interface Configuration { + app_version: string; +} + +export const LoadConfig = (): Configuration => { + const { success, data, error } = z + .object({ + VITE_APP_VERSION: z.string().default('development') + }) + .safeParse(import.meta.env); + + if (!success) { + logger.error(error.message); + } + + return { + app_version: data!.VITE_APP_VERSION + }; +}; + +export const Config = LoadConfig(); \ No newline at end of file diff --git a/src/lib/pothos/index.ts b/src/lib/pothos/index.ts index f1fa65a..934f50a 100644 --- a/src/lib/pothos/index.ts +++ b/src/lib/pothos/index.ts @@ -1,12 +1,14 @@ +import { Context } from '$lib/yoga/context'; import SchemaBuilder from '@pothos/core'; -export const builder = new SchemaBuilder({}); +type ContextType = ReturnType; + +export const builder = new SchemaBuilder<{ Context: ContextType }>({}); builder.queryType({ fields: (t) => ({ version: t.string({ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - resolve: (parent, args, context) => '1.0.0-alpha' + resolve: (parent, args, context) => context.config.app_version }) }) }); diff --git a/src/lib/yoga/context.ts b/src/lib/yoga/context.ts new file mode 100644 index 0000000..d9a6a68 --- /dev/null +++ b/src/lib/yoga/context.ts @@ -0,0 +1,7 @@ +import { Config } from '$lib/config'; +import type { YogaInitialContext } from 'graphql-yoga'; + +export const Context = (initialContext: YogaInitialContext) => ({ + ...initialContext, + config: Config +}); \ No newline at end of file diff --git a/src/lib/yoga/index.ts b/src/lib/yoga/index.ts index 274b44f..b008b24 100644 --- a/src/lib/yoga/index.ts +++ b/src/lib/yoga/index.ts @@ -2,8 +2,10 @@ 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