Skip to content

Commit 86c28d1

Browse files
committed
fix(handler): context option can return a response
1 parent d0cd67b commit 86c28d1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/__tests__/handler.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
it.todo('should behave as advertised');
1+
import fetch from 'node-fetch';
2+
import { startTServer } from './utils/tserver';
3+
4+
it('should use the response returned from context', async () => {
5+
const server = startTServer({
6+
context: () => {
7+
return [null, { status: 418 }];
8+
},
9+
});
10+
11+
const url = new URL(server.url);
12+
url.searchParams.set('query', '{ __typename }');
13+
const res = await fetch(url.toString());
14+
expect(res.status).toBe(418);
15+
});

src/handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,10 @@ export function createHandler<RawRequest = unknown>(
453453
}
454454

455455
if (!('contextValue' in args)) {
456-
args.contextValue =
456+
const maybeResOrContext =
457457
typeof context === 'function' ? await context(req, args) : context;
458+
if (isResponse(maybeResOrContext)) return maybeResOrContext;
459+
args.contextValue = maybeResOrContext;
458460
}
459461

460462
const validationErrs = validate(args.schema, args.document);

0 commit comments

Comments
 (0)