graphql-http 1.10.0 → 1.12.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.
@@ -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/lib/utils.d.mts CHANGED
@@ -3,7 +3,8 @@
3
3
  * utils
4
4
  *
5
5
  */
6
- import type { ExecutionResult, GraphQLError } from 'graphql';
6
+ import type { ExecutionResult } from 'graphql';
7
+ import { GraphQLError } from 'graphql';
7
8
  /** @private */
8
9
  export declare function extendedTypeof(val: unknown): 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'array' | 'null';
9
10
  /** @private */
@@ -11,6 +12,8 @@ export declare function isObject(val: unknown): val is Record<PropertyKey, any>;
11
12
  /** @private */
12
13
  export declare function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[];
13
14
  /** @private */
15
+ export declare function isGraphQLError(obj: unknown): obj is GraphQLError;
16
+ /** @private */
14
17
  export declare function isExecutionResult(val: unknown): val is ExecutionResult;
15
18
  /** @private */
16
19
  export declare function isAsyncIterable<T = unknown>(val: unknown): val is AsyncIterable<T>;
package/lib/utils.d.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  * utils
4
4
  *
5
5
  */
6
- import type { ExecutionResult, GraphQLError } from 'graphql';
6
+ import type { ExecutionResult } from 'graphql';
7
+ import { GraphQLError } from 'graphql';
7
8
  /** @private */
8
9
  export declare function extendedTypeof(val: unknown): 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'array' | 'null';
9
10
  /** @private */
@@ -11,6 +12,8 @@ export declare function isObject(val: unknown): val is Record<PropertyKey, any>;
11
12
  /** @private */
12
13
  export declare function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[];
13
14
  /** @private */
15
+ export declare function isGraphQLError(obj: unknown): obj is GraphQLError;
16
+ /** @private */
14
17
  export declare function isExecutionResult(val: unknown): val is ExecutionResult;
15
18
  /** @private */
16
19
  export declare function isAsyncIterable<T = unknown>(val: unknown): val is AsyncIterable<T>;
package/lib/utils.js CHANGED
@@ -5,7 +5,8 @@
5
5
  *
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.isAsyncIterable = exports.isExecutionResult = exports.areGraphQLErrors = exports.isObject = exports.extendedTypeof = void 0;
8
+ exports.isAsyncIterable = exports.isExecutionResult = exports.isGraphQLError = exports.areGraphQLErrors = exports.isObject = exports.extendedTypeof = void 0;
9
+ const graphql_1 = require("graphql");
9
10
  /** @private */
