graphql-http 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -55,7 +55,7 @@ const schema = new GraphQLSchema({
55
55
 
56
56
  ```js
57
57
  import http from 'http';
58
- import { createHandler } from 'graphql-http/lib/use/node';
58
+ import { createHandler } from 'graphql-http/lib/use/http';
59
59
  import { schema } from './previous-step';
60
60
 
61
61
  // Create the GraphQL over HTTP Node request handler
@@ -86,7 +86,7 @@ $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
86
86
  ```js
87
87
  import fs from 'fs';
88
88
  import http2 from 'http2';
89
- import { createHandler } from 'graphql-http/lib/use/node';
89
+ import { createHandler } from 'graphql-http/lib/use/http2';
90
90
  import { schema } from './previous-step';
91
91
 
92
92
  // Create the GraphQL over HTTP Node request handler
@@ -114,22 +114,25 @@ function ressert(res) {
114
114
  bodyAsExecutionResult: {
115
115
  data: {
116
116
  async toBe(val) {
117
+ const clonedRes = res.clone(); // allow the body to be re-read
117
118
  const body = await assertBodyAsExecutionResult(res);
118
119
  if (body.data !== val) {
119
- throw new AuditError(res, `Response body execution result data is not "${val}"`);
120
+ throw new AuditError(clonedRes, `Response body execution result data is not "${val}"`);
120
121
  }
121
122
  },
122
123
  },
123
124
  async toHaveProperty(key) {
125
+ const clonedRes = res.clone(); // allow the body to be re-read
124
126
  const body = await assertBodyAsExecutionResult(res);
125
127
  if (!(key in body)) {
126
- throw new AuditError(res, `Response body execution result does not have a property "${key}"`);
128
+ throw new AuditError(clonedRes, `Response body execution result does not have a property "${key}"`);
127
129
  }
128
130
  },
129
131
  async notToHaveProperty(key) {
132
+ const clonedRes = res.clone(); // allow the body to be re-read
130
133
  const body = await assertBodyAsExecutionResult(res);
131
134
  if (key in body) {
132
- throw new AuditError(res, `Response body execution result has a property "${key}"`);
135
+ throw new AuditError(clonedRes, `Response body execution result has a property "${key}"`);
133
136
  }
134
137
  },
135
138
  },
@@ -95,22 +95,25 @@ export function ressert(res) {
95
95
  bodyAsExecutionResult: {
96
96
  data: {
97
97
  async toBe(val) {
98
+ const clonedRes = res.clone(); // allow the body to be re-read
98
99
  const body = await assertBodyAsExecutionResult(res);
99
100
  if (body.data !== val) {
100
- throw new AuditError(res, `Response body execution result data is not "${val}"`);
101
+ throw new AuditError(clonedRes, `Response body execution result data is not "${val}"`);
101
102
  }
102
103
  },
103
104
  },
104
105
  async toHaveProperty(key) {
106
+ const clonedRes = res.clone(); // allow the body to be re-read
105
107
  const body = await assertBodyAsExecutionResult(res);
106
108
  if (!(key in body)) {
107
- throw new AuditError(res, `Response body execution result does not have a property "${key}"`);
109
+ throw new AuditError(clonedRes, `Response body execution result does not have a property "${key}"`);
108
110
  }
109
111
  },
110
112
  async notToHaveProperty(key) {
113
+ const clonedRes = res.clone(); // allow the body to be re-read
111
114
  const body = await assertBodyAsExecutionResult(res);
112
115
  if (key in body) {
113
- throw new AuditError(res, `Response body execution result has a property "${key}"`);
116
+ throw new AuditError(clonedRes, `Response body execution result has a property "${key}"`);
114
117
  }
115
118
  },
116
119
  },
package/lib/handler.d.mts CHANGED
@@ -228,7 +228,7 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
228
228
  */
229
229
  export type Handler<RequestRaw = unknown, RequestContext = unknown> = (req: Request<RequestRaw, RequestContext>) => Promise<Response>;
230
230
  /**
231
- * Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
231
+ * Makes a GraphQL over HTTP spec compliant server handler. The handler can
232
232
  * be used with your favourite server library.
233
233
  *
234
234
  * Beware that the handler resolves only after the whole operation completes.
@@ -291,7 +291,7 @@ export declare function createHandler<RequestRaw = unknown, RequestContext = unk
291
291
  export type AcceptableMediaType = 'application/graphql-response+json' | 'application/json';
292
292
  /**
293
293
  * Inspects the request and detects the appropriate/acceptable Media-Type
294
- * looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
294
+ * looking at the `Accept` header while complying with the GraphQL over HTTP spec.
295
295
  *
296
296
  * @category Server
297
297
  */
@@ -302,7 +302,7 @@ export declare function getAcceptableMediaType(acceptHeader: string | null | und
302
302
  * If the first argument is an `ExecutionResult`, the operation will be treated as "successful".
303
303
  *
304
304
  * If the first argument is _any_ object without the `data` field, it will be treated as an error (as per the spec)
305
- * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP Protocol.
305
+ * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP spec.
306
306
  *
307
307
  * @category Server
308
308
  */
package/lib/handler.d.ts CHANGED
@@ -228,7 +228,7 @@ export interface HandlerOptions<RequestRaw = unknown, RequestContext = unknown,
228
228
  */
229
229
  export type Handler<RequestRaw = unknown, RequestContext = unknown> = (req: Request<RequestRaw, RequestContext>) => Promise<Response>;
230
230
  /**
231
- * Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
231
+ * Makes a GraphQL over HTTP spec compliant server handler. The handler can
232
232
  * be used with your favourite server library.
233
233
  *
234
234
  * Beware that the handler resolves only after the whole operation completes.
@@ -291,7 +291,7 @@ export declare function createHandler<RequestRaw = unknown, RequestContext = unk
291
291
  export type AcceptableMediaType = 'application/graphql-response+json' | 'application/json';
292
292
  /**
293
293
  * Inspects the request and detects the appropriate/acceptable Media-Type
294
- * looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
294
+ * looking at the `Accept` header while complying with the GraphQL over HTTP spec.
295
295
  *
296
296
  * @category Server
297
297
  */
@@ -302,7 +302,7 @@ export declare function getAcceptableMediaType(acceptHeader: string | null | und
302
302
  * If the first argument is an `ExecutionResult`, the operation will be treated as "successful".
303
303
  *
304
304
  * If the first argument is _any_ object without the `data` field, it will be treated as an error (as per the spec)
305
- * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP Protocol.
305
+ * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP spec.
306
306
  *
307
307
  * @category Server
308
308
  */
package/lib/handler.js CHANGED
@@ -21,7 +21,7 @@ function isResponse(val) {
21
21
  }
22
22
  exports.isResponse = isResponse;
23
23
  /**
24
- * Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
24
+ * Makes a GraphQL over HTTP spec compliant server handler. The handler can
25
25
  * be used with your favourite server library.
26
26
  *
27
27
  * Beware that the handler resolves only after the whole operation completes.
@@ -291,7 +291,7 @@ function createHandler(options) {
291
291
  exports.createHandler = createHandler;
292
292
  /**
293
293
  * Inspects the request and detects the appropriate/acceptable Media-Type
294
- * looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
294
+ * looking at the `Accept` header while complying with the GraphQL over HTTP spec.
295
295
  *
296
296
  * @category Server
297
297
  */
@@ -328,7 +328,7 @@ exports.getAcceptableMediaType = getAcceptableMediaType;
328
328
  * If the first argument is an `ExecutionResult`, the operation will be treated as "successful".
329
329
  *
330
330
  * If the first argument is _any_ object without the `data` field, it will be treated as an error (as per the spec)
331
- * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP Protocol.
331
+ * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP spec.
332
332
  *
333
333
  * @category Server
334
334
  */
package/lib/handler.mjs CHANGED
@@ -17,7 +17,7 @@ export function isResponse(val) {
17
17
  isObject(val[1]));
18
18
  }
19
19
  /**
20
- * Makes a GraphQL over HTTP Protocol compliant server handler. The handler can
20
+ * Makes a GraphQL over HTTP spec compliant server handler. The handler can
21
21
  * be used with your favourite server library.
22
22
  *
23
23
  * Beware that the handler resolves only after the whole operation completes.
@@ -286,7 +286,7 @@ export function createHandler(options) {
286
286
  }
287
287
  /**
288
288
  * Inspects the request and detects the appropriate/acceptable Media-Type
289
- * looking at the `Accept` header while complying with the GraphQL over HTTP Protocol.
289
+ * looking at the `Accept` header while complying with the GraphQL over HTTP spec.
290
290
  *
291
291
  * @category Server
292
292
  */
@@ -322,7 +322,7 @@ export function getAcceptableMediaType(acceptHeader) {
322
322
  * If the first argument is an `ExecutionResult`, the operation will be treated as "successful".
323
323
  *
324
324
  * If the first argument is _any_ object without the `data` field, it will be treated as an error (as per the spec)
325
- * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP Protocol.
325
+ * and the response will be constructed with the help of `acceptedMediaType` complying with the GraphQL over HTTP spec.
326
326
  *
327
327
  * @category Server
328
328
  */
@@ -1,7 +1,13 @@
1
1
  import type { Request, Handler } from 'express';
2
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
3
  /**
4
- * Create a GraphQL over HTTP Protocol compliant request handler for
4
+ * Handler options when using the express adapter.
5
+ *
6
+ * @category Server/express
7
+ */
8
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, undefined, Context>;
9
+ /**
10
+ * Create a GraphQL over HTTP spec compliant request handler for
5
11
  * the express framework.
6
12
  *
7
13
  * ```js
@@ -18,4 +24,4 @@ import { HandlerOptions, OperationContext } from '../handler';
18
24
  *
19
25
  * @category Server/express
20
26
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, undefined, Context>): Handler;
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): Handler;
@@ -1,7 +1,13 @@
1
1
  import type { Request, Handler } from 'express';
2
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
3
  /**
4
- * Create a GraphQL over HTTP Protocol compliant request handler for
4
+ * Handler options when using the express adapter.
5
+ *
6
+ * @category Server/express
7
+ */
8
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, undefined, Context>;
9
+ /**
10
+ * Create a GraphQL over HTTP spec compliant request handler for
5
11
  * the express framework.
6
12
  *
7
13
  * ```js
@@ -18,4 +24,4 @@ import { HandlerOptions, OperationContext } from '../handler';
18
24
  *
19
25
  * @category Server/express
20
26
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, undefined, Context>): Handler;
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): Handler;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
4
  const handler_1 = require("../handler");
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
7
  * the express framework.
8
8
  *
9
9
  * ```js
@@ -1,6 +1,6 @@
1
1
  import { createHandler as createRawHandler, } from '../handler.mjs';
2
2
  /**
3
- * Create a GraphQL over HTTP Protocol compliant request handler for
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
4
  * the express framework.
5
5
  *
6
6
  * ```js
@@ -1,7 +1,13 @@
1
1
  import type { FastifyRequest, RouteHandler } from 'fastify';
2
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
3
  /**
4
- * Create a GraphQL over HTTP Protocol compliant request handler for
4
+ * Handler options when using the fastify adapter.
5
+ *
6
+ * @category Server/fastify
7
+ */
8
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<FastifyRequest, undefined, Context>;
9
+ /**
10
+ * Create a GraphQL over HTTP spec compliant request handler for
5
11
  * the fastify framework.
6
12
  *
7
13
  * ```js
@@ -18,4 +24,4 @@ import { HandlerOptions, OperationContext } from '../handler';
18
24
  *
19
25
  * @category Server/fastify
20
26
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<FastifyRequest, undefined, Context>): RouteHandler;
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): RouteHandler;
@@ -1,7 +1,13 @@
1
1
  import type { FastifyRequest, RouteHandler } from 'fastify';
2
- import { HandlerOptions, OperationContext } from '../handler';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
3
  /**
4
- * Create a GraphQL over HTTP Protocol compliant request handler for
4
+ * Handler options when using the fastify adapter.
5
+ *
6
+ * @category Server/fastify
7
+ */
8
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<FastifyRequest, undefined, Context>;
9
+ /**
10
+ * Create a GraphQL over HTTP spec compliant request handler for
5
11
  * the fastify framework.
6
12
  *
7
13
  * ```js
@@ -18,4 +24,4 @@ import { HandlerOptions, OperationContext } from '../handler';
18
24
  *
19
25
  * @category Server/fastify
20
26
  */
21
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<FastifyRequest, undefined, Context>): RouteHandler;
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): RouteHandler;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
4
  const handler_1 = require("../handler");
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
7
  * the fastify framework.
8
8
  *
9
9
  * ```js
@@ -1,6 +1,6 @@
1
1
  import { createHandler as createRawHandler, } from '../handler.mjs';
2
2
  /**
3
- * Create a GraphQL over HTTP Protocol compliant request handler for
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
4
  * the fastify framework.
5
5
  *
6
6
  * ```js
@@ -1,4 +1,4 @@
1
- import { HandlerOptions, OperationContext } from '../handler';
1
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
2
2
  /**
3
3
  * The necessary API from the fetch environment for the handler.
4
4
  *
@@ -10,7 +10,13 @@ export interface FetchAPI {
10
10
  TextEncoder: typeof TextEncoder;
11
11
  }
12
12
  /**
13
- * Create a GraphQL over HTTP Protocol compliant request handler for
13
+ * Handler options when using the fetch adapter.
14
+ *
15
+ * @category Server/fetch
16
+ */
17
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, FetchAPI, Context>;
18
+ /**
19
+ * Create a GraphQL over HTTP spec compliant request handler for
14
20
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
15
21
  *
16
22
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -33,8 +39,8 @@ export interface FetchAPI {
33
39
  * console.log('Listening to port 4000');
34
40
  * ```
35
41
  *
36
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
42
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
37
43
  *
38
44
  * @category Server/fetch
39
45
  */
40
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, FetchAPI, Context>, fetchApi?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
46
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>, reqCtx?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
@@ -1,4 +1,4 @@
1
- import { HandlerOptions, OperationContext } from '../handler';
1
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
2
2
  /**
3
3
  * The necessary API from the fetch environment for the handler.
4
4
  *
@@ -10,7 +10,13 @@ export interface FetchAPI {
10
10
  TextEncoder: typeof TextEncoder;
11
11
  }
12
12
  /**
13
- * Create a GraphQL over HTTP Protocol compliant request handler for
13
+ * Handler options when using the fetch adapter.
14
+ *
15
+ * @category Server/fetch
16
+ */
17
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, FetchAPI, Context>;
18
+ /**
19
+ * Create a GraphQL over HTTP spec compliant request handler for
14
20
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
15
21
  *
16
22
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -33,8 +39,8 @@ export interface FetchAPI {
33
39
  * console.log('Listening to port 4000');
34
40
  * ```
35
41
  *
36
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
42
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
37
43
  *
38
44
  * @category Server/fetch
39
45
  */
40
- export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Request, FetchAPI, Context>, fetchApi?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
46
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>, reqCtx?: Partial<FetchAPI>): (req: Request) => Promise<Response>;
package/lib/use/fetch.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHandler = void 0;
4
4
  const handler_1 = require("../handler");
5
5
  /**
6
- * Create a GraphQL over HTTP Protocol compliant request handler for
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
7
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
8
8
  *
9
9
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -26,16 +26,16 @@ const handler_1 = require("../handler");
26
26
  * console.log('Listening to port 4000');
27
27
  * ```
28
28
  *
29
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
29
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
30
30
  *
31
31
  * @category Server/fetch
32
32
  */
33
- function createHandler(options, fetchApi = {}) {
33
+ function createHandler(options, reqCtx = {}) {
34
34
  const isProd = process.env.NODE_ENV === 'production';
35
35
  const api = {
36
- Response: fetchApi.Response || Response,
37
- TextEncoder: fetchApi.TextEncoder || TextEncoder,
38
- ReadableStream: fetchApi.ReadableStream || ReadableStream,
36
+ Response: reqCtx.Response || Response,
37
+ TextEncoder: reqCtx.TextEncoder || TextEncoder,
38
+ ReadableStream: reqCtx.ReadableStream || ReadableStream,
39
39
  };
40
40
  const handler = (0, handler_1.createHandler)(options);
41
41
  return async function handleRequest(req) {
package/lib/use/fetch.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHandler as createRawHandler, } from '../handler.mjs';
2
2
  /**
3
- * Create a GraphQL over HTTP Protocol compliant request handler for
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
4
  * a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
5
5
  *
6
6
  * You can use [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server) to create a server adapter and
@@ -23,16 +23,16 @@ import { createHandler as createRawHandler, } from '../handler.mjs';
23
23
  * console.log('Listening to port 4000');
24
24
  * ```
25
25
  *
26
- * @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
26
+ * @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
27
27
  *
28
28
  * @category Server/fetch
29
29
  */
30
- export function createHandler(options, fetchApi = {}) {
30
+ export function createHandler(options, reqCtx = {}) {
31
31
  const isProd = process.env.NODE_ENV === 'production';
32
32
  const api = {
33
- Response: fetchApi.Response || Response,
34
- TextEncoder: fetchApi.TextEncoder || TextEncoder,
35
- ReadableStream: fetchApi.ReadableStream || ReadableStream,
33
+ Response: reqCtx.Response || Response,
34
+ TextEncoder: reqCtx.TextEncoder || TextEncoder,
35
+ ReadableStream: reqCtx.ReadableStream || ReadableStream,
36
36
  };
37
37
  const handler = createRawHandler(options);
38
38
  return async function handleRequest(req) {
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage, ServerResponse } from 'http';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http module.
13
+ *
14
+ * ```js
15
+ * import http from 'http';
16
+ * import { createHandler } from 'graphql-http/lib/use/http';
17
+ * import { schema } from './my-graphql-step';
18
+ *
19
+ * const server = http.createServer(createHandler({ schema }));
20
+ *
21
+ * server.listen(4000);
22
+ * console.log('Listening to port 4000');
23
+ * ```
24
+ *
25
+ * @category Server/http
26
+ */
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage, ServerResponse } from 'http';
3
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ /**
5
+ * Handler options when using the http adapter.
6
+ *
7
+ * @category Server/http
8
+ */
9
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, undefined, Context>;
10
+ /**
11
+ * Create a GraphQL over HTTP spec compliant request handler for
12
+ * the Node environment http module.
13
+ *
14
+ * ```js
15
+ * import http from 'http';
16
+ * import { createHandler } from 'graphql-http/lib/use/http';
17
+ * import { schema } from './my-graphql-step';
18
+ *
19
+ * const server = http.createServer(createHandler({ schema }));
20
+ *
21
+ * server.listen(4000);
22
+ * console.log('Listening to port 4000');
23
+ * ```
24
+ *
25
+ * @category Server/http
26
+ */
27
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
@@ -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: 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
+ };
71
+ }
72
+ exports.createHandler = createHandler;
@@ -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: undefined,
41
+ });
42
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
43
+ }
44
+ catch (err) {
45
+ // The handler shouldnt throw errors.
46
+ // If you wish to handle them differently, consider implementing your own request handler.
47
+ console.error('Internal error occurred during request handling. ' +
48
+ 'Please check your implementation.', err);
49
+ if (isProd) {
50
+ res.writeHead(500).end();
51
+ }
52
+ else {
53
+ res
54
+ .writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
55
+ .end(JSON.stringify({
56
+ errors: [
57
+ err instanceof Error
58
+ ? {
59
+ message: err.message,
60
+ stack: err.stack,
61
+ }
62
+ : err,
63
+ ],
64
+ }));
65
+ }
66
+ }
67
+ };
68
+ }