Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] add TContext for ApolloServer generic type #8

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { cors } from '@elysiajs/cors'
import typeDefs from './schema'
import resolvers from './resolvers'

interface MyContext {
hi: string
}

const app = new Elysia()
.use(
cors({
origin: 'http://localhost'
})
)
.use(
apollo({
apollo<'/graphql', MyContext>({
path: '/graphql',
typeDefs,
resolvers,
Expand Down
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path extends string = '/graphql'>
export interface ServerRegistration<Path extends string = '/graphql', TContext extends BaseContext = BaseContext>
extends Omit<StartStandaloneServerOptions<any>, 'context'> {
path?: Path
enablePlayground: boolean
context?: (context: Context) => Promise<any>
context?: (context: TContext) => Promise<TContext>
}

export type ElysiaApolloConfig<
Path extends string = '/graphql',
TContext extends BaseContext = BaseContext
> = ApolloServerOptions<TContext> &
Omit<ServerRegistration<Path>, 'enablePlayground'> &
Omit<ServerRegistration<Path, TContext>, 'enablePlayground'> &
Partial<Pick<ServerRegistration, 'enablePlayground'>>

const getQueryString = (url: string) => url.slice(url.indexOf('?', 11) + 1)
Expand All @@ -36,8 +36,8 @@ export class ElysiaApolloServer<
public async createHandler<Path extends string = '/graphql'>({
path = '/graphql' as Path,
enablePlayground,
context = async () => {}
}: ServerRegistration<Path>) {
context = async () => ({} as any)
}: ServerRegistration<Path, Context>) {
const landing = enablePlayground
? ApolloServerPluginLandingPageGraphQLPlayground({
endpoint: path
Expand Down Expand Up @@ -127,13 +127,16 @@ export class ElysiaApolloServer<
}
}

export const apollo = async <Path extends string = '/graphql'>({
export const apollo = async <
Path extends string = '/graphql',
TContext extends BaseContext = BaseContext,
>({
path,
enablePlayground = process.env.ENV !== 'production',
context,
...config
}: ElysiaApolloConfig<Path>) =>
new ElysiaApolloServer(config).createHandler<Path>({
}: ElysiaApolloConfig<Path, TContext>) =>
new ElysiaApolloServer<TContext>(config).createHandler<Path>({
context,
path,
enablePlayground
Expand Down