graphql-http 1.14.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +5 -3
  2. package/lib/audits/index.d.mts +1 -0
  3. package/lib/audits/index.d.ts +1 -0
  4. package/lib/audits/index.js +1 -0
  5. package/lib/audits/index.mjs +1 -0
  6. package/lib/audits/render.d.mts +8 -0
  7. package/lib/audits/render.d.ts +8 -0
  8. package/lib/audits/render.js +134 -0
  9. package/lib/audits/render.mjs +130 -0
  10. package/lib/audits/utils.js +6 -3
  11. package/lib/audits/utils.mjs +6 -3
  12. package/lib/handler.d.mts +3 -3
  13. package/lib/handler.d.ts +3 -3
  14. package/lib/handler.js +3 -3
  15. package/lib/handler.mjs +3 -3
  16. package/lib/use/express.d.mts +9 -3
  17. package/lib/use/express.d.ts +9 -3
  18. package/lib/use/express.js +1 -1
  19. package/lib/use/express.mjs +1 -1
  20. package/lib/use/fastify.d.mts +9 -3
  21. package/lib/use/fastify.d.ts +9 -3
  22. package/lib/use/fastify.js +1 -1
  23. package/lib/use/fastify.mjs +1 -1
  24. package/lib/use/fetch.d.mts +10 -4
  25. package/lib/use/fetch.d.ts +10 -4
  26. package/lib/use/fetch.js +6 -6
  27. package/lib/use/fetch.mjs +6 -6
  28. package/lib/use/http.d.mts +27 -0
  29. package/lib/use/http.d.ts +27 -0
  30. package/lib/use/http.js +72 -0
  31. package/lib/use/http.mjs +68 -0
  32. package/lib/use/http2.d.mts +39 -0
  33. package/lib/use/http2.d.ts +39 -0
  34. package/lib/use/http2.js +90 -0
  35. package/lib/use/http2.mjs +86 -0
  36. package/lib/use/koa.d.mts +9 -3
  37. package/lib/use/koa.d.ts +9 -3
  38. package/lib/use/koa.js +1 -1
  39. package/lib/use/koa.mjs +1 -1
  40. package/lib/use/node.d.mts +14 -4
  41. package/lib/use/node.d.ts +14 -4
  42. package/lib/use/node.js +5 -50
  43. package/lib/use/node.mjs +5 -50
  44. package/package.json +22 -9
  45. package/umd/graphql-http-audits.js +1006 -0
  46. package/umd/graphql-http-audits.min.js +1 -0
  47. package/umd/graphql-http-audits.min.js.gz +0 -0
  48. package/umd/graphql-http.min.js.gz +0 -0
@@ -1,4 +1,4 @@
1
- import { HandlerOptions, OperationContext } from '../handler';
1
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
2
2
  /**
3
3
  * The necessary API from the fetch environment for the handler.
4
4
  *
@@ -10,7 +10,13 @@ export interface FetchAPI {
10
10
  TextEncoder: typeof TextEncoder;
11
11
  }
12
12
  /**
13
- * Create a GraphQL over HTTP Protocol compliant request handler for
13
+ * Handler options when using the fetch adapter.
14
+ *
15
+ * @category Server/fetch
16
+ */
17
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, FetchAPI, Context>;
18
+ /**
19
+ * Create a GraphQL over HTTP spec compliant request handler for
14
20
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
15
21
  *
16
22
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -33,8 +39,8 @@ export interface FetchAPI {
33
39
  * console.log('Listening to port 4000');
34
40
  * ```
35
41
  *
36
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
42
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
37
43
  *
38
44
  * @category Server/fetch
39
45
  */
40
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, FetchAPI, Context>, fetchApi?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
46
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>, reqCtx?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
@@ -1,4 +1,4 @@
1
- import { HandlerOptions, OperationContext } from '../handler';
1
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
2
2
  /**
3
3
  * The necessary API from the fetch environment for the handler.
4
4
  *
@@ -10,7 +10,13 @@ export interface FetchAPI {
10
10
  TextEncoder: typeof TextEncoder;
11
11
  }
12
12
  /**
13
- * Create a GraphQL over HTTP Protocol compliant request handler for
13
+ * Handler options when using the fetch adapter.
14
+ *
15
+ * @category Server/fetch
16
+ */
17
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, FetchAPI, Context>;
18
+ /**
19
+ * Create a GraphQL over HTTP spec compliant request handler for
14
20
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
15
21
  *
16
22
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -33,8 +39,8 @@ export interface FetchAPI {
33
39
  * console.log('Listening to port 4000');
34
40
  * ```
35
41
  *
36
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
42
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
37
43
  *
38
44
  * @category Server/fetch
39
45
  */
40
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, FetchAPI, Context>, fetchApi?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
46
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>, reqCtx?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
package/lib/use/fetch.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
4
  const handler_1 = require("../handler");
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
7
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
8
8
  *
9
9
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -26,16 +26,16 @@ const handler_1 = require("../handler");
26
26
  * console.log('Listening to port 4000');
27
27
  * ```
28
28
  *
29
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
29
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
30
30
  *
31
31
  * @category Server/fetch
32
32
  */
33
- function createHandler(options, fetchApi = {}) {
33
+ function createHandler(options, reqCtx = {}) {
34
34
  const isProd = process.env.NODE_ENV === 'production';
35
35
  const api = {
36
- Response: fetchApi.Response || Response,
37
- TextEncoder: fetchApi.TextEncoder || TextEncoder,
38
- ReadableStream: fetchApi.ReadableStream || ReadableStream,
36
+ Response: reqCtx.Response || Response,
37
+ TextEncoder: reqCtx.TextEncoder || TextEncoder,
38
+ ReadableStream: reqCtx.ReadableStream || ReadableStream,
39
39
  };
40
40
  const handler = (0, handler_1.createHandler)(options);
41
41
  return async function handleRequest(req) {
package/lib/use/fetch.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHandler as createRawHandler, } from '../handler.mjs';
2
2
  /**
3
- * Create a GraphQL over HTTP Protocol compliant request handler for
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
4
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
5
5
  *
6
6
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -23,16 +23,16 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
23
23
  * console.log('Listening to port 4000');
24
24
  * ```
25
25
  *
26
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
26
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
27
27
  *
28
28
  * @category Server/fetch
29
29
  */
30
- export function createHandler(options, fetchApi = {}) {
30
+ export function createHandler(options, reqCtx = {}) {
31
31
  const isProd = process.env.NODE_ENV === 'production';
32
32
  const api = {
33
- Response: fetchApi.Response || Response,
34
- TextEncoder: fetchApi.TextEncoder || TextEncoder,
35
- ReadableStream: fetchApi.ReadableStream || ReadableStream,
33
+ Response: reqCtx.Response || Response,
34
+ TextEncoder: reqCtx.TextEncoder || TextEncoder,
35
+ ReadableStream: reqCtx.ReadableStream || ReadableStream,
36
36
  };
37
37
  const handler = createRawHandler(options);
38
38
  return async function handleRequest(req) {
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage, ServerResponse } from 'http';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http module.
13
+ *
14
+ * ```js
15
+ * import http from 'http';
16
+ * import { createHandler } from 'graphql-http/lib/use/http';
17
+ * import { schema } from './my-graphql-step';
18
+ *
19
+ * const server = http.createServer(createHandler({ schema }));
20
+ *
21
+ * server.listen(4000);
22
+ * console.log('Listening to port 4000');
23
+ * ```
24
+ *
25
+ * @category Server/http
26
+ */
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage, ServerResponse } from 'http';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http module.
13
+ *
14
+ * ```js
15
+ * import http from 'http';
16
+ * import { createHandler } from 'graphql-http/lib/use/http';
17
+ * import { schema } from './my-graphql-step';
18
+ *
19
+ * const server = http.createServer(createHandler({ schema }));
20
+ *
21
+ * server.listen(4000);
22
+ * console.log('Listening to port 4000');
23
+ * ```
24
+ *
25
+ * @category Server/http
26
+ */
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createHandler = void 0;
4
+ const handler_1 = require("../handler");
5
+ /**
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
+ * the Node environment http module.
8
+ *
9
+ * ```js
10
+ * import http from 'http';
11
+ * import { createHandler } from 'graphql-http/lib/use/http';
12
+ * import { schema } from './my-graphql-step';
13
+ *
14
+ * const server = http.createServer(createHandler({ schema }));
15
+ *
16
+ * server.listen(4000);
17
+ * console.log('Listening to port 4000');
18
+ * ```
19
+ *
20
+ * @category Server/http
21
+ */
22
+ function createHandler(options) {
23
+ const isProd = process.env.NODE_ENV === 'production';
24
+ const handle = (0, handler_1.createHandler)(options);
25
+ return async function requestListener(req, res) {
26
+ try {
27
+ if (!req.url) {
28
+ throw new Error('Missing request URL');
29
+ }
30
+ if (!req.method) {
31
+ throw new Error('Missing request method');
32
+ }
33
+ const [body, init] = await handle({
34
+ url: req.url,
35
+ method: req.method,
36
+ headers: req.headers,
37
+ body: () => new Promise((resolve) => {
38
+ let body = '';
39
+ req.on('data', (chunk) => (body += chunk));
40
+ req.on('end', () => resolve(body));
41
+ }),
42
+ raw: req,
43
+ context: undefined,
44
+ });
45
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
46
+ }
47
+ catch (err) {
48
+ // The handler shouldnt throw errors.
49
+ // If you wish to handle them differently, consider implementing your own request handler.
50
+ console.error('Internal error occurred during request handling. ' +
51
+ 'Please check your implementation.', err);
52
+ if (isProd) {
53
+ res.writeHead(500).end();
54
+ }
55
+ else {
56
+ res
57
+ .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
58
+ .end(JSON.stringify({
59
+ errors: [
60
+ err instanceof Error
61
+ ? {
62
+ message: err.message,
63
+ stack: err.stack,
64
+ }
65
+ : err,
66
+ ],
67
+ }));
68
+ }
69
+ }
70
+ };
71
+ }
72
+ exports.createHandler = createHandler;
@@ -0,0 +1,68 @@
1
+ import { createHandler as createRawHandler, } from '../handler.mjs';
2
+ /**
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
+ * the Node environment http module.
5
+ *
6
+ * ```js
7
+ * import http from 'http';
8
+ * import { createHandler } from 'graphql-http/lib/use/http';
9
+ * import { schema } from './my-graphql-step/index.mjs';
10
+ *
11
+ * const server = http.createServer(createHandler({ schema }));
12
+ *
13
+ * server.listen(4000);
14
+ * console.log('Listening to port 4000');
15
+ * ```
16
+ *
17
+ * @category Server/http
18
+ */
19
+ export function createHandler(options) {
20
+ const isProd = process.env.NODE_ENV === 'production';
21
+ const handle = createRawHandler(options);
22
+ return async function requestListener(req, res) {
23
+ try {
24
+ if (!req.url) {
25
+ throw new Error('Missing request URL');
26
+ }
27
+ if (!req.method) {
28
+ throw new Error('Missing request method');
29
+ }
30
+ const [body, init] = await handle({
31
+ url: req.url,
32
+ method: req.method,
33
+ headers: req.headers,
34
+ body: () => new Promise((resolve) => {
35
+ let body = '';
36
+ req.on('data', (chunk) => (body += chunk));
37
+ req.on('end', () => resolve(body));
38
+ }),
39
+ raw: req,
40
+ context: undefined,
41
+ });
42
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
43
+ }
44
+ catch (err) {
45
+ // The handler shouldnt throw errors.
46
+ // If you wish to handle them differently, consider implementing your own request handler.
47
+ console.error('Internal error occurred during request handling. ' +
48
+ 'Please check your implementation.', err);
49
+ if (isProd) {
50
+ res.writeHead(500).end();
51
+ }
52
+ else {
53
+ res
54
+ .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
55
+ .end(JSON.stringify({
56
+ errors: [
57
+ err instanceof Error
58
+ ? {
59
+ message: err.message,
60
+ stack: err.stack,
61
+ }
62
+ : err,
63
+ ],
64
+ }));
65
+ }
66
+ }
67
+ };
68
+ }
@@ -0,0 +1,39 @@
1
+ /// <reference types="node" />
2
+ import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http2
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Http2ServerRequest, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http2 module.
13
+ *
14
+ * ```shell
15
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
16
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
17
+ * ```
18
+ *
19
+ * ```js
20
+ * import fs from 'fs';
21
+ * import http2 from 'http2';
22
+ * import { createHandler } from 'graphql-http/lib/use/http2';
23
+ * import { schema } from './my-graphql-step';
24
+ *
25
+ * const server = http2.createSecureServer(
26
+ * {
27
+ * key: fs.readFileSync('localhost-privkey.pem'),
28
+ * cert: fs.readFileSync('localhost-cert.pem'),
29
+ * },
30
+ * createHandler({ schema }),
31
+ * );
32
+ *
33
+ * server.listen(4000);
34
+ * console.log('Listening to port 4000');
35
+ * ```
36
+ *
37
+ * @category Server/http2
38
+ */
39
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void>;
@@ -0,0 +1,39 @@
1
+ /// <reference types="node" />
2
+ import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http2
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Http2ServerRequest, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http2 module.
13
+ *
14
+ * ```shell
15
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
16
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
17
+ * ```
18
+ *
19
+ * ```js
20
+ * import fs from 'fs';
21
+ * import http2 from 'http2';
22
+ * import { createHandler } from 'graphql-http/lib/use/http2';
23
+ * import { schema } from './my-graphql-step';
24
+ *
25
+ * const server = http2.createSecureServer(
26
+ * {
27
+ * key: fs.readFileSync('localhost-privkey.pem'),
28
+ * cert: fs.readFileSync('localhost-cert.pem'),
29
+ * },
30
+ * createHandler({ schema }),
31
+ * );
32
+ *
33
+ * server.listen(4000);
34
+ * console.log('Listening to port 4000');
35
+ * ```
36
+ *
37
+ * @category Server/http2
38
+ */
39
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void>;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createHandler = void 0;
4
+ const handler_1 = require("../handler");
5
+ /**
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
+ * the Node environment http2 module.
8
+ *
9
+ * ```shell
10
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
11
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
12
+ * ```
13
+ *
14
+ * ```js
15
+ * import fs from 'fs';
16
+ * import http2 from 'http2';
17
+ * import { createHandler } from 'graphql-http/lib/use/http2';
18
+ * import { schema } from './my-graphql-step';
19
+ *
20
+ * const server = http2.createSecureServer(
21
+ * {
22
+ * key: fs.readFileSync('localhost-privkey.pem'),
23
+ * cert: fs.readFileSync('localhost-cert.pem'),
24
+ * },
25
+ * createHandler({ schema }),
26
+ * );
27
+ *
28
+ * server.listen(4000);
29
+ * console.log('Listening to port 4000');
30
+ * ```
31
+ *
32
+ * @category Server/http2
33
+ */
34
+ function createHandler(options) {
35
+ const isProd = process.env.NODE_ENV === 'production';
36
+ const handle = (0, handler_1.createHandler)(options);
37
+ return async function requestListener(req, res) {
38
+ try {
39
+ if (!req.url) {
40
+ throw new Error('Missing request URL');
41
+ }
42
+ if (!req.method) {
43
+ throw new Error('Missing request method');
44
+ }
45
+ const [body, init] = await handle({
46
+ url: req.url,
47
+ method: req.method,
48
+ headers: req.headers,
49
+ body: () => new Promise((resolve) => {
50
+ let body = '';
51
+ req.on('data', (chunk) => (body += chunk));
52
+ req.on('end', () => resolve(body));
53
+ }),
54
+ raw: req,
55
+ context: undefined,
56
+ });
57
+ res.writeHead(init.status, init.statusText, init.headers);
58
+ if (body) {
59
+ res.end(body);
60
+ }
61
+ else {
62
+ res.end();
63
+ }
64
+ }
65
+ catch (err) {
66
+ // The handler shouldnt throw errors.
67
+ // If you wish to handle them differently, consider implementing your own request handler.
68
+ console.error('Internal error occurred during request handling. ' +
69
+ 'Please check your implementation.', err);
70
+ if (isProd) {
71
+ res.writeHead(500).end();
72
+ }
73
+ else {
74
+ res
75
+ .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
76
+ .end(JSON.stringify({
77
+ errors: [
78
+ err instanceof Error
79
+ ? {
80
+ message: err.message,
81
+ stack: err.stack,
82
+ }
83
+ : err,
84
+ ],
85
+ }));
86
+ }
87
+ }
88
+ };
89
+ }
90
+ exports.createHandler = createHandler;
@@ -0,0 +1,86 @@
1
+ import { createHandler as createRawHandler, } from '../handler.mjs';
2
+ /**
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
+ * the Node environment http2 module.
5
+ *
6
+ * ```shell
7
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
8
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
9
+ * ```
10
+ *
11
+ * ```js
12
+ * import fs from 'fs';
13
+ * import http2 from 'http2';
14
+ * import { createHandler } from 'graphql-http/lib/use/http2';
15
+ * import { schema } from './my-graphql-step/index.mjs';
16
+ *
17
+ * const server = http2.createSecureServer(
18
+ * {
19
+ * key: fs.readFileSync('localhost-privkey.pem'),
20
+ * cert: fs.readFileSync('localhost-cert.pem'),
21
+ * },
22
+ * createHandler({ schema }),
23
+ * );
24
+ *
25
+ * server.listen(4000);
26
+ * console.log('Listening to port 4000');
27
+ * ```
28
+ *
29
+ * @category Server/http2
30
+ */
31
+ export function createHandler(options) {
32
+ const isProd = process.env.NODE_ENV === 'production';
33
+ const handle = createRawHandler(options);
34
+ return async function requestListener(req, res) {
35
+ try {
36
+ if (!req.url) {
37
+ throw new Error('Missing request URL');
38
+ }
39
+ if (!req.method) {
40
+ throw new Error('Missing request method');
41
+ }
42
+ const [body, init] = await handle({
43
+ url: req.url,
44
+ method: req.method,
45
+ headers: req.headers,
46
+ body: () => new Promise((resolve) => {
47
+ let body = '';
48
+ req.on('data', (chunk) => (body += chunk));
49
+ req.on('end', () => resolve(body));
50
+ }),
51
+ raw: req,
52
+ context: undefined,
53
+ });
54
+ res.writeHead(init.status, init.statusText, init.headers);
55
+ if (body) {
56
+ res.end(body);
57
+ }
58
+ else {
59
+ res.end();
60
+ }
61
+ }
62
+ catch (err) {
63
+ // The handler shouldnt throw errors.
64
+ // If you wish to handle them differently, consider implementing your own request handler.
65
+ console.error('Internal error occurred during request handling. ' +
66
+ 'Please check your implementation.', err);
67
+ if (isProd) {
68
+ res.writeHead(500).end();
69
+ }
70
+ else {
71
+ res
72
+ .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
73
+ .end(JSON.stringify({
74
+ errors: [
75
+ err instanceof Error
76
+ ? {
77
+ message: err.message,
78
+ stack: err.stack,
79
+ }
80
+ : err,
81
+ ],
82
+ }));
83
+ }
84
+ }
85
+ };
86
+ }
package/lib/use/koa.d.mts CHANGED
@@ -1,9 +1,15 @@
1
1
  /// <reference types="node" />
2
2
  import type { Middleware } from 'koa';
3
3
  import type { IncomingMessage } from 'http';
4
- import { HandlerOptions, OperationContext } from '../handler';
4
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Handler options when using the koa adapter.
7
+ *
8
+ * @category Server/koa
9
+ */
10
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
11
+ /**
12
+ * Create a GraphQL over HTTP spec compliant request handler for
7
13
  * the Koa framework.
8
14
  *
9
15
  * ```js
@@ -21,4 +27,4 @@ import { HandlerOptions, OperationContext } from '../handler';
21
27
  *
22
28
  * @category Server/koa
23
29
  */
24
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): Middleware;
30
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): Middleware;
package/lib/use/koa.d.ts CHANGED
@@ -1,9 +1,15 @@
1
1
  /// <reference types="node" />
2
2
  import type { Middleware } from 'koa';
3
3
  import type { IncomingMessage } from 'http';
4
- import { HandlerOptions, OperationContext } from '../handler';
4
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Handler options when using the koa adapter.
7
+ *
8
+ * @category Server/koa
9
+ */
10
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
11
+ /**
12
+ * Create a GraphQL over HTTP spec compliant request handler for
7
13
  * the Koa framework.
8
14
  *
9
15
  * ```js
@@ -21,4 +27,4 @@ import { HandlerOptions, OperationContext } from '../handler';
21
27
  *
22
28
  * @category Server/koa
23
29
  */
24
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): Middleware;
30
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): Middleware;
package/lib/use/koa.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
4
  const handler_1 = require("../handler");
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
7
  * the Koa framework.
8
8
  *
9
9
  * ```js
package/lib/use/koa.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHandler as createRawHandler, } from '../handler.mjs';
2
2
  /**
3
- * Create a GraphQL over HTTP Protocol compliant request handler for
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
4
  * the Koa framework.
5
5
  *
6
6
  * ```js