10
11
  function extendedTypeof(val) {
11
12
  if (val === null) {
@@ -25,13 +26,17 @@ exports.isObject = isObject;
25
26
  /** @private */
26
27
  function areGraphQLErrors(obj) {
27
28
  return (Array.isArray(obj) &&
28
- // must be at least one error
29
29
  obj.length > 0 &&
30
- // error has at least a message
31
- obj.every((ob) => 'message' in ob));
30
+ // if one item in the array is a GraphQLError, we're good
31
+ obj.some(isGraphQLError));
32
32
  }
33
33
  exports.areGraphQLErrors = areGraphQLErrors;
34
34
  /** @private */
35
+ function isGraphQLError(obj) {
36
+ return obj instanceof graphql_1.GraphQLError;
37
+ }
38
+ exports.isGraphQLError = isGraphQLError;
39
+ /** @private */
35
40
  function isExecutionResult(val) {
36
41
  return (isObject(val) &&
37
42
  ('data' in val || ('data' in val && val.data == null && 'errors' in val)));
package/lib/utils.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
  * utils
4
4
  *
5
5
  */
6
+ import { GraphQLError } from 'graphql';
6
7
  /** @private */
7
8
  export function extendedTypeof(val) {
8
9
  if (val === null) {
@@ -20,10 +21,13 @@ export function isObject(val) {
20
21
  /** @private */
21
22
  export function areGraphQLErrors(obj) {
22
23
  return (Array.isArray(obj) &&
23
- // must be at least one error
24
24
  obj.length > 0 &&
25
- // error has at least a message
26
- obj.every((ob) => 'message' in ob));
25
+ // if one item in the array is a GraphQLError, we're good
26
+ obj.some(isGraphQLError));
27
+ }
28
+ /** @private */
29
+ export function isGraphQLError(obj) {
30
+ return obj instanceof GraphQLError;
27
31
  }
28
32
  /** @private */
29
33
  export function isExecutionResult(val) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-http",
3
- "version": "1.10.0",
3
+ "version": "1.12.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",
@@ -90,38 +95,42 @@
90
95
  "graphql": ">=0.11 <=16"
91
96
  },
92
97
  "devDependencies": {
93
- "@babel/core": "^7.20.7",
98
+ "@babel/core": "^7.20.12",
94
99
  "@babel/plugin-proposal-class-properties": "^7.18.6",
95
100
  "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
96
101
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
97
102
  "@babel/plugin-proposal-optional-chaining": "^7.20.7",
98
103
  "@babel/preset-env": "^7.20.2",
99
104
  "@babel/preset-typescript": "^7.18.6",
100
- "@rollup/plugin-terser": "^0.2.1",
101
- "@rollup/plugin-typescript": "^10.0.1",
105
+ "@rollup/plugin-terser": "^0.3.0",
106
+ "@rollup/plugin-typescript": "^11.0.0",
102
107
  "@semantic-release/changelog": "^6.0.2",
103
108
  "@semantic-release/git": "^10.0.1",
104
109
  "@types/eslint": "^8.4.10",
105
110
  "@types/express": "^4.17.15",
106
- "@types/glob": "^8.0.0",
107
- "@types/jest": "^29.2.4",
108
- "@typescript-eslint/eslint-plugin": "^5.47.0",
109
- "@typescript-eslint/parser": "^5.47.0",
110
- "@whatwg-node/fetch": "^0.5.3",
111
+ "@types/glob": "^8.0.1",
112
+ "@types/jest": "^29.2.6",
113
+ "@types/koa": "^2.13.5",
114
+ "@types/koa-mount": "^4.0.2",
115
+ "@typescript-eslint/eslint-plugin": "^5.48.2",
116
+ "@typescript-eslint/parser": "^5.48.2",
117
+ "@whatwg-node/fetch": "^0.6.2",
111
118
  "babel-jest": "^29.3.1",
112
- "eslint": "^8.30.0",
113
- "eslint-config-prettier": "^8.5.0",
119
+ "eslint": "^8.32.0",
120
+ "eslint-config-prettier": "^8.6.0",
114
121
  "express": "^4.18.2",
115
- "fastify": "^4.10.2",
122
+ "fastify": "^4.11.0",
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
- "prettier": "^2.8.1",
121
- "rollup": "^3.8.1",
122
- "semantic-release": "^19.0.5",
129
+ "prettier": "^2.8.3",
130
+ "rollup": "^3.10.0",
131
+ "semantic-release": "^20.0.2",
123
132
  "tslib": "^2.4.1",
124
- "typedoc": "^0.23.23",
133
+ "typedoc": "^0.23.24",
125
134
  "typedoc-plugin-markdown": "^3.14.0",
126
135
  "typescript": "^4.9.4"
127
136
  },
@@ -1,6 +1,6 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('graphql')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'graphql'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.graphqlHttp = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).graphqlHttp={})}(this,(function(e){"use strict";class t extends Error{constructor(e){let t,r;var o;(function(e){return"object"==typeof e&&null!==e})(o=e)&&"boolean"==typeof o.ok&&"number"==typeof o.status&&"string"==typeof o.statusText?(r=e,t="Server responded with "+e.status+": "+e.statusText):t=e instanceof Error?e.message:String(e),super(t),this.name=this.constructor.name,this.response=r}}e.NetworkError=t,e.createClient=function(e){const{credentials:r="same-origin",referrer:o,referrerPolicy:n,shouldRetry:s=(()=>!1)}=e,i=e.fetchFn||fetch,a=e.abortControllerImpl||AbortController,c=(()=>{let e=!1;const t=[];return{get disposed(){return e},onDispose:r=>e?(setTimeout((()=>r()),0),()=>{}):(t.push(r),()=>{t.splice(t.indexOf(r),1)}),dispose(){if(!e){e=!0;for(const e of[...t])e()}}}})();return{subscribe(p,l){if(c.disposed)throw new Error("Client has been disposed");const f=new a,u=c.onDispose((()=>{u(),f.abort()}));return(async()=>{var a;let c=null,u=0;for(;;){if(c){const e=await s(c,u);if(f.signal.aborted)return;if(!e)throw c;u++}try{const s="function"==typeof e.url?await e.url(p):e.url;if(f.signal.aborted)return;const c="function"==typeof e.headers?await e.headers():null!==(a=e.headers)&&void 0!==a?a:{};if(f.signal.aborted)return;let u;try{u=await i(s,{signal:f.signal,method:"POST",headers:Object.assign(Object.assign({},c),{"content-type":"application/json; charset=utf-8",accept:"application/graphql-response+json, application/json"}),credentials:r,referrer:o,referrerPolicy:n,body:JSON.stringify(p)})}catch(e){throw new t(e)}if(!u.ok)throw new t(u);if(!u.body)throw new Error("Missing response body");const d=u.headers.get("content-type");if(!d)throw new Error("Missing response content-type");if(!d.includes("application/graphql-response+json")&&!d.includes("application/json"))throw new Error(`Unsupported response content-type ${d}`);const h=await u.json();return l.next(h),f.abort()}catch(e){if(f.signal.aborted)return;if(!(e instanceof t))throw e;c=e}}})().then((()=>l.complete())).catch((e=>l.error(e))),()=>f.abort()},dispose(){c.dispose()}}}}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("graphql")):"function"==typeof define&&define.amd?define(["exports","graphql"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).graphqlHttp={})}(this,(function(e){"use strict";class t extends Error{constructor(e){let t,r;var o;(function(e){return"object"==typeof e&&null!==e})(o=e)&&"boolean"==typeof o.ok&&"number"==typeof o.status&&"string"==typeof o.statusText?(r=e,t="Server responded with "+e.status+": "+e.statusText):t=e instanceof Error?e.message:String(e),super(t),this.name=this.constructor.name,this.response=r}}e.NetworkError=t,e.createClient=function(e){const{credentials:r="same-origin",referrer:o,referrerPolicy:n,shouldRetry:s=(()=>!1)}=e,i=e.fetchFn||fetch,a=e.abortControllerImpl||AbortController,c=(()=>{let e=!1;const t=[];return{get disposed(){return e},onDispose:r=>e?(setTimeout((()=>r()),0),()=>{}):(t.push(r),()=>{t.splice(t.indexOf(r),1)}),dispose(){if(!e){e=!0;for(const e of[...t])e()}}}})();return{subscribe(p,l){if(c.disposed)throw new Error("Client has been disposed");const f=new a,u=c.onDispose((()=>{u(),f.abort()}));return(async()=>{var a;let c=null,u=0;for(;;){if(c){const e=await s(c,u);if(f.signal.aborted)return;if(!e)throw c;u++}try{const s="function"==typeof e.url?await e.url(p):e.url;if(f.signal.aborted)return;const c="function"==typeof e.headers?await e.headers():null!==(a=e.headers)&&void 0!==a?a:{};if(f.signal.aborted)return;let u;try{u=await i(s,{signal:f.signal,method:"POST",headers:Object.assign(Object.assign({},c),{"content-type":"application/json; charset=utf-8",accept:"application/graphql-response+json, application/json"}),credentials:r,referrer:o,referrerPolicy:n,body:JSON.stringify(p)})}catch(e){throw new t(e)}if(!u.ok)throw new t(u);if(!u.body)throw new Error("Missing response body");const d=u.headers.get("content-type");if(!d)throw new Error("Missing response content-type");if(!d.includes("application/graphql-response+json")&&!d.includes("application/json"))throw new Error(`Unsupported response content-type ${d}`);const h=await u.json();return l.next(h),f.abort()}catch(e){if(f.signal.aborted)return;if(!(e instanceof t))throw e;c=e}}})().then((()=>l.complete())).catch((e=>l.error(e))),()=>f.abort()},dispose(){c.dispose()}}}}));
Binary file