Skip to content

Commit 665175e

Browse files
committed
feat(use/express,use/fastify,use/koa): Request context with the response
Closes #66
1 parent f519a97 commit 665175e

File tree

10 files changed

+189
-12
lines changed

10 files changed

+189
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[graphql-http](../README.md) / [use/express](../modules/use_express.md) / RequestContext
2+
3+
# Interface: RequestContext
4+
5+
[use/express](../modules/use_express.md).RequestContext
6+
7+
The context in the request for the handler.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [res](use_express.RequestContext.md#res)
14+
15+
## Properties
16+
17+
### res
18+
19+
**res**: `Response`<`any`, `Record`<`string`, `any`\>\>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[graphql-http](../README.md) / [use/fastify](../modules/use_fastify.md) / RequestContext
2+
3+
# Interface: RequestContext
4+
5+
[use/fastify](../modules/use_fastify.md).RequestContext
6+
7+
The context in the request for the handler.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [reply](use_fastify.RequestContext.md#reply)
14+
15+
## Properties
16+
17+
### reply
18+
19+
**reply**: `FastifyReply`<`RawServerDefault`, `IncomingMessage`, `ServerResponse`<`IncomingMessage`\>, `RouteGenericInterface`, `unknown`, `FastifySchema`, `FastifyTypeProviderDefault`, `unknown`\>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[graphql-http](../README.md) / [use/koa](../modules/use_koa.md) / RequestContext
2+
3+
# Interface: RequestContext
4+
5+
[use/koa](../modules/use_koa.md).RequestContext
6+
7+
The context in the request for the handler.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [res](use_koa.RequestContext.md#res)
14+
15+
## Properties
16+
17+
### res
18+
19+
**res**: `Response`

docs/modules/use_express.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## Table of contents
66

7+
### Interfaces
8+
9+
- [RequestContext](../interfaces/use_express.RequestContext.md)
10+
711
### Type Aliases
812

913
- [HandlerOptions](use_express.md#handleroptions)
@@ -16,7 +20,7 @@
1620

1721
### HandlerOptions
1822

19-
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`, `undefined`, `Context`\>
23+
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`, [`RequestContext`](../interfaces/use_express.RequestContext.md), `Context`\>
2024

2125
Handler options when using the express adapter.
2226

docs/modules/use_fastify.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## Table of contents
66

7+
### Interfaces
8+
9+
- [RequestContext](../interfaces/use_fastify.RequestContext.md)
10+
711
### Type Aliases
812

913
- [HandlerOptions](use_fastify.md#handleroptions)
@@ -16,7 +20,7 @@
1620

1721
### HandlerOptions
1822

19-
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`FastifyRequest`, `undefined`, `Context`\>
23+
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`FastifyRequest`, [`RequestContext`](../interfaces/use_fastify.RequestContext.md), `Context`\>
2024

2125
Handler options when using the fastify adapter.
2226

docs/modules/use_koa.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## Table of contents
66

7+
### Interfaces
8+
9+
- [RequestContext](../interfaces/use_koa.RequestContext.md)
10+
711
### Type Aliases
812

913
- [HandlerOptions](use_koa.md#handleroptions)
@@ -16,7 +20,7 @@
1620

1721
### HandlerOptions
1822

19-
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>
23+
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, [`RequestContext`](../interfaces/use_koa.RequestContext.md), `Context`\>
2024

2125
Handler options when using the koa adapter.
2226

src/__tests__/use.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ describe('express', () => {
7474
}
7575
});
7676
}
77+
78+
it('should allow manipulating the response from the request context', async () => {
79+
const app = express();
80+
app.all(
81+
'/',
82+
createExpressHandler({
83+
schema,
84+
context(req) {
85+
req.context.res.setHeader('x-test', 'test-x');
86+
return undefined;
87+
},
88+
}),
89+
);
90+
91+
const [url, dispose] = startDisposableServer(app.listen(0));
92+
93+
const res = await fetch(url + '?query={hello}');
94+
95+
await expect(res.text()).resolves.toMatchInlineSnapshot(
96+
`"{"data":{"hello":"world"}}"`,
97+
);
98+
expect(res.headers.get('x-test')).toBe('test-x');
99+
100+
await dispose();
101+
});
77102
});
78103

79104
describe('fastify', () => {
@@ -104,6 +129,35 @@ describe('fastify', () => {
104129
}
105130
});
106131
}
132+
133+
it('should allow manipulating the response from the request context', async () => {
134+
const app = fastify();
135+
136+
app.all(
137+
'/',
138+
createFastifyHandler({
139+
schema,
140+
context(req) {
141+
req.context.reply.header('x-test', 'test-x');
142+
return undefined;
143+
},
144+
}),
145+
);
146+
147+
// call ready since we're not calling listen
148+
app.ready();
149+
150+
const [url, dispose] = startDisposableServer(app.server);
151+
152+
const res = await fetch(url + '?query={hello}');
153+
154+
await expect(res.text()).resolves.toMatchInlineSnapshot(
155+
`"{"data":{"hello":"world"}}"`,
156+
);
157+
expect(res.headers.get('x-test')).toBe('test-x');
158+
159+
await dispose();
160+
});
107161
});
108162

