graphql-http 1.9.0 → 1.11.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 +16 -0
- package/lib/audits/common.d.mts +3 -3
- package/lib/audits/common.d.ts +3 -3
- package/lib/handler.d.mts +17 -8
- package/lib/handler.d.ts +17 -8
- package/lib/handler.js +4 -1
- package/lib/handler.mjs +4 -1
- package/lib/use/koa.d.mts +24 -0
- package/lib/use/koa.d.ts +24 -0
- package/lib/use/koa.js +78 -0
- package/lib/use/koa.mjs +74 -0
- package/package.json +39 -27
- package/umd/graphql-http.min.js.gz +0 -0
package/README.md
CHANGED
|
@@ -141,6 +141,21 @@ fastify.listen({ port: 4000 });
|
|
|
141
141
|
console.log('Listening to port 4000');
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
+
##### With [`Koa`](https://koajs.com/)
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import Koa from 'koa'; // yarn add koa
|
|
148
|
+
import mount from 'koa-mount'; // yarn add koa-mount
|
|
149
|
+
import { createHandler } from 'graphql-http/lib/use/koa';
|
|
150
|
+
import { schema } from './previous-step';
|
|
151
|
+
|
|
152
|
+
const app = new Koa();
|
|
153
|
+
app.use(mount('/graphql', createHandler({ schema })));
|
|
154
|
+
|
|
155
|
+
app.listen({ port: 4000 });
|
|
156
|
+
console.log('Listening to port 4000');
|
|
157
|
+
```
|
|
158
|
+
|
|
144
159
|
##### With [`Deno`](https://deno.land/)
|
|
145
160
|
|
|
146
161
|
```ts
|
|
@@ -733,6 +748,7 @@ If you want a feature-full server with bleeding edge technologies, you're recomm
|
|
|
733
748
|
| ------------------------------------------------------------------ | -------------------------------------------------------- |
|
|
734
749
|
| [graphql-yoga](https://www.the-guild.dev/graphql/yoga-server) | [✅ Compliant](/implementations/graphql-yoga/README.md) |
|
|
735
750
|
| [hotchocolate](https://chillicream.com/docs/hotchocolate) | [✅ Compliant](/implementations/hotchocolate/README.md) |
|
|
751
|
+
| [pioneer](https://pioneer.dexclaimation.com/) | [✅ Compliant](/implementations/pioneer/README.md) |
|
|
736
752
|
| [postgraphile](https://www.graphile.org/postgraphile/) | [✅ Compliant](/implementations/postgraphile/README.md) |
|
|
737
753
|
| [apollo-server](https://www.apollographql.com/docs/apollo-server/) | [✅ Compliant](/implementations/apollo-server/README.md) |
|
|
738
754
|
|
package/lib/audits/common.d.mts
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @category Audits
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export type AuditRequirement = 'MUST' | 'SHOULD' | 'MAY';
|
|
12
12
|
/**
|
|
13
13
|
* Audit name starting with the audit requirement level.
|
|
14
14
|
*
|
|
15
15
|
* @category Audits
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
17
|
+
export type AuditName = `${AuditRequirement} ${string}`;
|
|
18
18
|
/**
|
|
19
19
|
* Actual audit test returning an result.
|
|
20
20
|
*
|
|
@@ -55,4 +55,4 @@ export interface AuditFail {
|
|
|
55
55
|
*
|
|
56
56
|
* @category Audits
|
|
57
57
|
*/
|
|
58
|
-
export
|
|
58
|
+
export type AuditResult = AuditOk | AuditFail;
|
package/lib/audits/common.d.ts
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @category Audits
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export type AuditRequirement = 'MUST' | 'SHOULD' | 'MAY';
|
|
12
12
|
/**
|
|
13
13
|
* Audit name starting with the audit requirement level.
|
|
14
14
|
*
|
|
15
15
|
* @category Audits
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
17
|
+
export type AuditName = `${AuditRequirement} ${string}`;
|
|
18
18
|
/**
|
|
19
19
|
* Actual audit test returning an result.
|
|
20
20
|
*
|
|
@@ -55,4 +55,4 @@ export interface AuditFail {
|
|
|
55
55
|
*
|
|
56
56
|
* @category Audits
|
|
57
57
|
*/
|
|
58
|
-
export
|
|
58
|
+
export type AuditResult = AuditOk | AuditFail;
|
package/lib/handler.d.mts
CHANGED
|
@@ -10,7 +10,7 @@ import { RequestParams } from './common';
|
|
|
10
10
|
*
|
|
11
11
|
* @category Server
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
13
|
+
export type RequestHeaders = {
|
|
14
14
|
/**
|
|
15
15
|
* Always an array in Node. Duplicates are added to it.
|
|
16
16
|
* Not necessarily true for other environments.
|
|
@@ -54,7 +54,7 @@ export interface Request<Raw, Context> {
|
|
|
54
54
|
*
|
|
55
55
|
* @category Server
|
|
56
56
|
*/
|
|
57
|
-
export
|
|
57
|
+
export type ResponseHeaders = {
|
|
58
58
|
accept?: string;
|
|
59
59
|
allow?: string;
|
|
60
60
|
'content-type'?: string;
|
|
@@ -65,7 +65,7 @@ export declare type ResponseHeaders = {
|
|
|
65
65
|
*
|
|
66
66
|
* @category Server
|
|
67
67
|
*/
|
|
68
|
-
export
|
|
68
|
+
export type ResponseBody = string;
|
|
69
69
|
/**
|
|
70
70
|
* Server agnostic response options (ex. status and headers) returned from
|
|
71
71
|
* `graphql-http` needing to be coerced to the server implementation in use.
|
|
@@ -83,7 +83,7 @@ export interface ResponseInit {
|
|
|
83
83
|
*
|
|
84
84
|
* @category Server
|
|
85
85
|
*/
|
|
86
|
-
export
|
|
86
|
+
export type Response = readonly [body: ResponseBody | null, init: ResponseInit];
|
|
87
87
|
/**
|
|
88
88
|
* Checks whether the passed value is the `graphql-http` server agnostic response.
|
|
89
89
|
*
|
|
@@ -100,9 +100,9 @@ export declare function isResponse(val: unknown): val is Response;
|
|
|
100
100
|
*
|
|
101
101
|
* @category Server
|
|
102
102
|
*/
|
|
103
|
-
export
|
|
103
|
+
export type OperationContext = Record<PropertyKey, unknown> | symbol | number | string | boolean | undefined | null;
|
|
104
104
|
/** @category Server */
|
|
105
|
-
export
|
|
105
|
+
export type OperationArgs<Context extends OperationContext = undefined> = ExecutionArgs & {
|
|
106
106
|
contextValue?: Context;
|
|
107
107
|
};
|
|
108
108
|
/** @category Server */
|
|
@@ -150,6 +150,15 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
|
|
|
150
150
|
* GraphQL operation AST getter used for detecting the operation type.
|
|
151
151
|
*/
|
|
152
152
|
getOperationAST?: typeof graphqlGetOperationAST;
|
|
153
|
+
/**
|
|
154
|
+
* The GraphQL root value or resolvers to go alongside the execution.
|
|
155
|
+
* Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.
|
|
156
|
+
*
|
|
157
|
+
* If you return from `onSubscribe`, and the returned value is
|
|
158
|
+
* missing the `rootValue` field, the relevant operation root
|
|
159
|
+
* will be used instead.
|
|
160
|
+
*/
|
|
161
|
+
rootValue?: unknown;
|
|
153
162
|
/**
|
|
154
163
|
* The subscribe callback executed right after processing the request
|
|
155
164
|
* before proceeding with the GraphQL operation execution.
|
|
@@ -204,7 +213,7 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
|
|
|
204
213
|
*
|
|
205
214
|
* @category Server
|
|
206
215
|
*/
|
|
207
|
-
export
|
|
216
|
+
export type Handler<RequestRaw = unknown, RequestContext = unknown> = (req: Request<RequestRaw, RequestContext>) => Promise<Response>;
|
|
208
217
|
/**
|
|
209
218
|
* Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
|
|
210
219
|
* be used with your favourite server library.
|
|
@@ -266,7 +275,7 @@ export declare function createHandler<RequestRaw = unknown, RequestContext = unk
|
|
|
266
275
|
*
|
|
267
276
|
* @category Server
|
|
268
277
|
*/
|
|
269
|
-
export
|
|
278
|
+
export type AcceptableMediaType = 'application/graphql-response+json' | 'application/json';
|
|
270
279
|
/**
|
|
271
280
|
* Inspects the request and detects the appropriate/acceptable Media-Type
|
|
272
281
|
* looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
|
package/lib/handler.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { RequestParams } from './common';
|
|
|
10
10
|
*
|
|
11
11
|
* @category Server
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
13
|
+
export type RequestHeaders = {
|
|
14
14
|
/**
|
|
15
15
|
* Always an array in Node. Duplicates are added to it.
|
|
16
16
|
* Not necessarily true for other environments.
|
|
@@ -54,7 +54,7 @@ export interface Request<Raw, Context> {
|
|
|
54
54
|
*
|
|
55
55
|
* @category Server
|
|
56
56
|
*/
|
|
57
|
-
export
|
|
57
|
+
export type ResponseHeaders = {
|
|
58
58
|
accept?: string;
|
|
59
59
|
allow?: string;
|
|
60
60
|
'content-type'?: string;
|
|
@@ -65,7 +65,7 @@ export declare type ResponseHeaders = {
|
|
|
65
65
|
*
|
|
66
66
|
* @category Server
|
|
67
67
|
*/
|
|
68
|
-
export
|
|
68
|
+
export type ResponseBody = string;
|
|
69
69
|
/**
|
|
70
70
|
* Server agnostic response options (ex. status and headers) returned from
|
|
71
71
|
* `graphql-http` needing to be coerced to the server implementation in use.
|
|
@@ -83,7 +83,7 @@ export interface ResponseInit {
|
|
|
83
83
|
*
|
|
84
84
|
* @category Server
|
|
85
85
|
*/
|
|
86
|
-
export
|
|
86
|
+
export type Response = readonly [body: ResponseBody | null, init: ResponseInit];
|
|
87
87
|
/**
|
|
88
88
|
* Checks whether the passed value is the `graphql-http` server agnostic response.
|
|
89
89
|
*
|
|
@@ -100,9 +100,9 @@ export declare function isResponse(val: unknown): val is Response;
|
|
|
100
100
|
*
|
|
101
101
|
* @category Server
|
|
102
102
|
*/
|
|
103
|
-
export
|
|
103
|
+
export type OperationContext = Record<PropertyKey, unknown> | symbol | number | string | boolean | undefined | null;
|
|
104
104
|
/** @category Server */
|
|
105
|
-
export
|
|
105
|
+
export type OperationArgs<Context extends OperationContext = undefined> = ExecutionArgs & {
|
|
106
106
|
contextValue?: Context;
|
|
107
107
|
};
|
|
108
108
|
/** @category Server */
|
|
@@ -150,6 +150,15 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
|
|
|
150
150
|
* GraphQL operation AST getter used for detecting the operation type.
|
|
151
151
|
*/
|
|
152
152
|
getOperationAST?: typeof graphqlGetOperationAST;
|
|
153
|
+
/**
|
|
154
|
+
* The GraphQL root value or resolvers to go alongside the execution.
|
|
155
|
+
* Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.
|
|
156
|
+
*
|
|
157
|
+
* If you return from `onSubscribe`, and the returned value is
|
|
158
|
+
* missing the `rootValue` field, the relevant operation root
|
|
159
|
+
* will be used instead.
|
|
160
|
+
*/
|
|
161
|
+
rootValue?: unknown;
|
|
153
162
|
/**
|
|
154
163
|
* The subscribe callback executed right after processing the request
|
|
155
164
|
* before proceeding with the GraphQL operation execution.
|
|
@@ -204,7 +213,7 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
|
|
|
204
213
|
*
|
|
205
214
|
* @category Server
|
|
206
215
|
*/
|
|
207
|
-
export
|
|
216
|
+
export type Handler<RequestRaw = unknown, RequestContext = unknown> = (req: Request<RequestRaw, RequestContext>) => Promise<Response>;
|
|
208
217
|
/**
|
|
209
218
|
* Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
|
|
210
219
|
* be used with your favourite server library.
|
|
@@ -266,7 +275,7 @@ export declare function createHandler<RequestRaw = unknown, RequestContext = unk
|
|
|
266
275
|
*
|
|
267
276
|
* @category Server
|
|
268
277
|
*/
|
|
269
|
-
export
|
|
278
|
+
export type AcceptableMediaType = 'application/graphql-response+json' | 'application/json';
|
|
270
279
|
/**
|
|
271
280
|
* Inspects the request and detects the appropriate/acceptable Media-Type
|
|
272
281
|
* looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
|
package/lib/handler.js
CHANGED
|
@@ -76,7 +76,7 @@ exports.isResponse = isResponse;
|
|
|
76
76
|
* @category Server
|
|
77
77
|
*/
|
|
78
78
|
function createHandler(options) {
|
|
79
|
-
const { schema, context, validate = graphql_1.validate, execute = graphql_1.execute, parse = graphql_1.parse, getOperationAST = graphql_1.getOperationAST, onSubscribe, onOperation, } = options;
|
|
79
|
+
const { schema, context, validate = graphql_1.validate, execute = graphql_1.execute, parse = graphql_1.parse, getOperationAST = graphql_1.getOperationAST, rootValue, onSubscribe, onOperation, } = options;
|
|
80
80
|
return async function handler(req) {
|
|
81
81
|
var _a, _b;
|
|
82
82
|
const method = req.method;
|
|
@@ -260,6 +260,9 @@ function createHandler(options) {
|
|
|
260
260
|
},
|
|
261
261
|
];
|
|
262
262
|
}
|
|
263
|
+
if (!('rootValue' in args)) {
|
|
264
|
+
args.rootValue = rootValue;
|
|
265
|
+
}
|
|
263
266
|
if (!('contextValue' in args)) {
|
|
264
267
|
const resOrContext = typeof context === 'function' ? await context(req, params) : context;
|
|
265
268
|
if (isResponse(resOrContext))
|
package/lib/handler.mjs
CHANGED
|
@@ -72,7 +72,7 @@ export function isResponse(val) {
|
|
|
72
72
|
* @category Server
|
|
73
73
|
*/
|
|
74
74
|
export function createHandler(options) {
|
|
75
|
-
const { schema, context, validate = graphqlValidate, execute = graphqlExecute, parse = graphqlParse, getOperationAST = graphqlGetOperationAST, onSubscribe, onOperation, } = options;
|
|
75
|
+
const { schema, context, validate = graphqlValidate, execute = graphqlExecute, parse = graphqlParse, getOperationAST = graphqlGetOperationAST, rootValue, onSubscribe, onOperation, } = options;
|
|
76
76
|
return async function handler(req) {
|
|
77
77
|
var _a, _b;
|
|
78
78
|
const method = req.method;
|
|
@@ -256,6 +256,9 @@ export function createHandler(options) {
|
|
|
256
256
|
},
|
|
257
257
|
];
|
|
258
258
|
}
|
|
259
|
+
if (!('rootValue' in args)) {
|
|
260
|
+
args.rootValue = rootValue;
|
|
261
|
+
}
|
|
259
262
|
if (!('contextValue' in args)) {
|
|
260
263
|
const resOrContext = typeof context === 'function' ? await context(req, params) : context;
|
|
261
264
|
if (isResponse(resOrContext))
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Middleware } from 'koa';
|
|
3
|
+
import type { IncomingMessage } from 'http';
|
|
4
|
+
import { HandlerOptions, OperationContext } from '../handler';
|
|
5
|
+
/**
|
|
6
|
+
* Create a GraphQL over HTTP Protocol compliant request handler for
|
|
7
|
+
* the Koa framework.
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import Koa from 'koa'; // yarn add koa
|
|
11
|
+
* import mount from 'koa-mount'; // yarn add koa-mount
|
|
12
|
+
* import { createHandler } from 'graphql-http/lib/use/koa';
|
|
13
|
+
* import { schema } from './my-graphql-schema';
|
|
14
|
+
*
|
|
15
|
+
* const app = new Koa();
|
|
16
|
+
* app.use(mount('/', createHandler({ schema })));
|
|
17
|
+
*
|
|
18
|
+
* app.listen({ port: 4000 });
|
|
19
|
+
* console.log('Listening to port 4000');
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @category Server/koa
|
|
23
|
+
*/
|
|
24
|
+
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): Middleware;
|
package/lib/use/koa.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Middleware } from 'koa';
|
|
3
|
+
import type { IncomingMessage } from 'http';
|
|
4
|
+
import { HandlerOptions, OperationContext } from '../handler';
|
|
5
|
+
/**
|
|
6
|
+
* Create a GraphQL over HTTP Protocol compliant request handler for
|
|
7
|
+
* the Koa framework.
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import Koa from 'koa'; // yarn add koa
|
|
11
|
+
* import mount from 'koa-mount'; // yarn add koa-mount
|
|
12
|
+
* import { createHandler } from 'graphql-http/lib/use/koa';
|
|
13
|
+
* import { schema } from './my-graphql-schema';
|
|
14
|
+
*
|
|
15
|
+
* const app = new Koa();
|
|
16
|
+
* app.use(mount('/', createHandler({ schema })));
|
|
17
|
+
*
|
|
18
|
+
* app.listen({ port: 4000 });
|
|
19
|
+
* console.log('Listening to port 4000');
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @category Server/koa
|
|
23
|
+
*/
|
|
24
|
+
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<IncomingMessage, undefined, Context>): Middleware;
|
package/lib/use/koa.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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 Protocol compliant request handler for
|
|
7
|
+
* the Koa framework.
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import Koa from 'koa'; // yarn add koa
|
|
11
|
+
* import mount from 'koa-mount'; // yarn add koa-mount
|
|
12
|
+
* import { createHandler } from 'graphql-http/lib/use/koa';
|
|
13
|
+
* import { schema } from './my-graphql-schema';
|
|
14
|
+
*
|
|
15
|
+
* const app = new Koa();
|
|
16
|
+
* app.use(mount('/', createHandler({ schema })));
|
|
17
|
+
*
|
|
18
|
+
* app.listen({ port: 4000 });
|
|
19
|
+
* console.log('Listening to port 4000');
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @category Server/koa
|
|
23
|
+
*/
|
|
24
|
+
function createHandler(options) {
|
|
25
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
26
|
+
const handle = (0, handler_1.createHandler)(options);
|
|
27
|
+
return async function requestListener(ctx) {
|
|
28
|
+
try {
|
|
29
|
+
const [body, init] = await handle({
|
|
30
|
+
url: ctx.url,
|
|
31
|
+
method: ctx.method,
|
|
32
|
+
headers: ctx.headers,
|
|
33
|
+
body: () => {
|
|
34
|
+
if (ctx.body) {
|
|
35
|
+
// in case koa has a body parser
|
|
36
|
+
return ctx.body;
|
|
37
|
+
}
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
let body = '';
|
|
40
|
+
ctx.req.on('data', (chunk) => (body += chunk));
|
|
41
|
+
ctx.req.on('end', () => resolve(body));
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
raw: ctx.req,
|
|
45
|
+
context: undefined,
|
|
46
|
+
});
|
|
47
|
+
ctx.body = body;
|
|
48
|
+
ctx.response.status = init.status;
|
|
49
|
+
ctx.response.message = init.statusText;
|
|
50
|
+
if (init.headers) {
|
|
51
|
+
for (const [name, value] of Object.entries(init.headers)) {
|
|
52
|
+
ctx.response.set(name, value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
// The handler shouldnt throw errors.
|
|
58
|
+
// If you wish to handle them differently, consider implementing your own request handler.
|
|
59
|
+
console.error('Internal error occurred during request handling. ' +
|
|
60
|
+
'Please check your implementation.', err);
|
|
61
|
+
ctx.response.status = 500;
|
|
62
|
+
if (!isProd) {
|
|
63
|
+
ctx.response.set('content-type', 'application/json; charset=utf-8');
|
|
64
|
+
ctx.body = {
|
|
65
|
+
errors: [
|
|
66
|
+
err instanceof Error
|
|
67
|
+
? {
|
|
68
|
+
message: err.message,
|
|
69
|
+
stack: err.stack,
|
|
70
|
+
}
|
|
71
|
+
: err,
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
exports.createHandler = createHandler;
|
package/lib/use/koa.mjs
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createHandler as createRawHandler, } from '../handler.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Create a GraphQL over HTTP Protocol compliant request handler for
|
|
4
|
+
* the Koa framework.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import Koa from 'koa'; // yarn add koa
|
|
8
|
+
* import mount from 'koa-mount'; // yarn add koa-mount
|
|
9
|
+
* import { createHandler } from 'graphql-http/lib/use/koa';
|
|
10
|
+
* import { schema } from './my-graphql-schema/index.mjs';
|
|
11
|
+
*
|
|
12
|
+
* const app = new Koa();
|
|
13
|
+
* app.use(mount('/', createHandler({ schema })));
|
|
14
|
+
*
|
|
15
|
+
* app.listen({ port: 4000 });
|
|
16
|
+
* console.log('Listening to port 4000');
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @category Server/koa
|
|
20
|
+
*/
|
|
21
|
+
export function createHandler(options) {
|
|
22
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
23
|
+
const handle = createRawHandler(options);
|
|
24
|
+
return async function requestListener(ctx) {
|
|
25
|
+
try {
|
|
26
|
+
const [body, init] = await handle({
|
|
27
|
+
url: ctx.url,
|
|
28
|
+
method: ctx.method,
|
|
29
|
+
headers: ctx.headers,
|
|
30
|
+
body: () => {
|
|
31
|
+
if (ctx.body) {
|
|
32
|
+
// in case koa has a body parser
|
|
33
|
+
return ctx.body;
|
|
34
|
+
}
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
let body = '';
|
|
37
|
+
ctx.req.on('data', (chunk) => (body += chunk));
|
|
38
|
+
ctx.req.on('end', () => resolve(body));
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
raw: ctx.req,
|
|
42
|
+
context: undefined,
|
|
43
|
+
});
|
|
44
|
+
ctx.body = body;
|
|
45
|
+
ctx.response.status = init.status;
|
|
46
|
+
ctx.response.message = init.statusText;
|
|
47
|
+
if (init.headers) {
|
|
48
|
+
for (const [name, value] of Object.entries(init.headers)) {
|
|
49
|
+
ctx.response.set(name, value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
// The handler shouldnt throw errors.
|
|
55
|
+
// If you wish to handle them differently, consider implementing your own request handler.
|
|
56
|
+
console.error('Internal error occurred during request handling. ' +
|
|
57
|
+
'Please check your implementation.', err);
|
|
58
|
+
ctx.response.status = 500;
|
|
59
|
+
if (!isProd) {
|
|
60
|
+
ctx.response.set('content-type', 'application/json; charset=utf-8');
|
|
61
|
+
ctx.body = {
|
|
62
|
+
errors: [
|
|
63
|
+
err instanceof Error
|
|
64
|
+
? {
|
|
65
|
+
message: err.message,
|
|
66
|
+
stack: err.stack,
|
|
67
|
+
}
|
|
68
|
+
: err,
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Simple, pluggable, zero-dependency, GraphQL over HTTP Protocol compliant server and client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=12"
|
|
26
26
|
},
|
|
27
|
-
"packageManager": "yarn@3.
|
|
27
|
+
"packageManager": "yarn@3.3.1",
|
|
28
28
|
"main": "lib/index.js",
|
|
29
29
|
"module": "lib/index.mjs",
|
|
30
30
|
"browser": "umd/graphql-http.js",
|
|
@@ -55,6 +55,11 @@
|
|
|
55
55
|
"require": "./lib/use/fastify.js",
|
|
56
56
|
"import": "./lib/use/fastify.mjs"
|
|
57
57
|
},
|
|
58
|
+
"./lib/use/koa": {
|
|
59
|
+
"types": "./lib/use/koa.d.ts",
|
|
60
|
+
"require": "./lib/use/koa.js",
|
|
61
|
+
"import": "./lib/use/koa.mjs"
|
|
62
|
+
},
|
|
58
63
|
"./package.json": "./package.json"
|
|
59
64
|
},
|
|
60
65
|
"types": "lib/index.d.ts",
|
|
@@ -72,12 +77,14 @@
|
|
|
72
77
|
},
|
|
73
78
|
"scripts": {
|
|
74
79
|
"gendocs": "typedoc --options typedoc.js src/",
|
|
75
|
-
"lint": "eslint 'src'",
|
|
80
|
+
"lint:eslint": "eslint 'src'",
|
|
81
|
+
"lint:prettier": "prettier -c .",
|
|
82
|
+
"lint": "yarn lint:eslint && yarn lint:prettier",
|
|
76
83
|
"type-check": "tsc --noEmit",
|
|
77
84
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
78
85
|
"build:esm": "tsc -b tsconfig.esm.json && node scripts/esm-post-process.mjs",
|
|
79
86
|
"build:cjs": "tsc -b tsconfig.cjs.json",
|
|
80
|
-
"build:umd": "rollup
|
|
87
|
+
"build:umd": "rollup --bundleConfigAsCjs --config rollup.config.ts --configPlugin typescript && gzip umd/graphql-http.min.js -c > umd/graphql-http.min.js.gz",
|
|
81
88
|
"build": "yarn build:esm && yarn build:cjs && yarn build:umd",
|
|
82
89
|
"release": "semantic-release"
|
|
83
90
|
},
|
|
@@ -88,39 +95,44 @@
|
|
|
88
95
|
"graphql": ">=0.11 <=16"
|
|
89
96
|
},
|
|
90
97
|
"devDependencies": {
|
|
91
|
-
"@babel/core": "^7.20.
|
|
98
|
+
"@babel/core": "^7.20.7",
|
|
92
99
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
93
100
|
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
94
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.20.
|
|
95
|
-
"@babel/plugin-proposal-optional-chaining": "^7.
|
|
101
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
|
|
102
|
+
"@babel/plugin-proposal-optional-chaining": "^7.20.7",
|
|
96
103
|
"@babel/preset-env": "^7.20.2",
|
|
97
104
|
"@babel/preset-typescript": "^7.18.6",
|
|
98
|
-
"@rollup/plugin-
|
|
99
|
-
"@
|
|
105
|
+
"@rollup/plugin-terser": "^0.2.1",
|
|
106
|
+
"@rollup/plugin-typescript": "^10.0.1",
|
|
107
|
+
"@semantic-release/changelog": "^6.0.2",
|
|
100
108
|
"@semantic-release/git": "^10.0.1",
|
|
101
|
-
"@types/
|
|
102
|
-
"@types/
|
|
103
|
-
"@
|
|
104
|
-
"@
|
|
105
|
-
"@
|
|
106
|
-
"
|
|
107
|
-
"eslint": "^
|
|
109
|
+
"@types/eslint": "^8.4.10",
|
|
110
|
+
"@types/express": "^4.17.15",
|
|
111
|
+
"@types/glob": "^8.0.0",
|
|
112
|
+
"@types/jest": "^29.2.4",
|
|
113
|
+
"@types/koa": "^2.13.5",
|
|
114
|
+
"@types/koa-mount": "^4.0.2",
|
|
115
|
+
"@typescript-eslint/eslint-plugin": "^5.47.0",
|
|
116
|
+
"@typescript-eslint/parser": "^5.47.0",
|
|
117
|
+
"@whatwg-node/fetch": "^0.5.3",
|
|
118
|
+
"babel-jest": "^29.3.1",
|
|
119
|
+
"eslint": "^8.30.0",
|
|
108
120
|
"eslint-config-prettier": "^8.5.0",
|
|
109
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
110
121
|
"express": "^4.18.2",
|
|
111
|
-
"fastify": "^4.
|
|
122
|
+
"fastify": "^4.10.2",
|
|
112
123
|
"graphql": "^16.6.0",
|
|
113
|
-
"jest": "^29.3.
|
|
114
|
-
"jest-jasmine2": "^29.3.
|
|
115
|
-
"
|
|
116
|
-
"
|
|
117
|
-
"
|
|
118
|
-
"
|
|
124
|
+
"jest": "^29.3.1",
|
|
125
|
+
"jest-jasmine2": "^29.3.1",
|
|
126
|
+
"koa": "^2.14.1",
|
|
127
|
+
"koa-mount": "^4.0.0",
|
|
128
|
+
"node-fetch": "^3.3.0",
|
|
129
|
+
"prettier": "^2.8.1",
|
|
130
|
+
"rollup": "^3.8.1",
|
|
119
131
|
"semantic-release": "^19.0.5",
|
|
120
132
|
"tslib": "^2.4.1",
|
|
121
|
-
"typedoc": "^0.23.
|
|
122
|
-
"typedoc-plugin-markdown": "^3.
|
|
123
|
-
"typescript": "^4.
|
|
133
|
+
"typedoc": "^0.23.23",
|
|
134
|
+
"typedoc-plugin-markdown": "^3.14.0",
|
|
135
|
+
"typescript": "^4.9.4"
|
|
124
136
|
},
|
|
125
137
|
"resolutions": {
|
|
126
138
|
"npm/libnpmversion": "^3.0.6"
|
|
Binary file
|