add config

This commit is contained in:
Benjamin Palko 2024-12-10 10:28:27 -05:00
parent 21634249d7
commit 05a95b72d0
8 changed files with 44 additions and 5 deletions

1
.env.development Normal file
View file

@ -0,0 +1 @@
VITE_APP_VERSION=1.0.0-alpha

1
.gitignore vendored
View file

@ -18,6 +18,7 @@ Thumbs.db
.env.*
!.env.example
!.env.test
!.env.development
# Vite
vite.config.js.timestamp-*

BIN
bun.lockb

Binary file not shown.

View file

@ -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"
}
}

24
src/lib/config/index.ts Normal file
View file

@ -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();

View file

@ -1,12 +1,14 @@
import { Context } from '$lib/yoga/context';
import SchemaBuilder from '@pothos/core';
export const builder = new SchemaBuilder({});
type ContextType = ReturnType<typeof Context>;
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
})
})
});

7
src/lib/yoga/context.ts Normal file
View file

@ -0,0 +1,7 @@
import { Config } from '$lib/config';
import type { YogaInitialContext } from 'graphql-yoga';
export const Context = (initialContext: YogaInitialContext) => ({
...initialContext,
config: Config
});

View file

@ -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<RequestEvent>({
context: Context,
schema: Schema,
graphqlEndpoint: '/api/graphql',
// Let Yoga use sveltekit's Response object