1.22.0 (2023-08-28)
Features
- handler: Expose
parseRequestParams
from the core and each of the adapters (#111) (2caae00), closes #106
Examples
Request params parser usage with http
import http from 'http';
import { parseRequestParams } from 'graphql-http/lib/use/http';
const server = http.createServer(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');
Request params parser usage with http2
$ 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');
Request params parser usage with express
import express from 'express'; // yarn add express
import { parseRequestParams } from 'graphql-http/lib/use/express';
const app = express();
app.all('/graphql', async (req, res) => {
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);
}
});
app.listen({ port: 4000 });
console.log('Listening to port 4000');
Request params parser usage with fastify
import Fastify from 'fastify'; // yarn add fastify
import { parseRequestParams } from 'graphql-http/lib/use/fastify';
const fastify = Fastify();
fastify.all('/graphql', async (req, reply) => {
try {
const maybeParams = await parseRequestParams(req, reply);
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
reply.status(200).send(JSON.stringify(maybeParams, null, ' '));
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
reply.status(400).send(err.message);
}
});
fastify.listen({ port: 4000 });
console.log('Listening to port 4000');
Request params parser usage with Koa
import Koa from 'koa'; // yarn add koa
import mount from 'koa-mount'; // yarn add koa-mount
import { parseRequestParams } from 'graphql-http/lib/use/koa';
const app = new Koa();
app.use(
mount('/', async (ctx) => {
try {
const maybeParams = await parseRequestParams(ctx);
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
ctx.response.status = 200;
ctx.body = JSON.stringify(maybeParams, null, ' ');
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
ctx.response.status = 400;
ctx.body = err.message;
}
}),
);
app.listen({ port: 4000 });
console.log('Listening to port 4000');
Request params parser usage with uWebSockets.js
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');
});
Request params parser usage with Deno
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { parseRequestParams } from 'https://esm.sh/graphql-http/lib/use/fetch';
await serve(
async (req: Request) => {
const [path, _search] = req.url.split('?');
if (path.endsWith('/graphql')) {
try {
const paramsOrResponse = await parseRequestParams(req);
if (paramsOrResponse instanceof Response) {
// not a well-formatted GraphQL over HTTP request,
// parser created a response object to use
return paramsOrResponse;
}
// well-formatted GraphQL over HTTP request,
// with valid parameters
return new Response(JSON.stringify(paramsOrResponse, null, ' '), {
status: 200,
});
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
return new Response(err.message, { status: 400 });
}
} else {
return new Response(null, { status: 404 });
}
},
{
port: 4000, // Listening to port 4000
},
);
Request params parser usage with Bun
import { parseRequestParams } from 'graphql-http/lib/use/fetch'; // bun install graphql-http
export default {
port: 4000, // Listening to port 4000
async fetch(req) {
const [path, _search] = req.url.split('?');
if (path.endsWith('/graphql')) {
try {
const paramsOrResponse = await parseRequestParams(req);
if (paramsOrResponse instanceof Response) {
// not a well-formatted GraphQL over HTTP request,
// parser created a response object to use
return paramsOrResponse;
}
// well-formatted GraphQL over HTTP request,
// with valid parameters
return new Response(JSON.stringify(paramsOrResponse, null, ' '), {
status: 200,
});
} catch (err) {
// well-formatted GraphQL over HTTP request,
// but with invalid parameters
return new Response(err.message, { status: 400 });
}
} else {
return new Response(null, { status: 404 });
}
},
};