@envelop/core 2.2.0-alpha-555debe.0 → 2.2.0-alpha-e0e4d8e.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/index.js CHANGED
@@ -475,6 +475,9 @@ function createEnvelopOrchestrator(plugins) {
475
475
  extendContext: extension => {
476
476
  Object.assign(context, extension);
477
477
  },
478
+ setResult: newResult => {
479
+ result = newResult;
480
+ },
478
481
  });
479
482
  }
480
483
  return result;
@@ -1099,33 +1102,22 @@ const useAsyncSchema = (schemaPromise) => {
1099
1102
  };
1100
1103
  };
1101
1104
 
1102
- const useErrorHandler = (errorHandler) => {
1103
- return {
1104
- onValidate({ context, params }) {
1105
- return function ({ result }) {
1106
- if (result.length) {
1107
- errorHandler(result, {
1108
- schema: params.schema,
1109
- document: params.documentAST,
1110
- contextValue: context,
1111
- });
1112
- }
1113
- };
1114
- },
1115
- onExecute() {
1116
- return {
1117
- onExecuteDone(payload) {
1118
- return handleStreamOrSingleExecutionResult(payload, ({ result, args }) => {
1119
- var _a;
1120
- if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
1121
- errorHandler(result.errors, args);
1122
- }
1123
- });
1124
- },
1125
- };
1126
- },
1127
- };
1105
+ const makeHandleResult = (errorHandler) => ({ result, args }) => {
1106
+ var _a;
1107
+ if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
1108
+ errorHandler(result.errors, args);
1109
+ }
1128
1110
  };
1111
+ const useErrorHandler = (errorHandler) => ({
1112
+ onExecute() {
1113
+ const handleResult = makeHandleResult(errorHandler);
1114
+ return {
1115
+ onExecuteDone(payload) {
1116
+ return handleStreamOrSingleExecutionResult(payload, handleResult);
1117
+ },
1118
+ };
1119
+ },
1120
+ });
1129
1121
 
