diff --git a/example/index.ts b/example/index.ts index 93466da..162a016 100644 --- a/example/index.ts +++ b/example/index.ts @@ -5,6 +5,10 @@ import { cors } from '@elysiajs/cors' import typeDefs from './schema' import resolvers from './resolvers' +interface MyContext { + hi: string +} + const app = new Elysia() .use( cors({ @@ -12,7 +16,7 @@ const app = new Elysia() }) ) .use( - apollo({ + apollo<'/graphql', MyContext>({ path: '/graphql', typeDefs, resolvers, diff --git a/src/index.ts b/src/index.ts index 23e092d..7b2b452 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,18 +14,18 @@ import { import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground' import { type StartStandaloneServerOptions } from '@apollo/server/standalone' -export interface ServerRegistration +export interface ServerRegistration extends Omit, 'context'> { path?: Path enablePlayground: boolean - context?: (context: Context) => Promise + context?: (context: TContext) => Promise } export type ElysiaApolloConfig< Path extends string = '/graphql', TContext extends BaseContext = BaseContext > = ApolloServerOptions & - Omit, 'enablePlayground'> & + Omit, 'enablePlayground'> & Partial> const getQueryString = (url: string) => url.slice(url.indexOf('?', 11) + 1) @@ -36,8 +36,8 @@ export class ElysiaApolloServer< public async createHandler({ path = '/graphql' as Path, enablePlayground, - context = async () => {} - }: ServerRegistration) { + context = async () => ({} as any) + }: ServerRegistration) { const landing = enablePlayground ? ApolloServerPluginLandingPageGraphQLPlayground({ endpoint: path @@ -127,13 +127,16 @@ export class ElysiaApolloServer< } } -export const apollo = async ({ +export const apollo = async < + Path extends string = '/graphql', + TContext extends BaseContext = BaseContext, +>({ path, enablePlayground = process.env.ENV !== 'production', context, ...config -}: ElysiaApolloConfig) => - new ElysiaApolloServer(config).createHandler({ +}: ElysiaApolloConfig) => + new ElysiaApolloServer(config).createHandler({ context, path, enablePlayground