graphql-http 1.15.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.
@@ -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
@@ -1,8 +1,16 @@
1
1
  /// <reference types="node" />
2
- import type { IncomingMessage, RequestListener } from 'http';
3
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as HttpHandlerOptions } from './http';
3
+ import { OperationContext } from '../handler';
4
4
  /**
5
- * Create a GraphQL over HTTP Protocol compliant request handler for
5
+ * Handler options when using the node adapter.
6
+ *
7
+ * @category Server/node
8
+ *
9
+ * @deprecated Please use {@link use/http.HandlerOptions | http} or {@link use/http2.HandlerOptions | http2} adapters instead.
10
+ */
11
+ export type HandlerOptions<Context extends OperationContext = undefined> = HttpHandlerOptions<Context>;
12
+ /**
13
+ * Create a GraphQL over HTTP spec compliant request handler for
6
14
  * the Node environment.
7
15
  *
8
16
  * ```js
@@ -17,5 +25,7 @@ import { HandlerOptions, OperationContext } from '../handler';
17
25
  * ```
18
26
  *
19
27
  * @category Server/node
28
+ *
29
+ * @deprecated Please use {@link use/http.createHandler | http} or {@link use/http2.createHandler | http2} adapters instead.
20
30
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): RequestListener;
31
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: import("http").IncomingMessage, res: import("http").ServerResponse<import("http").IncomingMessage>) => Promise<void>;
package/lib/use/node.d.ts CHANGED
@@ -1,8 +1,16 @@
1
1
  /// <reference types="node" />
2
- import type { IncomingMessage, RequestListener } from 'http';
3
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as HttpHandlerOptions } from './http';
3
+ import { OperationContext } from '../handler';
4
4
  /**
5
- * Create a GraphQL over HTTP Protocol compliant request handler for
5
+ * Handler options when using the node adapter.
6
+ *
7
+ * @category Server/node
8
+ *
9
+ * @deprecated Please use {@link use/http.HandlerOptions | http} or {@link use/http2.HandlerOptions | http2} adapters instead.
10
+ */
11
+ export type HandlerOptions<Context extends OperationContext = undefined> = HttpHandlerOptions<Context>;
12
+ /**
13
+ * Create a GraphQL over HTTP spec compliant request handler for
6
14
  * the Node environment.
7
15
  *
8
16
  * ```js
@@ -17,5 +25,7 @@ import { HandlerOptions, OperationContext } from '../handler';
17
25
  * ```
18
26
  *
19
27
  * @category Server/node
28
+ *
29
+ * @deprecated Please use {@link use/http.createHandler | http} or {@link use/http2.createHandler | http2} adapters instead.
20
30
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): RequestListener;
31
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: import("http").IncomingMessage, res: import("http").ServerResponse<import("http").IncomingMessage>) => Promise<void>;
package/lib/use/node.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
- const handler_1 = require("../handler");
4
+ const http_1 = require("./http");
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 Node environment.
8
8
  *
9
9
  * ```js
@@ -18,55 +18,10 @@ const handler_1 = require("../handler");
18
18
  * ```
19
19
  *
20
20
  * @category Server/node
21
+ *
22
+ * @deprecated Please use {@link use/http.createHandler | http} or {@link use/http2.createHandler | http2} adapters instead.
21
23
  */
22
24
  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
- };
25
+ return (0, http_1.createHandler)(options);
71
26
  }
72
27
  exports.createHandler = createHandler;
package/lib/use/node.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { createHandler as createRawHandler, } from '../handler.mjs';
1
+ import { createHandler as httpCreateHandler, } from './http.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 Node environment.
5
5
  *
6
6
  * ```js
@@ -15,54 +15,9 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
15
15
  * ```
16
16
  *
17
17
  * @category Server/node
18
+ *
19
+ * @deprecated Please use {@link use/http.createHandler | http} or {@link use/http2.createHandler | http2} adapters instead.
18
20
  */
19
21
  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
- };
22
+ return httpCreateHandler(options);
68
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-http",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "Simple, pluggable, zero-dependency, GraphQL over HTTP spec compliant server, client and audit suite.",
5
5
  "keywords": [
6
6
  "graphql",
@@ -45,6 +45,16 @@
45
45
  "require": "./lib/use/node.js",
46
46
  "import": "./lib/use/node.mjs"
47
47
  },
48
+ "./lib/use/http": {
49
+ "types": "./lib/use/http.d.ts",
50
+ "require": "./lib/use/http.js",
51
+ "import": "./lib/use/http.mjs"
52
+ },
53
+ "./lib/use/http2": {
54
+ "types": "./lib/use/http2.d.ts",
55
+ "require": "./lib/use/http2.js",
56
+ "import": "./lib/use/http2.mjs"
57
+ },
48
58
  "./lib/use/express": {
49
59
  "types": "./lib/use/express.d.ts",
50
60
  "require": "./lib/use/express.js",
@@ -81,7 +91,7 @@
81
91
  "lint:prettier": "prettier -c .",
82
92
  "lint": "yarn lint:eslint && yarn lint:prettier",
83
93
  "type-check": "tsc --noEmit",
84
- "test": "NODE_OPTIONS=--experimental-vm-modules jest",
94
+ "test": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest",
85
95
  "build:esm": "tsc -b tsconfig.esm.json && node scripts/esm-post-process.mjs",
86
96
  "build:cjs": "tsc -b tsconfig.cjs.json",
87
97
  "build:umd": "rollup --configPlugin typescript --config rollup.config.ts",
@@ -116,22 +116,25 @@
116
116
  bodyAsExecutionResult: {
117
117
  data: {
118
118
  async toBe(val) {
119
+ const clonedRes = res.clone(); // allow the body to be re-read
119
120
  const body = await assertBodyAsExecutionResult(res);
120
121
  if (body.data !== val) {
121
- throw new AuditError(res, `Response body execution result data is not "${val}"`);
122
+ throw new AuditError(clonedRes, `Response body execution result data is not "${val}"`);
122
123
  }
123
124
  },
124
125
  },
125
126
  async toHaveProperty(key) {
127
+ const clonedRes = res.clone(); // allow the body to be re-read
126
128
  const body = await assertBodyAsExecutionResult(res);
127
129
  if (!(key in body)) {
128
- throw new AuditError(res, `Response body execution result does not have a property "${key}"`);
130
+ throw new AuditError(clonedRes, `Response body execution result does not have a property "${key}"`);
129
131
  }
130
132
  },
131
133
  async notToHaveProperty(key) {
134
+ const clonedRes = res.clone(); // allow the body to be re-read
132
135
  const body = await assertBodyAsExecutionResult(res);
133
136
  if (key in body) {
134
- throw new AuditError(res, `Response body execution result has a property "${key}"`);
137
+ throw new AuditError(clonedRes, `Response body execution result has a property "${key}"`);
135
138
  }
136
139
  },
137
140
  },