Add sequelize index file

This commit is contained in:
Jorge Vargas 2024-09-03 19:11:19 -06:00
parent 6f00481c25
commit d20b00532d
12 changed files with 77 additions and 50 deletions

View file

@ -4,24 +4,21 @@ import { SchemaLink } from "@apollo/client/link/schema"
import { makeExecutableSchema } from "@graphql-tools/schema";
import { getSession } from 'auth-astro/server';
import type { Session } from '@auth/core/types';
import { typeDefs } from "./__generated__/typeDefs.generated";
import resolvers from '@/graphql/resolvers'
import type { MySqlDialect } from '@sequelize/mysql';
import { Sequelize, type ModelStatic, type Options } from '@sequelize/core'
import db from '@/sequelize';
const schema = makeExecutableSchema({ typeDefs, resolvers })
export type ResolverContext = { request?: Request, session?: Session }
const envOptions: Options<MySqlDialect> = JSON.parse(import.meta.env.SEQUELIZE) || {}
export async function getApolloClient(request?: Request) {
const session = request ? await getSession(request) : null
const db = new Sequelize(envOptions)
envOptions.models = Object.values<ModelStatic>(await import.meta.glob('@/sequelize/models/**/*.ts', { eager: true, import: 'default' }))
return new ApolloClient({
ssrMode: true,
link: new SchemaLink({ schema, context: { request, session } }),
link: new SchemaLink({ schema, context: { request, session, db } }),
cache: new InMemoryCache()
})
}