Skip to content

Commit

Permalink
give access to the server context in onResultProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin authored and ardatan committed Aug 15, 2024
1 parent 41510b4 commit a6e3f25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-mails-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-yoga': minor
---

Expose server context in `onResultProcessHook`. In particular, this gives access to the `waitUntil`
method to cleanly handle hanging promises.
7 changes: 5 additions & 2 deletions packages/graphql-yoga/src/process-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { ExecutionArgs } from '@graphql-tools/executor';
import { OnResultProcess, ResultProcessor, ResultProcessorInput } from './plugins/types.js';
import { FetchAPI, GraphQLParams } from './types.js';

export async function processResult({
export async function processResult<TServerContext>({
request,
result,
fetchAPI,
onResultProcessHooks,
serverContext,
}: {
request: Request;
result: ResultProcessorInput;
fetchAPI: FetchAPI;
/**
* Response Hooks
*/
onResultProcessHooks: OnResultProcess[];
onResultProcessHooks: OnResultProcess<TServerContext>[];
serverContext: TServerContext;
}) {
let resultProcessor: ResultProcessor | undefined;

Expand All @@ -36,6 +38,7 @@ export async function processResult({
resultProcessor = newResultProcessor;
acceptedMediaType = newAcceptedMimeType;
},
serverContext,
});
}

Expand Down
28 changes: 16 additions & 12 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ServerAdapterRequestHandler,
useCORS,
useErrorHandling,
type ServerAdapterInitialContext,
} from '@whatwg-node/server';
import { handleError, isAbortError } from './error.js';
import { isGETRequest, parseGETRequest } from './plugins/request-parser/get.js';
Expand Down Expand Up @@ -350,20 +351,22 @@ export class YogaServer<
// Middlewares after the GraphQL execution
useResultProcessors(),
useErrorHandling<TServerContext & YogaInitialContext & ServerAdapterInitialContext>(
(error, request) => {
const errors = handleError(error, this.maskedErrorsOpts, this.logger);
(error, request, serverContext) => {
const errors = handleError(error, this.maskedErrorsOpts, this.logger);

const result = {
errors,
};
const result = {
errors,
};

return processResult({
request,
result,
fetchAPI: this.fetchAPI,
onResultProcessHooks: this.onResultProcessHooks,
});
}),
return processResult({
request,
result,
fetchAPI: this.fetchAPI,
onResultProcessHooks: this.onResultProcessHooks,
serverContext,
});
},
),

...(options?.plugins ?? []),
// To make sure those are called at the end
Expand Down Expand Up @@ -621,6 +624,7 @@ export class YogaServer<
result,
fetchAPI: this.fetchAPI,
onResultProcessHooks: this.onResultProcessHooks,
serverContext,
});
};
}
Expand Down

0 comments on commit a6e3f25

Please sign in to comment.