-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[graphql-http](../README.md) / [use/uWebSockets](../modules/use_uWebSockets.md) / RequestContext | ||
|
||
# Interface: RequestContext | ||
|
||
[use/uWebSockets](../modules/use_uWebSockets.md).RequestContext | ||
|
||
The context in the request for the handler. | ||
|
||
## Table of contents | ||
|
||
### Properties | ||
|
||
- [res](use_uWebSockets.RequestContext.md#res) | ||
|
||
## Properties | ||
|
||
### res | ||
|
||
• **res**: `HttpResponse` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
[graphql-http](../README.md) / use/uWebSockets | ||
|
||
# Module: use/uWebSockets | ||
|
||
## Table of contents | ||
|
||
### Interfaces | ||
|
||
- [RequestContext](../interfaces/use_uWebSockets.RequestContext.md) | ||
|
||
### Type Aliases | ||
|
||
- [HandlerOptions](use_uWebSockets.md#handleroptions) | ||
|
||
### Functions | ||
|
||
- [createHandler](use_uWebSockets.md#createhandler) | ||
|
||
## Server/uWebSockets | ||
|
||
### HandlerOptions | ||
|
||
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`HttpRequest`, [`RequestContext`](../interfaces/use_uWebSockets.RequestContext.md), `Context`\> | ||
|
||
Handler options when using the http adapter. | ||
|
||
#### Type parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` | | ||
|
||
___ | ||
|
||
### createHandler | ||
|
||
▸ **createHandler**<`Context`\>(`options`): (`res`: `HttpResponse`, `req`: `HttpRequest`) => `Promise`<`void`\> | ||
|
||
Create a GraphQL over HTTP spec compliant request handler for | ||
the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/). | ||
|
||
```js | ||
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version> | ||
import { createHandler } from 'graphql-http/lib/use/uWebSockets'; | ||
import { schema } from './my-graphql-schema'; | ||
|
||
uWS | ||
.App() | ||
.any('/graphql', createHandler({ schema })) | ||
.listen(4000, () => { | ||
console.log('Listening to port 4000'); | ||
}); | ||
``` | ||
|
||
#### Type parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` | | ||
|
||
#### Parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `options` | [`HandlerOptions`](use_uWebSockets.md#handleroptions)<`Context`\> | | ||
|
||
#### Returns | ||
|
||
`fn` | ||
|
||
▸ (`res`, `req`): `Promise`<`void`\> | ||
|
||
##### Parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `res` | `HttpResponse` | | ||
| `req` | `HttpRequest` | | ||
|
||
##### Returns | ||
|
||
`Promise`<`void`\> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { HttpRequest, HttpResponse } from 'uWebSockets.js'; | ||
import { | ||
createHandler as createRawHandler, | ||
HandlerOptions as RawHandlerOptions, | ||
OperationContext, | ||
} from '../handler'; | ||
|
||
/** | ||
* The context in the request for the handler. | ||
* | ||
* @category Server/uWebSockets | ||
*/ | ||
export interface RequestContext { | ||
res: HttpResponse; | ||
} | ||
|
||
/** | ||
* Handler options when using the http adapter. | ||
* | ||
* @category Server/uWebSockets | ||
*/ | ||
export type HandlerOptions<Context extends OperationContext = undefined> = | ||
RawHandlerOptions<HttpRequest, RequestContext, Context>; | ||
|
||
/** | ||
* Create a GraphQL over HTTP spec compliant request handler for | ||
* the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/). | ||
* | ||
* ```js | ||
* import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version> | ||
* import { createHandler } from 'graphql-http/lib/use/uWebSockets'; | ||
* import { schema } from './my-graphql-schema'; | ||
* | ||
* uWS | ||
* .App() | ||
* .any('/graphql', createHandler({ schema })) | ||
* .listen(4000, () => { | ||
* console.log('Listening to port 4000'); | ||
* }); | ||
* ``` | ||
* | ||
* @category Server/uWebSockets | ||
*/ | ||
export function createHandler<Context extends OperationContext = undefined>( | ||
options: HandlerOptions<Context>, | ||
): (res: HttpResponse, req: HttpRequest) => Promise<void> { | ||
const handle = createRawHandler(options); | ||
return async function requestListener(res, req) { | ||
let aborted = false; | ||
res.onAborted(() => (aborted = true)); | ||
try { | ||
let url = req.getUrl(); | ||
const query = req.getQuery(); | ||
if (query) { | ||
url += '?' + query; | ||
} | ||
const [body, init] = await handle({ | ||
url, | ||
method: req.getMethod().toUpperCase(), | ||
headers: { get: (key) => req.getHeader(key) }, | ||
body: () => | ||
new Promise<string>((resolve) => { | ||
let body = ''; | ||
if (aborted) { | ||
resolve(body); | ||
} else { | ||
res.onData((chunk, isLast) => { | ||
body += Buffer.from(chunk, 0, chunk.byteLength).toString(); | ||
if (isLast) { | ||
resolve(body); | ||
} | ||
}); | ||
} | ||
}), | ||
raw: req, | ||
context: { res }, | ||
}); | ||
if (!aborted) { | ||
res.writeStatus(`${init.status} ${init.statusText}`); | ||
for (const [key, val] of Object.entries(init.headers || {})) { | ||
res.writeHeader(key, val); | ||
} | ||
if (body) { | ||
res.end(body); | ||
} else { | ||
res.endWithoutBody(); | ||
} | ||
} | ||
} catch (err) { | ||
// The handler shouldnt throw errors. | ||
// If you wish to handle them differently, consider implementing your own request handler. | ||
console.error( | ||
'Internal error occurred during request handling. ' + | ||
'Please check your implementation.', | ||
err, | ||
); | ||
if (!aborted) { | ||
res.writeStatus('500 Internal Server Error').endWithoutBody(); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters