graphql-http 1.10.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 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
 
@@ -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;
@@ -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;
@@ -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.10.0",
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",
@@ -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",
@@ -105,6 +110,8 @@
105
110
  "@types/express": "^4.17.15",
106
111
  "@types/glob": "^8.0.0",
107
112
  "@types/jest": "^29.2.4",
113
+ "@types/koa": "^2.13.5",
114
+ "@types/koa-mount": "^4.0.2",
108
115
  "@typescript-eslint/eslint-plugin": "^5.47.0",
109
116
  "@typescript-eslint/parser": "^5.47.0",
110
117
  "@whatwg-node/fetch": "^0.5.3",
@@ -116,6 +123,8 @@
116
123
  "graphql": "^16.6.0",
117
124
  "jest": "^29.3.1",
118
125
  "jest-jasmine2": "^29.3.1",
126
+ "koa": "^2.14.1",
127
+ "koa-mount": "^4.0.0",
119
128
  "node-fetch": "^3.3.0",
120
129
  "prettier": "^2.8.1",
121
130
  "rollup": "^3.8.1",
Binary file