graphql-http / use/uWebSockets
Ƭ HandlerOptions<Context
>: HandlerOptions
<HttpRequest
, RequestContext
, Context
>
Handler options when using the http adapter.
Name | Type |
---|---|
Context |
extends OperationContext = undefined |
▸ 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.
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');
});
Name | Type |
---|---|
Context |
extends OperationContext = undefined |
Name | Type |
---|---|
options |
HandlerOptions <Context > |
fn
▸ (res
, req
): Promise
<void
>
Name | Type |
---|---|
res |
HttpResponse |
req |
HttpRequest |
Promise
<void
>
▸ parseRequestParams(req
, res
, abortedRef
): Promise
<RequestParams
| null
>
The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
It is important to pass in the abortedRef
so that the parser does not perform any
operations on a disposed request (see example).
If the HTTP request is not a well-formatted GraphQL over HTTP request, the function will respond
on the HttpResponse
argument and return null
.
If the HTTP request is a well-formatted GraphQL over HTTP request, but is invalid or malformed, the function will throw an error and it is up to the user to handle and respond as they see fit.
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
import { parseRequestParams } from 'graphql-http/lib/use/uWebSockets';
uWS
.App()
.any('/graphql', async (res, req) => {
const abortedRef = { current: false };
res.onAborted(() => (abortedRef.current = true));
try {
const maybeParams = await parseRequestParams(req, res, abortedRef);
if (!maybeParams) {
// not a well-formatted GraphQL over HTTP request,
// parser responded and there's nothing else to do
return;
}
// well-formatted GraphQL over HTTP request,
// with valid parameters
if (!abortedRef.current) {
res.writeStatus('200 OK');
res.end(JSON.stringify(maybeParams, null, ' '));
}
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
if (!abortedRef.current) {
res.writeStatus('400 Bad Request');
res.end(err.message);
}
}
})
.listen(4000, () => {
console.log('Listening to port 4000');
});
Name | Type |
---|---|
req |
HttpRequest |
res |
HttpResponse |
abortedRef |
Object |
abortedRef.current |
boolean |
Promise
<RequestParams
| null
>