Skip to content

Commit e2cc0cd

Browse files
committed
feat(use/http,use/http2): Request context with the response
Closes #66
1 parent a4aca6e commit e2cc0cd

File tree

7 files changed

+94
-6
lines changed

7 files changed

+94
-6
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[graphql-http](../README.md) / [use/http](../modules/use_http.md) / RequestContext
2+
3+
# Interface: RequestContext
4+
5+
[use/http](../modules/use_http.md).RequestContext
6+
7+
The context in the request for the handler.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [res](use_http.RequestContext.md#res)
14+
15+
## Properties
16+
17+
### res
18+
19+
**res**: `ServerResponse`<`IncomingMessage`\>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[graphql-http](../README.md) / [use/http2](../modules/use_http2.md) / RequestContext
2+
3+
# Interface: RequestContext
4+
5+
[use/http2](../modules/use_http2.md).RequestContext
6+
7+
The context in the request for the handler.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [res](use_http2.RequestContext.md#res)
14+
15+
## Properties
16+
17+
### res
18+
19+
**res**: `Http2ServerResponse`

docs/modules/use_http.md

+5-1
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_http.RequestContext.md)
10+
711
### Type Aliases
812

913
- [HandlerOptions](use_http.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_http.RequestContext.md), `Context`\>
2024

2125
Handler options when using the http adapter.
2226

docs/modules/use_http2.md

+5-1
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_http2.RequestContext.md)
10+
711
### Type Aliases
812

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

1721
### HandlerOptions
1822

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

2125
Handler options when using the http adapter.
2226

src/__tests__/use.ts

+24
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,34 @@ describe('http', () => {
2929
}
3030
});
3131
}
32+
33+
it('should allow manipulating the response from the request context', async () => {
34+
const [url, dispose] = startDisposableServer(
35+
http.createServer(
36+
createHttpHandler({
37+
schema,
38+
context(req) {
39+
req.context.res.setHeader('x-test', 'test-x');
40+
return undefined;
41+
},
42+
}),
43+
),
44+
);
45+
46+
const res = await fetch(url + '?query={hello}');
47+
48+
await expect(res.text()).resolves.toMatchInlineSnapshot(
49+
`"{"data":{"hello":"world"}}"`,
50+
);
51+
expect(res.headers.get('x-test')).toBe('test-x');
52+
53+
await dispose();
54+
});
3255
});
3356

3457
describe('http2', () => {
3558
it.todo('should pass all server audits');
59+
it.todo('should allow manipulating the response from the request context');
3660
});
3761

3862
describe('express', () => {

src/use/http.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import {
55
OperationContext,
66
} from '../handler';
77

8+
/**
9+
* The context in the request for the handler.
10+
*
11+
* @category Server/http
12+
*/
13+
export interface RequestContext {
14+
res: ServerResponse;
15+
}
16+
817
/**
918
* Handler options when using the http adapter.
1019
*
1120
* @category Server/http
1221
*/
1322
export type HandlerOptions<Context extends OperationContext = undefined> =
14-
RawHandlerOptions<IncomingMessage, undefined, Context>;
23+
RawHandlerOptions<IncomingMessage, 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
req.on('end', () => resolve(body));
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/http2.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import {
55
OperationContext,
66
} from '../handler';
77

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

1625
/**
1726
* Create a GraphQL over HTTP spec compliant request handler for
@@ -66,7 +75,7 @@ export function createHandler<Context extends OperationContext = undefined>(
6675
req.on('end', () => resolve(body));
6776
}),
6877
raw: req,
69-
context: undefined,
78+
context: { res },
7079
});
7180
res.writeHead(init.status, init.statusText, init.headers);
7281
if (body) {

0 commit comments

Comments
 (0)