Skip to content

Commit

Permalink
feat: pass context to inParams hook
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Nov 7, 2024
1 parent 076d25c commit 1246771
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/graphql-yoga/src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type Plugin<
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
*/
onParams?: OnParamsHook;
onParams?: OnParamsHook<TServerContext>;
/**
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
Expand Down Expand Up @@ -105,14 +105,17 @@ export interface OnRequestParseDoneEventPayload {
setRequestParserResult: (params: GraphQLParams | GraphQLParams[]) => void;
}

export type OnParamsHook = (payload: OnParamsEventPayload) => PromiseOrValue<void>;
export type OnParamsHook<TServerContext> = (
payload: OnParamsEventPayload<TServerContext>,
) => PromiseOrValue<void>;

export interface OnParamsEventPayload {
export interface OnParamsEventPayload<TServerContext> {
params: GraphQLParams;
request: Request;
setParams: (params: GraphQLParams) => void;
setResult: (result: ExecutionResult | AsyncIterable<ExecutionResult>) => void;
fetchAPI: FetchAPI;
context: TServerContext;
}

export type OnResultProcess<TServerContext> = (
Expand Down
12 changes: 6 additions & 6 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class YogaServer<
Plugin<TUserContext & TServerContext & YogaInitialContext, TServerContext, TUserContext>
>;
private onRequestParseHooks: OnRequestParseHook<TServerContext>[];
private onParamsHooks: OnParamsHook[];
private onParamsHooks: OnParamsHook<TServerContext>[];
private onExecutionResultHooks: OnExecutionResultHook<TServerContext>[];
private onResultProcessHooks: OnResultProcess<TServerContext>[];
private maskedErrorsOpts: YogaMaskedErrorOpts | null;
Expand Down Expand Up @@ -471,7 +471,9 @@ export class YogaServer<
serverContext: TServerContext,
) {
let result: ExecutionResult | AsyncIterable<ExecutionResult> | undefined;
let context = serverContext as TServerContext & YogaInitialContext;
let context: TServerContext & YogaInitialContext =
batched === undefined ? serverContext : Object.create(serverContext);

try {
for (const onParamsHook of this.onParamsHooks) {
await onParamsHook({
Expand All @@ -484,6 +486,7 @@ export class YogaServer<
result = newResult;
},
fetchAPI: this.fetchAPI,
context,
});
}

Expand All @@ -498,10 +501,7 @@ export class YogaServer<
params,
};

context = Object.assign(
batched ? Object.create(serverContext) : serverContext,
additionalContext,
);
context = Object.assign(context, additionalContext);

const enveloped = this.getEnveloped(context);

Expand Down

0 comments on commit 1246771

Please sign in to comment.