Skip to content

Commit

Permalink
solidify context shape
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 6, 2023
1 parent 7225f96 commit f34c080
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
4 changes: 1 addition & 3 deletions packages/core/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ builder.addScalarType('Date', DateResolver, {})

export type GetContext<
ServerOptions extends Record<string, any> = {},
UserOptions extends Record<string, any> = {} & {
headers?: Record<string, string> | undefined | null
},
UserOptions extends Record<string, any> = {},
> = NonNullable<YogaServerOptions<ServerOptions, UserOptions>['context']>

type Builder = Omit<
Expand Down
47 changes: 40 additions & 7 deletions packages/core/src/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ interface StellateOptions {
serviceName: string
}

type InitialContext = {
headers: Headers
params: GraphQLParams
request: YogaInitialContext['request']
}

export function createAPIRouteHandler<
AdditionalContext extends Record<string, unknown> = any,
>(options?: {
context?: GetContext<YogaInitialContext, AdditionalContext>
context?: GetContext<InitialContext, AdditionalContext>
stellate?: StellateOptions
}) {
return (request: Request, context: NextPageContext) => {
Expand All @@ -44,7 +50,22 @@ export function createAPIRouteHandler<
schema: completedSchema,
// We allow batching by default
batching: true,
context: options?.context,
context: (ct) => {
const baseContext: InitialContext = {
request: ct.request,
headers: ct.request.headers,
params: ct.params,
}
if (options?.context) {
const userCtx = options.context(baseContext)
return {
...baseContext,
...userCtx,
}
}

return baseContext
},
// While using Next.js file convention for routing, we need to configure Yoga to use the correct endpoint
graphqlEndpoint: '/api/fuse',

Expand All @@ -70,10 +91,7 @@ export function createAPIRouteHandler<
export function createPagesRouteHandler<
AdditionalContext extends Record<string, unknown> = any,
>(options?: {
context?: GetContext<
{ req: NextApiRequest; res: NextApiResponse },
AdditionalContext
>
context?: GetContext<InitialContext, AdditionalContext>
stellate?: StellateOptions
}) {
const schema = builder.toSchema({})
Expand All @@ -95,7 +113,22 @@ export function createPagesRouteHandler<
: false,
maskedErrors: process.env.NODE_ENV === 'production',
batching: true,
context: options?.context,
context: (ct) => {
const baseContext: InitialContext = {
request: ct.request,
headers: ct.request.headers,
params: ct.params,
}
if (options?.context) {
const userCtx = options.context(baseContext)
return {
...baseContext,
...userCtx,
}
}

return baseContext
},
graphqlEndpoint: '/api/fuse',
plugins: [
useDeferStream(),
Expand Down

1 comment on commit f34c080

@vercel
Copy link

@vercel vercel bot commented on f34c080 Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

spacex-fuse – ./examples/spacex

spacex-fuse-stellate.vercel.app
spacex-fuse-git-main-stellate.vercel.app
spacex-fuse.vercel.app

Please sign in to comment.