graphql-http / use/http2
Ƭ HandlerOptions<Context
>: HandlerOptions
<Http2ServerRequest
, RequestContext
, Context
>
Handler options when using the http adapter.
Name | Type |
---|---|
Context |
extends OperationContext = undefined |
▸ createHandler<Context
>(options
): (req
: Http2ServerRequest
, res
: Http2ServerResponse
) => Promise
<void
>
Create a GraphQL over HTTP spec compliant request handler for the Node environment http2 module.
$ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout localhost-privkey.pem -out localhost-cert.pem
import fs from 'fs';
import http2 from 'http2';
import { createHandler } from 'graphql-http/lib/use/http2';
import { schema } from './my-graphql-schema';
const server = http2.createSecureServer(
{
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem'),
},
createHandler({ schema }),
);
server.listen(4000);
console.log('Listening to port 4000');
Name | Type |
---|---|
Context |
extends OperationContext = undefined |
Name | Type |
---|---|
options |
HandlerOptions <Context > |
fn
▸ (req
, res
): Promise
<void
>
Name | Type |
---|---|
req |
Http2ServerRequest |
res |
Http2ServerResponse |
Promise
<void
>
▸ parseRequestParams(req
, res
): Promise
<RequestParams
| null
>
The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
If the HTTP request is not a well-formatted GraphQL over HTTP request, the function will respond
on the Http2ServerResponse
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.
$ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout localhost-privkey.pem -out localhost-cert.pem
import fs from 'fs';
import http2 from 'http2';
import { parseRequestParams } from 'graphql-http/lib/use/http2';
const server = http2.createSecureServer(
{
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem'),
},
async (req, res) => {
if (req.url.startsWith('/graphql')) {
try {
const maybeParams = await parseRequestParams(req, res);
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
res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
res.writeHead(400).end(err.message);
}
} else {
res.writeHead(404).end();
}
},
);
server.listen(4000);
console.log('Listening to port 4000');
Name | Type |
---|---|
req |
Http2ServerRequest |
res |
Http2ServerResponse |
Promise
<RequestParams
| null
>