1130
1122
  const useExtendContext = (contextFactory) => ({
1131
1123
  async onContextBuilding({ context, extendContext }) {
@@ -1133,7 +1125,7 @@ const useExtendContext = (contextFactory) => ({
1133
1125
  },
1134
1126
  });
1135
1127
 
1136
- const makeHandleResult = (formatter) => ({ args, result, setResult, }) => {
1128
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1137
1129
  const modified = formatter(result, args);
1138
1130
  if (modified !== false) {
1139
1131
  setResult(modified);
@@ -1141,7 +1133,7 @@ const makeHandleResult = (formatter) => ({ args, result, setResult, }) => {
1141
1133
  };
1142
1134
  const usePayloadFormatter = (formatter) => ({
1143
1135
  onExecute() {
1144
- const handleResult = makeHandleResult(formatter);
1136
+ const handleResult = makeHandleResult$1(formatter);
1145
1137
  return {
1146
1138
  onExecuteDone(payload) {
1147
1139
  return handleStreamOrSingleExecutionResult(payload, handleResult);
@@ -1157,13 +1149,18 @@ class EnvelopError extends graphql.GraphQLError {
1157
1149
  }
1158
1150
  }
1159
1151
  const formatError = (err, message, isDev) => {
1152
+ var _a, _b, _c, _d;
1160
1153
  if (err instanceof graphql.GraphQLError) {
1161
- if (err.originalError && err.originalError instanceof EnvelopError === false) {
1154
+ if (
1155
+ /** execution error */
1156
+ (err.originalError && err.originalError instanceof EnvelopError === false) ||
1157
+ /** validate and parse errors */
1158
+ (err.originalError === undefined && err instanceof EnvelopError === false)) {
1162
1159
  return new graphql.GraphQLError(message, err.nodes, err.source, err.positions, err.path, undefined, isDev
1163
1160
  ? {
1164
1161
  originalError: {
1165
- message: err.originalError.message,
1166
- stack: err.originalError.stack,
1162
+ message: (_b = (_a = err.originalError) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : err.message,
1163
+ stack: (_d = (_c = err.originalError) === null || _c === void 0 ? void 0 : _c.stack) !== null && _d !== void 0 ? _d : err.stack,
1167
1164
  },
1168
1165
  }
1169
1166
  : undefined);
@@ -1172,7 +1169,7 @@ const formatError = (err, message, isDev) => {
1172
1169
  }
1173
1170
  return new graphql.GraphQLError(message);
1174
1171
  };
1175
- const makeHandleResult$1 = (format, message, isDev) => ({ result, setResult }) => {
1172
+ const makeHandleResult$2 = (format, message, isDev) => ({ result, setResult }) => {
1176
1173
  if (result.errors != null) {
1177
1174
  setResult({ ...result, errors: result.errors.map(error => format(error, message, isDev)) });
1178
1175
  }
@@ -1183,8 +1180,26 @@ const useMaskedErrors = (opts) => {
1183
1180
  const message = (opts === null || opts === void 0 ? void 0 : opts.errorMessage) || DEFAULT_ERROR_MESSAGE;
1184
1181
  // eslint-disable-next-line dot-notation
1185
1182
  const isDev = (_b = opts === null || opts === void 0 ? void 0 : opts.isDev) !== null && _b !== void 0 ? _b : (typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false);
1186
- const handleResult = makeHandleResult$1(format, message, isDev);
1183
+ const handleResult = makeHandleResult$2(format, message, isDev);
1187
1184
  return {
1185
+ onParse: (opts === null || opts === void 0 ? void 0 : opts.onParse) === true
1186
+ ? function onParse() {
1187
+ return function onParseEnd({ result, replaceParseResult }) {
1188
+ if (result instanceof Error) {
1189
+ replaceParseResult(format(result, message, isDev));
1190
+ }
1191
+ };
1192
+ }
1193
+ : undefined,
1194
+ onValidate: (opts === null || opts === void 0 ? void 0 : opts.onValidate) === true
1195
+ ? function onValidate() {
1196
+ return function onValidateEnd({ valid, result, setResult }) {
1197
+ if (valid === false) {
1198
+ setResult(result.map(error => format(error, message, isDev)));
1199
+ }
1200
+ };
1201
+ }
1202
+ : undefined,
1188
1203
  onPluginInit(context) {
1189
1204
  context.registerContextErrorHandler(({ error, setError }) => {
1190
1205
  setError(formatError(error, message, isDev));
package/index.mjs CHANGED
@@ -471,6 +471,9 @@ function createEnvelopOrchestrator(plugins) {
471
471
  extendContext: extension => {
472
472
  Object.assign(context, extension);
473
473
  },
474
+ setResult: newResult => {
475
+ result = newResult;
476
+ },
474
477
  });
475
478
  }
476
479
  return result;
@@ -1095,33 +1098,22 @@ const useAsyncSchema = (schemaPromise) => {
1095
1098
  };
1096
1099
  };
1097
1100
 
1098
- const useErrorHandler = (errorHandler) => {
1099
- return {
1100
- onValidate({ context, params }) {
1101
- return function ({ result }) {
1102
- if (result.length) {
1103
- errorHandler(result, {
1104
- schema: params.schema,
1105
- document: params.documentAST,
1106
- contextValue: context,
1107
- });
1108
- }
1109
- };
1110
- },
1111
- onExecute() {
1112
- return {
1113
- onExecuteDone(payload) {
1114
- return handleStreamOrSingleExecutionResult(payload, ({ result, args }) => {
1115
- var _a;
1116
- if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
1117
- errorHandler(result.errors, args);
1118
- }
1119
- });
1120
- },
1121
- };
1122
- },
1123
- };
1101
+ const makeHandleResult = (errorHandler) => ({ result, args }) => {
1102
+ var _a;
1103
+ if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
1104
+ errorHandler(result.errors, args);
1105
+ }
1124
1106
  };
1107
+ const useErrorHandler = (errorHandler) => ({
1108
+ onExecute() {
1109
+ const handleResult = makeHandleResult(errorHandler);
1110
+ return {
1111
+ onExecuteDone(payload) {
1112
+ return handleStreamOrSingleExecutionResult(payload, handleResult);
1113
+ },
1114
+ };
1115
+ },
1116
+ });
1125
1117
 
1126
1118
  const useExtendContext = (contextFactory) => ({
1127
1119
  async onContextBuilding({ context, extendContext }) {
@@ -1129,7 +1121,7 @@ const useExtendContext = (contextFactory) => ({
1129
1121
  },
1130
1122
  });
1131
1123
 
1132
- const makeHandleResult = (formatter) => ({ args, result, setResult, }) => {
1124
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1133
1125
  const modified = formatter(result, args);
1134
1126
  if (modified !== false) {
1135
1127
  setResult(modified);
@@ -1137,7 +1129,7 @@ const makeHandleResult = (formatter) => ({ args, result, setResult, }) => {
1137
1129
  };
1138
1130
  const usePayloadFormatter = (formatter) => ({
1139
1131
  onExecute() {
1140
- const handleResult = makeHandleResult(formatter);
1132
+ const handleResult = makeHandleResult$1(formatter);
1141
1133
  return {
1142
1134
  onExecuteDone(payload) {
1143
1135
  return handleStreamOrSingleExecutionResult(payload, handleResult);
@@ -1153,13 +1145,18 @@ class EnvelopError extends GraphQLError {
1153
1145
  }
1154
1146
  }
1155
1147
  const formatError = (err, message, isDev) => {
1148
+ var _a, _b, _c, _d;
1156
1149
  if (err instanceof GraphQLError) {
1157
- if (err.originalError && err.originalError instanceof EnvelopError === false) {
1150
+ if (
1151
+ /** execution error */
1152
+ (err.originalError && err.originalError instanceof EnvelopError === false) ||
1153
+ /** validate and parse errors */
1154
+ (err.originalError === undefined && err instanceof EnvelopError === false)) {
1158
1155
  return new GraphQLError(message, err.nodes, err.source, err.positions, err.path, undefined, isDev
1159
1156
  ? {
1160
1157
  originalError: {
1161
- message: err.originalError.message,
1162
- stack: err.originalError.stack,
1158
+ message: (_b = (_a = err.originalError) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : err.message,
1159
+ stack: (_d = (_c = err.originalError) === null || _c === void 0 ? void 0 : _c.stack) !== null && _d !== void 0 ? _d : err.stack,
1163
1160
  },
1164
1161
  }
1165
1162
  : undefined);
@@ -1168,7 +1165,7 @@ const formatError = (err, message, isDev) => {
1168
1165
  }
1169
1166
  return new GraphQLError(message);
1170
1167
  };
1171
- const makeHandleResult$1 = (format, message, isDev) => ({ result, setResult }) => {
1168
+ const makeHandleResult$2 = (format, message, isDev) => ({ result, setResult }) => {
1172
1169
  if (result.errors != null) {
1173
1170
  setResult({ ...result, errors: result.errors.map(error => format(error, message, isDev)) });
1174
1171
  }
@@ -1179,8 +1176,26 @@ const useMaskedErrors = (opts) => {
1179
1176
  const message = (opts === null || opts === void 0 ? void 0 : opts.errorMessage) || DEFAULT_ERROR_MESSAGE;
1180
1177
  // eslint-disable-next-line dot-notation
1181
1178
  const isDev = (_b = opts === null || opts === void 0 ? void 0 : opts.isDev) !== null && _b !== void 0 ? _b : (typeof process !== 'undefined' ? process.env['NODE_ENV'] === 'development' : false);
1182
- const handleResult = makeHandleResult$1(format, message, isDev);
1179
+ const handleResult = makeHandleResult$2(format, message, isDev);
1183
1180
  return {
1181
+ onParse: (opts === null || opts === void 0 ? void 0 : opts.onParse) === true
1182
+ ? function onParse() {
1183
+ return function onParseEnd({ result, replaceParseResult }) {
1184
+ if (result instanceof Error) {
1185
+ replaceParseResult(format(result, message, isDev));
1186
+ }
1187
+ };
1188
+ }
1189
+ : undefined,
1190
+ onValidate: (opts === null || opts === void 0 ? void 0 : opts.onValidate) === true
1191
+ ? function onValidate() {
1192
+ return function onValidateEnd({ valid, result, setResult }) {
1193
+ if (valid === false) {
1194
+ setResult(result.map(error => format(error, message, isDev)));
1195
+ }
1196
+ };
1197
+ }
1198
+ : undefined,
1184
1199
  onPluginInit(context) {
1185
1200
  context.registerContextErrorHandler(({ error, setError }) => {
1186
1201
  setError(formatError(error, message, isDev));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "2.2.0-alpha-555debe.0",
3
+ "version": "2.2.0-alpha-e0e4d8e.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "@envelop/types": "2.0.0"
9
+ "@envelop/types": "2.1.0-alpha-e0e4d8e.0"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -1,4 +1,4 @@
1
- import { Plugin, DefaultContext, TypedExecutionArgs } from '@envelop/types';
1
+ import { Plugin, DefaultContext } from '@envelop/types';
2
2
  import { GraphQLError } from 'graphql';
3
- export declare type ErrorHandler = <TContext = DefaultContext>(errors: readonly GraphQLError[], context: TypedExecutionArgs<TContext>) => void;
3
+ export declare type ErrorHandler = (errors: readonly GraphQLError[], context: Readonly<DefaultContext>) => void;
4
4
  export declare const useErrorHandler: <ContextType>(errorHandler: ErrorHandler) => Plugin<ContextType>;
@@ -16,5 +16,15 @@ export declare type UseMaskedErrorsOpts = {
16
16
  * The default value is `process.env['NODE_ENV'] === 'development'`
17
17
  */
18
18
  isDev?: boolean;
19
+ /**
20
+ * Whether parse errors should be masked.
21
+ * @default false
22
+ */
23
+ onParse?: boolean;
24
+ /**
25
+ * Whether validation errors should be masked.
26
+ * @default false
27
+ */
28
+ onValidate?: boolean;
19
29
  };
20
30
  export declare const useMaskedErrors: (opts?: UseMaskedErrorsOpts | undefined) => Plugin;