109163
describe('fetch', () => {
@@ -137,4 +191,31 @@ describe('koa', () => {
137191
}
138192
});
139193
}
194+
195+
it('should allow manipulating the response from the request context', async () => {
196+
const app = new Koa();
197+
app.use(
198+
mount(
199+
'/',
200+
createKoaHandler({
201+
schema,
202+
context(req) {
203+
req.context.res.set('x-test', 'test-x');
204+
return undefined;
205+
},
206+
}),
207+
),
208+
);
209+
210+
const [url, dispose] = startDisposableServer(app.listen(0));
211+
212+
const res = await fetch(url + '?query={hello}');
213+
214+
await expect(res.text()).resolves.toMatchInlineSnapshot(
215+
`"{"data":{"hello":"world"}}"`,
216+
);
217+
expect(res.headers.get('x-test')).toBe('test-x');
218+
219+
await dispose();
220+
});
140221
});

src/use/express.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import type { Request, Handler } from 'express';
1+
import type { Request, Response, Handler } from 'express';
22
import {
33
createHandler as createRawHandler,
44
HandlerOptions as RawHandlerOptions,
55
OperationContext,
66
} from '../handler';
77

8+
/**
9+
* The context in the request for the handler.
10+
*
11+
* @category Server/express
12+
*/
13+
export interface RequestContext {
14+
res: Response;
15+
}
16+
817
/**
918
* Handler options when using the express adapter.
1019
*
1120
* @category Server/express
1221
*/
1322
export type HandlerOptions<Context extends OperationContext = undefined> =
14-
RawHandlerOptions<Request, undefined, Context>;
23+
RawHandlerOptions<Request, RequestContext, Context>;
1524

1625
/**
1726
* Create a GraphQL over HTTP spec compliant request handler for
@@ -54,7 +63,7 @@ export function createHandler<Context extends OperationContext = undefined>(
5463
});
5564
},
5665
raw: req,
57-
context: undefined,
66+
context: { res },
5867
});
5968
res.writeHead(init.status, init.statusText, init.headers).end(body);
6069
} catch (err) {

src/use/fastify.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import type { FastifyRequest, RouteHandler } from 'fastify';
1+
import type { FastifyRequest, FastifyReply, RouteHandler } from 'fastify';
22
import {
33
createHandler as createRawHandler,
44
HandlerOptions as RawHandlerOptions,
55
OperationContext,
66
} from '../handler';
77

8+
/**
9+
* The context in the request for the handler.
10+
*
11+
* @category Server/fastify
12+
*/
13+
export interface RequestContext {
14+
reply: FastifyReply;
15+
}
16+
817
/**
918
* Handler options when using the fastify adapter.
1019
*
1120
* @category Server/fastify
1221
*/
1322
export type HandlerOptions<Context extends OperationContext = undefined> =
14-
RawHandlerOptions<FastifyRequest, undefined, Context>;
23+
RawHandlerOptions<FastifyRequest, RequestContext, Context>;
1524

1625
/**
1726
* Create a GraphQL over HTTP spec compliant request handler for
@@ -45,7 +54,7 @@ export function createHandler<Context extends OperationContext = undefined>(
4554
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4655
body: req.body as any,
4756
raw: req,
48-
context: undefined,
57+
context: { reply },
4958
});
5059
reply
5160
.status(init.status)

src/use/koa.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
import type { Middleware } from 'koa';
1+
import type { Middleware, Response } from 'koa';
22
import type { IncomingMessage } from 'http';
33
import {
44
createHandler as createRawHandler,
55
HandlerOptions as RawHandlerOptions,
66
OperationContext,
77
} from '../handler';
88

9+
/**
10+
* The context in the request for the handler.
11+
*
12+
* @category Server/koa
13+
*/
14+
export interface RequestContext {
15+
res: Response;
16+
}
17+
918
/**
1019
* Handler options when using the koa adapter.
1120
*
1221
* @category Server/koa
1322
*/
1423
export type HandlerOptions<Context extends OperationContext = undefined> =
15-
RawHandlerOptions<IncomingMessage, undefined, Context>;
24+
RawHandlerOptions<IncomingMessage, RequestContext, Context>;
1625

1726
/**
1827
* Create a GraphQL over HTTP spec compliant request handler for
@@ -56,7 +65,7 @@ export function createHandler<Context extends OperationContext = undefined>(
5665
});
5766
},
5867
raw: ctx.req,
59-
context: undefined,
68+
context: { res: ctx.response },
6069
});
6170
ctx.body = body;
6271
ctx.response.status = init.status;

0 commit comments

Comments
 (0)