graphql-http 1.15.0 → 1.17.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.
- package/README.md +38 -11
- package/lib/audits/utils.js +6 -3
- package/lib/audits/utils.mjs +6 -3
- package/lib/handler.d.mts +3 -3
- package/lib/handler.d.ts +3 -3
- package/lib/handler.js +3 -3
- package/lib/handler.mjs +3 -3
- package/lib/use/express.d.mts +18 -4
- package/lib/use/express.d.ts +18 -4
- package/lib/use/express.js +2 -2
- package/lib/use/express.mjs +2 -2
- package/lib/use/fastify.d.mts +18 -4
- package/lib/use/fastify.d.ts +18 -4
- package/lib/use/fastify.js +2 -2
- package/lib/use/fastify.mjs +2 -2
- package/lib/use/fetch.d.mts +10 -4
- package/lib/use/fetch.d.ts +10 -4
- package/lib/use/fetch.js +6 -6
- package/lib/use/fetch.mjs +6 -6
- package/lib/use/http.d.mts +35 -0
- package/lib/use/http.d.ts +35 -0
- package/lib/use/http.js +72 -0
- package/lib/use/http.mjs +68 -0
- package/lib/use/http2.d.mts +47 -0
- package/lib/use/http2.d.ts +47 -0
- package/lib/use/http2.js +90 -0
- package/lib/use/http2.mjs +86 -0
- package/lib/use/koa.d.mts +18 -4
- package/lib/use/koa.d.ts +18 -4
- package/lib/use/koa.js +2 -2
- package/lib/use/koa.mjs +2 -2
- package/lib/use/node.d.mts +14 -4
- package/lib/use/node.d.ts +14 -4
- package/lib/use/node.js +5 -50
- package/lib/use/node.mjs +5 -50
- package/package.json +38 -27
- package/umd/graphql-http-audits.js +6 -3
- package/umd/graphql-http-audits.min.js +1 -1
- package/umd/graphql-http-audits.min.js.gz +0 -0
package/lib/use/http.js
ADDED
|
@@ -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: { res },
|
|
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;
|
package/lib/use/http.mjs
ADDED
|
@@ -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: { res },
|
|
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,47 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
3
|
+
import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
|
|
4
|
+
/**
|
|
5
|
+
* The context in the request for the handler.
|
|
6
|
+
*
|
|
7
|
+
* @category Server/http2
|
|
8
|
+
*/
|
|
9
|
+
export interface RequestContext {
|
|
10
|
+
res: Http2ServerResponse;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Handler options when using the http adapter.
|
|
14
|
+
*
|
|
15
|
+
* @category Server/http2
|
|
16
|
+
*/
|
|
17
|
+
export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Http2ServerRequest, RequestContext, Context>;
|
|
18
|
+
/**
|
|
19
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
20
|
+
* the Node environment http2 module.
|
|
21
|
+
*
|
|
22
|
+
* ```shell
|
|
23
|
+
* $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
|
|
24
|
+
* -keyout localhost-privkey.pem -out localhost-cert.pem
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* ```js
|
|
28
|
+
* import fs from 'fs';
|
|
29
|
+
* import http2 from 'http2';
|
|
30
|
+
* import { createHandler } from 'graphql-http/lib/use/http2';
|
|
31
|
+
* import { schema } from './my-graphql-step';
|
|
32
|
+
*
|
|
33
|
+
* const server = http2.createSecureServer(
|
|
34
|
+
* {
|
|
35
|
+
* key: fs.readFileSync('localhost-privkey.pem'),
|
|
36
|
+
* cert: fs.readFileSync('localhost-cert.pem'),
|
|
37
|
+
* },
|
|
38
|
+
* createHandler({ schema }),
|
|
39
|
+
* );
|
|
40
|
+
*
|
|
41
|
+
* server.listen(4000);
|
|
42
|
+
* console.log('Listening to port 4000');
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @category Server/http2
|
|
46
|
+
*/
|
|
47
|
+
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
3
|
+
import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
|
|
4
|
+
/**
|
|
5
|
+
* The context in the request for the handler.
|
|
6
|
+
*
|
|
7
|
+
* @category Server/http2
|
|
8
|
+
*/
|
|
9
|
+
export interface RequestContext {
|
|
10
|
+
res: Http2ServerResponse;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Handler options when using the http adapter.
|
|
14
|
+
*
|
|
15
|
+
* @category Server/http2
|
|
16
|
+
*/
|
|
17
|
+
export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Http2ServerRequest, RequestContext, Context>;
|
|
18
|
+
/**
|
|
19
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
20
|
+
* the Node environment http2 module.
|
|
21
|
+
*
|
|
22
|
+
* ```shell
|
|
23
|
+
* $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
|
|
24
|
+
* -keyout localhost-privkey.pem -out localhost-cert.pem
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* ```js
|
|
28
|
+
* import fs from 'fs';
|
|
29
|
+
* import http2 from 'http2';
|
|
30
|
+
* import { createHandler } from 'graphql-http/lib/use/http2';
|
|
31
|
+
* import { schema } from './my-graphql-step';
|
|
32
|
+
*
|
|
33
|
+
* const server = http2.createSecureServer(
|
|
34
|
+
* {
|
|
35
|
+
* key: fs.readFileSync('localhost-privkey.pem'),
|
|
36
|
+
* cert: fs.readFileSync('localhost-cert.pem'),
|
|
37
|
+
* },
|
|
38
|
+
* createHandler({ schema }),
|
|
39
|
+
* );
|
|
40
|
+
*
|
|
41
|
+
* server.listen(4000);
|
|
42
|
+
* console.log('Listening to port 4000');
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @category Server/http2
|
|
46
|
+
*/
|
|
47
|
+
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void>;
|
package/lib/use/http2.js
ADDED
|
@@ -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: { res },
|
|
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: { res },
|
|
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,23 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { Middleware } from 'koa';
|
|
2
|
+
import type { Middleware, Response } 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
|
-
*
|
|
6
|
+
* The context in the request for the handler.
|
|
7
|
+
*
|
|
8
|
+
* @category Server/koa
|
|
9
|
+
*/
|
|
10
|
+
export interface RequestContext {
|
|
11
|
+
res: Response;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Handler options when using the koa adapter.
|
|
15
|
+
*
|
|
16
|
+
* @category Server/koa
|
|
17
|
+
*/
|
|
18
|
+
export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, RequestContext, Context>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
7
21
|
* the Koa framework.
|
|
8
22
|
*
|
|
9
23
|
* ```js
|
|
@@ -21,4 +35,4 @@ import { HandlerOptions, OperationContext } from '../handler';
|
|
|
21
35
|
*
|
|
22
36
|
* @category Server/koa
|
|
23
37
|
*/
|
|
24
|
-
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<
|
|
38
|
+
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): Middleware;
|
package/lib/use/koa.d.ts
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { Middleware } from 'koa';
|
|
2
|
+
import type { Middleware, Response } 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
|
-
*
|
|
6
|
+
* The context in the request for the handler.
|
|
7
|
+
*
|
|
8
|
+
* @category Server/koa
|
|
9
|
+
*/
|
|
10
|
+
export interface RequestContext {
|
|
11
|
+
res: Response;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Handler options when using the koa adapter.
|
|
15
|
+
*
|
|
16
|
+
* @category Server/koa
|
|
17
|
+
*/
|
|
18
|
+
export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, RequestContext, Context>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
7
21
|
* the Koa framework.
|
|
8
22
|
*
|
|
9
23
|
* ```js
|
|
@@ -21,4 +35,4 @@ import { HandlerOptions, OperationContext } from '../handler';
|
|
|
21
35
|
*
|
|
22
36
|
* @category Server/koa
|
|
23
37
|
*/
|
|
24
|
-
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<
|
|
38
|
+
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
|
|
6
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
7
7
|
* the Koa framework.
|
|
8
8
|
*
|
|
9
9
|
* ```js
|
|
@@ -42,7 +42,7 @@ function createHandler(options) {
|
|
|
42
42
|
});
|
|
43
43
|
},
|
|
44
44
|
raw: ctx.req,
|
|
45
|
-
context:
|
|
45
|
+
context: { res: ctx.response },
|
|
46
46
|
});
|
|
47
47
|
ctx.body = body;
|
|
48
48
|
ctx.response.status = init.status;
|
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
|
|
3
|
+
* Create a GraphQL over HTTP spec compliant request handler for
|
|
4
4
|
* the Koa framework.
|
|
5
5
|
*
|
|
6
6
|
* ```js
|
|
@@ -39,7 +39,7 @@ export function createHandler(options) {
|
|
|
39
39
|
});
|
|
40
40
|
},
|
|
41
41
|
raw: ctx.req,
|
|
42
|
-
context:
|
|
42
|
+
context: { res: ctx.response },
|
|
43
43
|
});
|
|
44
44
|
ctx.body = body;
|
|
45
45
|
ctx.response.status = init.status;
|
package/lib/use/node.d.mts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { HandlerOptions as HttpHandlerOptions } from './http';
|
|
3
|
+
import { OperationContext } from '../handler';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
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,
|
|
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
|
|
3
|
-
import {
|
|
2
|
+
import { HandlerOptions as HttpHandlerOptions } from './http';
|
|
3
|
+
import { OperationContext } from '../handler';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
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,
|
|
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
|
|
4
|
+
const http_1 = require("./http");
|
|
5
5
|
/**
|
|
6
|
-
* Create a GraphQL over HTTP
|
|
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
|
-
|
|
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;
|