Skip to content

Commit

Permalink
feat(use/http,use/http2): Request context with the response
Browse files Browse the repository at this point in the history
Closes #66
  • Loading branch information
enisdenjo committed Mar 28, 2023
1 parent a4aca6e commit e2cc0cd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
19 changes: 19 additions & 0 deletions docs/interfaces/use_http.RequestContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[graphql-http](../README.md) / [use/http](../modules/use_http.md) / RequestContext

# Interface: RequestContext

[use/http](../modules/use_http.md).RequestContext

The context in the request for the handler.

## Table of contents

### Properties

- [res](use_http.RequestContext.md#res)

## Properties

### res

**res**: `ServerResponse`<`IncomingMessage`\>
19 changes: 19 additions & 0 deletions docs/interfaces/use_http2.RequestContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[graphql-http](../README.md) / [use/http2](../modules/use_http2.md) / RequestContext

# Interface: RequestContext

[use/http2](../modules/use_http2.md).RequestContext

The context in the request for the handler.

## Table of contents

### Properties

- [res](use_http2.RequestContext.md#res)

## Properties

### res

**res**: `Http2ServerResponse`
6 changes: 5 additions & 1 deletion docs/modules/use_http.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## Table of contents

### Interfaces

- [RequestContext](../interfaces/use_http.RequestContext.md)

### Type Aliases

- [HandlerOptions](use_http.md#handleroptions)
Expand All @@ -16,7 +20,7 @@

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, [`RequestContext`](../interfaces/use_http.RequestContext.md), `Context`\>

Handler options when using the http adapter.

Expand Down
6 changes: 5 additions & 1 deletion docs/modules/use_http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## Table of contents

### Interfaces

- [RequestContext](../interfaces/use_http2.RequestContext.md)

### Type Aliases

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

### HandlerOptions

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

Handler options when using the http adapter.

Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,34 @@ describe('http', () => {
}
});
}

it('should allow manipulating the response from the request context', async () => {
const [url, dispose] = startDisposableServer(
http.createServer(
createHttpHandler({
schema,
context(req) {
req.context.res.setHeader('x-test', 'test-x');
return undefined;
},
}),
),
);

const res = await fetch(url + '?query={hello}');

await expect(res.text()).resolves.toMatchInlineSnapshot(
`"{"data":{"hello":"world"}}"`,
);
expect(res.headers.get('x-test')).toBe('test-x');

await dispose();
});
});

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

describe('express', () => {
Expand Down
13 changes: 11 additions & 2 deletions src/use/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import {
OperationContext,
} from '../handler';

/**
* The context in the request for the handler.
*
* @category Server/http
*/
export interface RequestContext {
res: ServerResponse;
}

/**
* Handler options when using the http adapter.
*
* @category Server/http
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<IncomingMessage, undefined, Context>;
RawHandlerOptions<IncomingMessage, RequestContext, Context>;

/**
* Create a GraphQL over HTTP spec compliant request handler for
Expand Down Expand Up @@ -54,7 +63,7 @@ export function createHandler<Context extends OperationContext = undefined>(
req.on('end', () => resolve(body));
}),
raw: req,
context: undefined,
context: { res },
});
res.writeHead(init.status, init.statusText, init.headers).end(body);
} catch (err) {
Expand Down
13 changes: 11 additions & 2 deletions src/use/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import {
OperationContext,
} from '../handler';

/**
* The context in the request for the handler.
*
* @category Server/http2
*/
export interface RequestContext {
res: Http2ServerResponse;
}

/**
* Handler options when using the http adapter.
*
* @category Server/http2
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<Http2ServerRequest, undefined, Context>;
RawHandlerOptions<Http2ServerRequest, RequestContext, Context>;

/**
* Create a GraphQL over HTTP spec compliant request handler for
Expand Down Expand Up @@ -66,7 +75,7 @@ export function createHandler<Context extends OperationContext = undefined>(
req.on('end', () => resolve(body));
}),
raw: req,
context: undefined,
context: { res },
});
res.writeHead(init.status, init.statusText, init.headers);
if (body) {
Expand Down

0 comments on commit e2cc0cd

Please sign in to comment.