graphql-http 1.18.0 → 1.19.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
@@ -158,6 +158,21 @@ app.listen({ port: 4000 });
158
158
  console.log('Listening to port 4000');
159
159
  ```
160
160
 
161
+ ##### With [`uWebSockets.js`](https://github.com/uNetworking/uWebSockets.js)
162
+
163
+ ```js
164
+ import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
165
+ import { createHandler } from 'graphql-http/lib/use/uWebSockets';
166
+ import { schema } from './previous-step';
167
+
168
+ uWS
169
+ .App()
170
+ .any('/graphql', createHandler({ schema }))
171
+ .listen(4000, () => {
172
+ console.log('Listening to port 4000');
173
+ });
174
+ ```
175
+
161
176
  ##### With [`Deno`](https://deno.land/)
162
177
 
163
178
  ```ts
@@ -768,7 +783,7 @@ Put the above contents in a file and run it with `deno test --allow-net`.
768
783
 
769
784
  ## Only [GraphQL over HTTP](https://graphql.github.io/graphql-over-http/)
770
785
 
771
- This is the official [GraphQL over HTTP spec](https://graphql.github.io/graphql-over-http/) reference implementation and as such follows the specification strictly without any additional features (like file uploads, @stream/@defer directives and subscriptions).
786
+ This is the official [GraphQL over HTTP spec](https://graphql.github.io/graphql-over-http/) reference implementation and as such follows the specification strictly without any additional features (like playgrounds or GUIs, file uploads, @stream/@defer directives and subscriptions).
772
787
 
773
788
  Having said this, graphql-http is mostly aimed for library authors and simple server setups, where the requirements are exact to what the aforementioned spec offers.
774
789
 
@@ -21,7 +21,6 @@ const handler_1 = require("../handler");
21
21
  * @category Server/express
22
22
  */
23
23
  function createHandler(options) {
24
- const isProd = process.env.NODE_ENV === 'production';
25
24
  const handle = (0, handler_1.createHandler)(options);
26
25
  return async function requestListener(req, res) {
27
26
  try {
@@ -50,23 +49,7 @@ function createHandler(options) {
50
49
  // If you wish to handle them differently, consider implementing your own request handler.
51
50
  console.error('Internal error occurred during request handling. ' +
52
51
  'Please check your implementation.', err);
53
- if (isProd) {
54
- res.writeHead(500).end();
55
- }
56
- else {
57
- res
58
- .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
59
- .end(JSON.stringify({
60
- errors: [
61
- err instanceof Error
62
- ? {
63
- message: err.message,
64
- stack: err.stack,
65
- }
66
- : err,
67
- ],
68
- }));
69
- }
52
+ res.writeHead(500).end();
70
53
  }
71
54
  };
72
55
  }
@@ -18,7 +18,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
18
18
  * @category Server/express
19
19
  */
20
20
  export function createHandler(options) {
21
- const isProd = process.env.NODE_ENV === 'production';
22
21
  const handle = createRawHandler(options);
23
22
  return async function requestListener(req, res) {
24
23
  try {
@@ -47,23 +46,7 @@ export function createHandler(options) {
47
46
  // If you wish to handle them differently, consider implementing your own request handler.
48
47
  console.error('Internal error occurred during request handling. ' +
49
48
  'Please check your implementation.', err);
50
- if (isProd) {
51
- res.writeHead(500).end();
52
- }
53
- else {
54
- res
55
- .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
56
- .end(JSON.stringify({
57
- errors: [
58
- err instanceof Error
59
- ? {
60
- message: err.message,
61
- stack: err.stack,
62
- }
63
- : err,
64
- ],
65
- }));
66
- }
49
+ res.writeHead(500).end();
67
50
  }
68
51
  };
69
52
  }
@@ -21,7 +21,6 @@ const handler_1 = require("../handler");
21
21
  * @category Server/fastify
22
22
  */
23
23
  function createHandler(options) {
24
- const isProd = process.env.NODE_ENV === 'production';
25
24
  const handle = (0, handler_1.createHandler)(options);
26
25
  return async function requestListener(req, reply) {
27
26
  try {
@@ -45,24 +44,7 @@ function createHandler(options) {
45
44
  // If you wish to handle them differently, consider implementing your own request handler.
46
45
  console.error('Internal error occurred during request handling. ' +
47
46
  'Please check your implementation.', err);
48
- if (isProd) {
49
- reply.status(500).send();
50
- }
51
- else {
52
- reply
53
- .status(500)
54
- .header('content-type', 'application/json; charset=utf-8')
55
- .send({
56
- errors: [
57
- err instanceof Error
58
- ? {
59
- message: err.message,
60
- stack: err.stack,
61
- }
62
- : err,
63
- ],
64
- });
65
- }
47
+ reply.status(500).send();
66
48
  }
67
49
  };
68
50
  }
@@ -18,7 +18,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
18
18
  * @category Server/fastify
19
19
  */
20
20
  export function createHandler(options) {
21
- const isProd = process.env.NODE_ENV === 'production';
22
21
  const handle = createRawHandler(options);
23
22
  return async function requestListener(req, reply) {
24
23
  try {
@@ -42,24 +41,7 @@ export function createHandler(options) {
42
41
  // If you wish to handle them differently, consider implementing your own request handler.
43
42
  console.error('Internal error occurred during request handling. ' +
44
43
  'Please check your implementation.', err);
45
- if (isProd) {
46
- reply.status(500).send();
47
- }
48
- else {
49
- reply
50
- .status(500)
51
- .header('content-type', 'application/json; charset=utf-8')
52
- .send({
53
- errors: [
54
- err instanceof Error
55
- ? {
56
- message: err.message,
57
- stack: err.stack,
58
- }
59
- : err,
60
- ],
61
- });
62
- }
44
+ reply.status(500).send();
63
45
  }
64
46
  };
65
47
  }
@@ -26,7 +26,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
26
26
  * import http from 'http';
27
27
  * import { createServerAdapter } from '@whatwg-node/server'; // yarn add @whatwg-node/server
28
28
  * import { createHandler } from 'graphql-http/lib/use/fetch';
29
- * import { schema } from './my-graphql-step/index.mjs';
29
+ * import { schema } from './my-graphql-schema/index.mjs';
30
30
  *
31
31
  * // Use this adapter in _any_ environment.
32
32
  * const adapter = createServerAdapter({
@@ -26,7 +26,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
26
26
  * import http from 'http';
27
27
  * import { createServerAdapter } from '@whatwg-node/server'; // yarn add @whatwg-node/server
28
28
  * import { createHandler } from 'graphql-http/lib/use/fetch';
29
- * import { schema } from './my-graphql-step';
29
+ * import { schema } from './my-graphql-schema';
30
30
  *
31
31
  * // Use this adapter in _any_ environment.
32
32
  * const adapter = createServerAdapter({
package/lib/use/fetch.js CHANGED
@@ -13,7 +13,7 @@ const handler_1 = require("../handler");
13
13
  * import http from 'http';
14
14
  * import { createServerAdapter } from '@whatwg-node/server'; // yarn add @whatwg-node/server
15
15
  * import { createHandler } from 'graphql-http/lib/use/fetch';
16
- * import { schema } from './my-graphql-step';
16
+ * import { schema } from './my-graphql-schema';
17
17
  *
18
18
  * // Use this adapter in _any_ environment.
19
19
  * const adapter = createServerAdapter({
@@ -31,7 +31,6 @@ const handler_1 = require("../handler");
31
31
  * @category Server/fetch
32
32
  */
33
33
  function createHandler(options, reqCtx = {}) {
34
- const isProd = process.env.NODE_ENV === 'production';
35
34
  const api = {
36
35
  Response: reqCtx.Response || Response,
37
36
  TextEncoder: reqCtx.TextEncoder || TextEncoder,
@@ -55,26 +54,7 @@ function createHandler(options, reqCtx = {}) {
55
54
  // If you wish to handle them differently, consider implementing your own request handler.
56
55
  console.error('Internal error occurred during request handling. ' +
57
56
  'Please check your implementation.', err);
58
- if (isProd) {
59
- return new api.Response(null, { status: 500 });
60
- }
61
- else {
62
- return new api.Response(JSON.stringify({
63
- errors: [
64
- err instanceof Error
65
- ? {
66
- message: err.message,
67
- stack: err.stack,
68
- }
69
- : err,
70
- ],
71
- }), {
72
- status: 500,
73
- headers: {
74
- 'content-type': 'application/json; charset=utf-8',
75
- },
76
- });
77
- }
57
+ return new api.Response(null, { status: 500 });
78
58
  }
79
59
  };
80
60
  }
package/lib/use/fetch.mjs CHANGED
@@ -10,7 +10,7 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
10
10
  * import http from 'http';
11
11
  * import { createServerAdapter } from '@whatwg-node/server'; // yarn add @whatwg-node/server
12
12
  * import { createHandler } from 'graphql-http/lib/use/fetch';
13
- * import { schema } from './my-graphql-step/index.mjs';
13
+ * import { schema } from './my-graphql-schema/index.mjs';
14
14
  *
15
15
  * // Use this adapter in _any_ environment.
16
16
  * const adapter = createServerAdapter({
@@ -28,7 +28,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
28
28
  * @category Server/fetch
29
29
  */
30
30
  export function createHandler(options, reqCtx = {}) {
31
- const isProd = process.env.NODE_ENV === 'production';
32
31
  const api = {
33
32
  Response: reqCtx.Response || Response,
34
33
  TextEncoder: reqCtx.TextEncoder || TextEncoder,
@@ -52,26 +51,7 @@ export function createHandler(options, reqCtx = {}) {
52
51
  // If you wish to handle them differently, consider implementing your own request handler.
53
52
  console.error('Internal error occurred during request handling. ' +
54
53
  'Please check your implementation.', err);
55
- if (isProd) {
56
- return new api.Response(null, { status: 500 });
57
- }
58
- else {
59
- return new api.Response(JSON.stringify({
60
- errors: [
61
- err instanceof Error
62
- ? {
63
- message: err.message,
64
- stack: err.stack,
65
- }
66
- : err,
67
- ],
68
- }), {
69
- status: 500,
70
- headers: {
71
- 'content-type': 'application/json; charset=utf-8',
72
- },
73
- });
74
- }
54
+ return new api.Response(null, { status: 500 });
75
55
  }
76
56
  };
77
57
  }
@@ -22,7 +22,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
22
22
  * ```js
23
23
  * import http from 'http';
24
24
  * import { createHandler } from 'graphql-http/lib/use/http';
25
- * import { schema } from './my-graphql-step/index.mjs';
25
+ * import { schema } from './my-graphql-schema/index.mjs';
26
26
  *
27
27
  * const server = http.createServer(createHandler({ schema }));
28
28
  *
package/lib/use/http.d.ts CHANGED
@@ -22,7 +22,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
22
22
  * ```js
23
23
  * import http from 'http';
24
24
  * import { createHandler } from 'graphql-http/lib/use/http';
25
- * import { schema } from './my-graphql-step';
25
+ * import { schema } from './my-graphql-schema';
26
26
  *
27
27
  * const server = http.createServer(createHandler({ schema }));
28
28
  *
package/lib/use/http.js CHANGED
@@ -9,7 +9,7 @@ const handler_1 = require("../handler");
9
9
  * ```js
10
10
  * import http from 'http';
11
11
  * import { createHandler } from 'graphql-http/lib/use/http';
12
- * import { schema } from './my-graphql-step';
12
+ * import { schema } from './my-graphql-schema';
13
13
  *
14
14
  * const server = http.createServer(createHandler({ schema }));
15
15
  *
@@ -20,7 +20,6 @@ const handler_1 = require("../handler");
20
20
  * @category Server/http
21
21
  */
22
22
  function createHandler(options) {
23
- const isProd = process.env.NODE_ENV === 'production';
24
23
  const handle = (0, handler_1.createHandler)(options);
25
24
  return async function requestListener(req, res) {
26
25
  try {
@@ -49,23 +48,7 @@ function createHandler(options) {
49
48
  // If you wish to handle them differently, consider implementing your own request handler.
50
49
  console.error('Internal error occurred during request handling. ' +
51
50
  '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
- }
51
+ res.writeHead(500).end();
69
52
  }
70
53
  };
71
54
  }
package/lib/use/http.mjs CHANGED
@@ -6,7 +6,7 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
6
6
  * ```js
7
7
  * import http from 'http';
8
8
  * import { createHandler } from 'graphql-http/lib/use/http';
9
- * import { schema } from './my-graphql-step/index.mjs';
9
+ * import { schema } from './my-graphql-schema/index.mjs';
10
10
  *
11
11
  * const server = http.createServer(createHandler({ schema }));
12
12
  *
@@ -17,7 +17,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
17
17
  * @category Server/http
18
18
  */
19
19
  export function createHandler(options) {
20
- const isProd = process.env.NODE_ENV === 'production';
21
20
  const handle = createRawHandler(options);
22
21
  return async function requestListener(req, res) {
23
22
  try {
@@ -46,23 +45,7 @@ export function createHandler(options) {
46
45
  // If you wish to handle them differently, consider implementing your own request handler.
47
46
  console.error('Internal error occurred during request handling. ' +
48
47
  '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
- }
48
+ res.writeHead(500).end();
66
49
  }
67
50
  };
68
51
  }
@@ -28,7 +28,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
28
28
  * import fs from 'fs';
29
29
  * import http2 from 'http2';
30
30
  * import { createHandler } from 'graphql-http/lib/use/http2';
31
- * import { schema } from './my-graphql-step/index.mjs';
31
+ * import { schema } from './my-graphql-schema/index.mjs';
32
32
  *
33
33
  * const server = http2.createSecureServer(
34
34
  * {
@@ -28,7 +28,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = RawHa
28
28
  * import fs from 'fs';
29
29
  * import http2 from 'http2';
30
30
  * import { createHandler } from 'graphql-http/lib/use/http2';
31
- * import { schema } from './my-graphql-step';
31
+ * import { schema } from './my-graphql-schema';
32
32
  *
33
33
  * const server = http2.createSecureServer(
34
34
  * {
package/lib/use/http2.js CHANGED
@@ -15,7 +15,7 @@ const handler_1 = require("../handler");
15
15
  * import fs from 'fs';
16
16
  * import http2 from 'http2';
17
17
  * import { createHandler } from 'graphql-http/lib/use/http2';
18
- * import { schema } from './my-graphql-step';
18
+ * import { schema } from './my-graphql-schema';
19
19
  *
20
20
  * const server = http2.createSecureServer(
21
21
  * {
@@ -32,7 +32,6 @@ const handler_1 = require("../handler");
32
32
  * @category Server/http2
33
33
  */
34
34
  function createHandler(options) {
35
- const isProd = process.env.NODE_ENV === 'production';
36
35
  const handle = (0, handler_1.createHandler)(options);
37
36
  return async function requestListener(req, res) {
38
37
  try {
@@ -67,23 +66,7 @@ function createHandler(options) {
67
66
  // If you wish to handle them differently, consider implementing your own request handler.
68
67
  console.error('Internal error occurred during request handling. ' +
69
68
  '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
- }
69
+ res.writeHead(500).end();
87
70
  }
88
71
  };
89
72
  }
package/lib/use/http2.mjs CHANGED
@@ -12,7 +12,7 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
12
12
  * import fs from 'fs';
13
13
  * import http2 from 'http2';
14
14
  * import { createHandler } from 'graphql-http/lib/use/http2';
15
- * import { schema } from './my-graphql-step/index.mjs';
15
+ * import { schema } from './my-graphql-schema/index.mjs';
16
16
  *
17
17
  * const server = http2.createSecureServer(
18
18
  * {
@@ -29,7 +29,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
29
29
  * @category Server/http2
30
30
  */
31
31
  export function createHandler(options) {
32
- const isProd = process.env.NODE_ENV === 'production';
33
32
  const handle = createRawHandler(options);
34
33
  return async function requestListener(req, res) {
35
34
  try {
@@ -64,23 +63,7 @@ export function createHandler(options) {
64
63
  // If you wish to handle them differently, consider implementing your own request handler.
65
64
  console.error('Internal error occurred during request handling. ' +
66
65
  '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
- }
66
+ res.writeHead(500).end();
84
67
  }
85
68
  };
86
69
  }
package/lib/use/koa.js CHANGED
@@ -22,7 +22,6 @@ const handler_1 = require("../handler");
22
22
  * @category Server/koa
23
23
  */
24
24
  function createHandler(options) {
25
- const isProd = process.env.NODE_ENV === 'production';
26
25
  const handle = (0, handler_1.createHandler)(options);
27
26
  return async function requestListener(ctx) {
28
27
  try {
@@ -59,19 +58,6 @@ function createHandler(options) {
59
58
  console.error('Internal error occurred during request handling. ' +
60
59
  'Please check your implementation.', err);
61
60
  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
61
  }
76
62
  };
77
63
  }
package/lib/use/koa.mjs CHANGED
@@ -19,7 +19,6 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
19
19
  * @category Server/koa
20
20
  */
21
21
  export function createHandler(options) {
22
- const isProd = process.env.NODE_ENV === 'production';
23
22
  const handle = createRawHandler(options);
24
23
  return async function requestListener(ctx) {
25
24
  try {
@@ -56,19 +55,6 @@ export function createHandler(options) {
56
55
  console.error('Internal error occurred during request handling. ' +
57
56
  'Please check your implementation.', err);
58
57
  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
58
  }
73
59
  };
74
60
  }
@@ -16,7 +16,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = HttpH
16
16
  * ```js
17
17
  * import http from 'http';
18
18
  * import { createHandler } from 'graphql-http/lib/use/node';
19
- * import { schema } from './my-graphql-step/index.mjs';
19
+ * import { schema } from './my-graphql-schema/index.mjs';
20
20
  *
21
21
  * const server = http.createServer(createHandler({ schema }));
22
22
  *
package/lib/use/node.d.ts CHANGED
@@ -16,7 +16,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = HttpH
16
16
  * ```js
17
17
  * import http from 'http';
18
18
  * import { createHandler } from 'graphql-http/lib/use/node';
19
- * import { schema } from './my-graphql-step';
19
+ * import { schema } from './my-graphql-schema';
20
20
  *
21
21
  * const server = http.createServer(createHandler({ schema }));
22
22
  *
package/lib/use/node.js CHANGED
@@ -9,7 +9,7 @@ const http_1 = require("./http");
9
9
  * ```js
10
10
  * import http from 'http';
11
11
  * import { createHandler } from 'graphql-http/lib/use/node';
12
- * import { schema } from './my-graphql-step';
12
+ * import { schema } from './my-graphql-schema';
13
13
  *
14
14
  * const server = http.createServer(createHandler({ schema }));
15
15
  *
package/lib/use/node.mjs CHANGED
@@ -6,7 +6,7 @@ import { createHandler as httpCreateHandler, } from './http.mjs';
6
6
  * ```js
7
7
  * import http from 'http';
8
8
  * import { createHandler } from 'graphql-http/lib/use/node';
9
- * import { schema } from './my-graphql-step/index.mjs';
9
+ * import { schema } from './my-graphql-schema/index.mjs';
10
10
  *
11
11
  * const server = http.createServer(createHandler({ schema }));
12
12
  *
@@ -0,0 +1,36 @@
1
+ import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler.mjs';
3
+ /**
4
+ * The context in the request for the handler.
5
+ *
6
+ * @category Server/uWebSockets
7
+ */
8
+ export interface RequestContext {
9
+ res: HttpResponse;
10
+ }
11
+ /**
12
+ * Handler options when using the http adapter.
13
+ *
14
+ * @category Server/uWebSockets
15
+ */
16
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<HttpRequest, RequestContext, Context>;
17
+ /**
18
+ * Create a GraphQL over HTTP spec compliant request handler for
19
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
20
+ *
21
+ * ```js
22
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
23
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
24
+ * import { schema } from './my-graphql-schema/index.mjs';
25
+ *
26
+ * uWS
27
+ * .App()
28
+ * .any('/graphql', createHandler({ schema }))
29
+ * .listen(4000, () => {
30
+ * console.log('Listening to port 4000');
31
+ * });
32
+ * ```
33
+ *
34
+ * @category Server/uWebSockets
35
+ */
36
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (res: HttpResponse, req: HttpRequest) => Promise<void>;
@@ -0,0 +1,36 @@
1
+ import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
+ /**
4
+ * The context in the request for the handler.
5
+ *
6
+ * @category Server/uWebSockets
7
+ */
8
+ export interface RequestContext {
9
+ res: HttpResponse;
10
+ }
11
+ /**
12
+ * Handler options when using the http adapter.
13
+ *
14
+ * @category Server/uWebSockets
15
+ */
16
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<HttpRequest, RequestContext, Context>;
17
+ /**
18
+ * Create a GraphQL over HTTP spec compliant request handler for
19
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
20
+ *
21
+ * ```js
22
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
23
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
24
+ * import { schema } from './my-graphql-schema';
25
+ *
26
+ * uWS
27
+ * .App()
28
+ * .any('/graphql', createHandler({ schema }))
29
+ * .listen(4000, () => {
30
+ * console.log('Listening to port 4000');
31
+ * });
32
+ * ```
33
+ *
34
+ * @category Server/uWebSockets
35
+ */
36
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (res: HttpResponse, req: HttpRequest) => Promise<void>;
@@ -0,0 +1,84 @@
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 [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
8
+ *
9
+ * ```js
10
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
11
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
12
+ * import { schema } from './my-graphql-schema';
13
+ *
14
+ * uWS
15
+ * .App()
16
+ * .any('/graphql', createHandler({ schema }))
17
+ * .listen(4000, () => {
18
+ * console.log('Listening to port 4000');
19
+ * });
20
+ * ```
21
+ *
22
+ * @category Server/uWebSockets
23
+ */
24
+ function createHandler(options) {
25
+ const handle = (0, handler_1.createHandler)(options);
26
+ return async function requestListener(res, req) {
27
+ let aborted = false;
28
+ res.onAborted(() => (aborted = true));
29
+ try {
30
+ let url = req.getUrl();
31
+ const query = req.getQuery();
32
+ if (query) {
33
+ url += '?' + query;
34
+ }
35
+ const [body, init] = await handle({
36
+ url,
37
+ method: req.getMethod().toUpperCase(),
38
+ headers: { get: (key) => req.getHeader(key) },
39
+ body: () => new Promise((resolve) => {
40
+ let body = '';
41
+ if (aborted) {
42
+ resolve(body);
43
+ }
44
+ else {
45
+ res.onData((chunk, isLast) => {
46
+ body += Buffer.from(chunk, 0, chunk.byteLength).toString();
47
+ if (isLast) {
48
+ resolve(body);
49
+ }
50
+ });
51
+ }
52
+ }),
53
+ raw: req,
54
+ context: { res },
55
+ });
56
+ if (!aborted) {
57
+ res.cork(() => {
58
+ res.writeStatus(`${init.status} ${init.statusText}`);
59
+ for (const [key, val] of Object.entries(init.headers || {})) {
60
+ res.writeHeader(key, val);
61
+ }
62
+ if (body) {
63
+ res.end(body);
64
+ }
65
+ else {
66
+ res.endWithoutBody();
67
+ }
68
+ });
69
+ }
70
+ }
71
+ catch (err) {
72
+ // The handler shouldnt throw errors.
73
+ // If you wish to handle them differently, consider implementing your own request handler.
74
+ console.error('Internal error occurred during request handling. ' +
75
+ 'Please check your implementation.', err);
76
+ if (!aborted) {
77
+ res.cork(() => {
78
+ res.writeStatus('500 Internal Server Error').endWithoutBody();
79
+ });
80
+ }
81
+ }
82
+ };
83
+ }
84
+ exports.createHandler = createHandler;
@@ -0,0 +1,80 @@
1
+ import { createHandler as createRawHandler, } from '../handler.mjs';
2
+ /**
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
5
+ *
6
+ * ```js
7
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
8
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
9
+ * import { schema } from './my-graphql-schema/index.mjs';
10
+ *
11
+ * uWS
12
+ * .App()
13
+ * .any('/graphql', createHandler({ schema }))
14
+ * .listen(4000, () => {
15
+ * console.log('Listening to port 4000');
16
+ * });
17
+ * ```
18
+ *
19
+ * @category Server/uWebSockets
20
+ */
21
+ export function createHandler(options) {
22
+ const handle = createRawHandler(options);
23
+ return async function requestListener(res, req) {
24
+ let aborted = false;
25
+ res.onAborted(() => (aborted = true));
26
+ try {
27
+ let url = req.getUrl();
28
+ const query = req.getQuery();
29
+ if (query) {
30
+ url += '?' + query;
31
+ }
32
+ const [body, init] = await handle({
33
+ url,
34
+ method: req.getMethod().toUpperCase(),
35
+ headers: { get: (key) => req.getHeader(key) },
36
+ body: () => new Promise((resolve) => {
37
+ let body = '';
38
+ if (aborted) {
39
+ resolve(body);
40
+ }
41
+ else {
42
+ res.onData((chunk, isLast) => {
43
+ body += Buffer.from(chunk, 0, chunk.byteLength).toString();
44
+ if (isLast) {
45
+ resolve(body);
46
+ }
47
+ });
48
+ }
49
+ }),
50
+ raw: req,
51
+ context: { res },
52
+ });
53
+ if (!aborted) {
54
+ res.cork(() => {
55
+ res.writeStatus(`${init.status} ${init.statusText}`);
56
+ for (const [key, val] of Object.entries(init.headers || {})) {
57
+ res.writeHeader(key, val);
58
+ }
59
+ if (body) {
60
+ res.end(body);
61
+ }
62
+ else {
63
+ res.endWithoutBody();
64
+ }
65
+ });
66
+ }
67
+ }
68
+ catch (err) {
69
+ // The handler shouldnt throw errors.
70
+ // If you wish to handle them differently, consider implementing your own request handler.
71
+ console.error('Internal error occurred during request handling. ' +
72
+ 'Please check your implementation.', err);
73
+ if (!aborted) {
74
+ res.cork(() => {
75
+ res.writeStatus('500 Internal Server Error').endWithoutBody();
76
+ });
77
+ }
78
+ }
79
+ };
80
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-http",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "Simple, pluggable, zero-dependency, GraphQL over HTTP spec compliant server, client and audit suite.",
5
5
  "keywords": [
6
6
  "graphql",
@@ -24,7 +24,7 @@
24
24
  "engines": {
25
25
  "node": ">=12"
26
26
  },
27
- "packageManager": "yarn@3.5.0",
27
+ "packageManager": "yarn@3.6.0",
28
28
  "main": "lib/index.js",
29
29
  "module": "lib/index.mjs",
30
30
  "browser": "umd/graphql-http.js",
@@ -70,6 +70,11 @@
70
70
  "require": "./lib/use/koa.js",
71
71
  "import": "./lib/use/koa.mjs"
72
72
  },
73
+ "./lib/use/uWebSockets": {
74
+ "types": "./lib/use/uWebSockets.d.ts",
75
+ "require": "./lib/use/uWebSockets.js",
76
+ "import": "./lib/use/uWebSockets.mjs"
77
+ },
73
78
  "./package.json": "./package.json"
74
79
  },
75
80
  "types": "lib/index.d.ts",
@@ -106,33 +111,34 @@
106
111
  "graphql": ">=0.11 <=16"
107
112
  },
108
113
  "devDependencies": {
109
- "@babel/core": "^7.21.4",
114
+ "@babel/core": "^7.22.1",
110
115
  "@babel/plugin-proposal-class-properties": "^7.18.6",
111
116
  "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
112
117
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
113
118
  "@babel/plugin-proposal-optional-chaining": "^7.21.0",
114
- "@babel/preset-env": "^7.21.4",
115
- "@babel/preset-typescript": "^7.21.4",
116
- "@rollup/plugin-terser": "^0.4.1",
117
- "@rollup/plugin-typescript": "^11.1.0",
119
+ "@babel/preset-env": "^7.22.4",
120
+ "@babel/preset-typescript": "^7.21.5",
121
+ "@rollup/plugin-terser": "^0.4.3",
122
+ "@rollup/plugin-typescript": "^11.1.1",
118
123
  "@semantic-release/changelog": "^6.0.3",
119
124
  "@semantic-release/git": "^10.0.1",
120
- "@types/eslint": "^8.37.0",
125
+ "@types/eslint": "^8.40.0",
121
126
  "@types/express": "^4.17.17",
122
127
  "@types/glob": "^8.1.0",
123
128
  "@types/html-validator": "^5.0.3",
124
- "@types/jest": "^29.5.0",
129
+ "@types/jest": "^29.5.2",
130
+ "@types/k6": "^0.44.2",
125
131
  "@types/koa": "^2.13.6",
126
132
  "@types/koa-mount": "^4.0.2",
127
- "@typescript-eslint/eslint-plugin": "^5.58.0",
128
- "@typescript-eslint/parser": "^5.58.0",
129
- "@whatwg-node/fetch": "^0.8.4",
133
+ "@typescript-eslint/eslint-plugin": "^5.59.8",
134
+ "@typescript-eslint/parser": "^5.59.8",
135
+ "@whatwg-node/fetch": "^0.9.2",
130
136
  "babel-jest": "^29.5.0",
131
- "eslint": "^8.38.0",
137
+ "eslint": "^8.42.0",
132
138
  "eslint-config-prettier": "^8.8.0",
133
139
  "express": "^4.18.2",
134
- "fastify": "^4.15.0",
135
- "glob": "^10.0.0",
140
+ "fastify": "^4.17.0",
141
+ "glob": "^10.2.6",
136
142
  "graphql": "^16.6.0",
137
143
  "html-validator": "^6.0.1",
138
144
  "jest": "^29.5.0",
@@ -140,16 +146,14 @@
140
146
  "koa": "^2.14.2",
141
147
  "koa-mount": "^4.0.0",
142
148
  "node-fetch": "^3.3.1",
143
- "prettier": "^2.8.7",
144
- "rollup": "^3.20.2",
149
+ "prettier": "^2.8.8",
150
+ "rollup": "^3.23.1",
145
151
  "rollup-plugin-gzip": "^3.1.0",
146
- "semantic-release": "^21.0.1",
147
- "tslib": "^2.5.0",
148
- "typedoc": "^0.24.1",
149
- "typedoc-plugin-markdown": "^3.15.1",
150
- "typescript": "^5.0.4"
151
- },
152
- "resolutions": {
153
- "npm/libnpmversion": "^3.0.6"
152
+ "semantic-release": "^21.0.3",
153
+ "tslib": "^2.5.3",
154
+ "typedoc": "^0.24.8",
155
+ "typedoc-plugin-markdown": "^3.15.3",
156
+ "typescript": "^5.1.3",
157
+ "uWebSockets.js": "uNetworking/uWebSockets.js#v20.30.0"
154
158
  }
155
159
  }