@envelop/core 2.3.2-alpha-3ace273.0 → 2.3.2-alpha-c913692.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
@@ -1092,7 +1092,8 @@ const useSchema = (schema) => {
1092
1092
  };
1093
1093
  };
1094
1094
  const VALIDATE_FN = Symbol('VALIDATE_FN');
1095
- const useLazyLoadedSchema = (schemaLoader) => {
1095
+ const VALIDATION_ERRORS = Symbol('VALIDATION_ERRORS');
1096
+ const useLazyLoadedSchema = (schemaLoader, validateSchema = true) => {
1096
1097
  let schemaSet$;
1097
1098
  return {
1098
1099
  onEnveloped({ setSchema, context }) {
@@ -1100,72 +1101,93 @@ const useLazyLoadedSchema = (schemaLoader) => {
1100
1101
  if (isPromise(schema$)) {
1101
1102
  schemaSet$ = schema$.then(schemaObj => {
1102
1103
  setSchema(schemaObj);
1104
+ schemaSet$ = undefined;
1103
1105
  });
1104
1106
  }
1105
1107
  else {
1106
1108
  setSchema(schema$);
1107
1109
  }
1108
1110
  },
1109
- onValidate({ validateFn, setValidationFn, extendContext }) {
1111
+ onValidate({ validateFn, params, setValidationFn, extendContext }) {
1110
1112
  if (schemaSet$) {
1111
- extendContext({
1112
- [VALIDATE_FN]: validateFn,
1113
- });
1113
+ if (validateSchema) {
1114
+ extendContext({
1115
+ [VALIDATE_FN]: (schema) => validateFn(schema, params.documentAST, params.rules, params.typeInfo, params.options),
1116
+ });
1117
+ }
1114
1118
  setValidationFn(() => []);
1115
1119
  }
1116
1120
  },
1117
- onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1118
- if (schemaSet$) {
1119
- const validateFn = contextValue[VALIDATE_FN];
1120
- const errors = validateFn(schema, document);
1121
+ async onContextBuilding({ context, extendContext, breakContextBuilding }) {
1122
+ if (context[VALIDATE_FN]) {
1123
+ const schema = await schemaSet$;
1124
+ const validateFn = context[VALIDATE_FN];
1125
+ const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema);
1121
1126
  if (errors === null || errors === void 0 ? void 0 : errors.length) {
1122
- setResultAndStopExecution({
1123
- errors,
1127
+ extendContext({
1128
+ [VALIDATION_ERRORS]: errors,
1124
1129
  });
1125
- }
1126
- else {
1127
- return schemaSet$;
1130
+ breakContextBuilding();
1128
1131
  }
1129
1132
  }
1130
- return undefined;
1133
+ },
1134
+ onExecute({ args: { contextValue }, setResultAndStopExecution }) {
1135
+ if (contextValue[VALIDATION_ERRORS]) {
1136
+ setResultAndStopExecution({
1137
+ errors: contextValue[VALIDATION_ERRORS],
1138
+ });
1139
+ }
1131
1140
  },
1132
1141
  };
1133
1142
  };
1134
- const useAsyncSchema = (schema$) => {
1143
+ const useAsyncSchema = (schema$, validateSchema = true) => {
1135
1144
  let schemaSet$;
1136
1145
  return {
1137
1146
  onPluginInit({ setSchema }) {
1138
1147
  if (isPromise(schema$)) {
1139
1148
  schemaSet$ = schema$.then(schemaObj => {
1140
1149
  setSchema(schemaObj);
1150
+ // Once the schema is completely resolved, we don't need to keep the logic below.
1151
+ schemaSet$ = undefined;
1152
+ return schemaObj;
1141
1153
  });
1142
1154
  }
1143
1155
  else {
1144
1156
  setSchema(schema$);
1145
1157
  }
1146
1158
  },
1147
- onValidate({ validateFn, setValidationFn, extendContext }) {
1159
+ onValidate({ validateFn, params, setValidationFn, extendContext }) {
1160
+ // If schemaSet promise is still ongoing
1148
1161
  if (schemaSet$) {
1149
- extendContext({
1150
- [VALIDATE_FN]: validateFn,
1151
- });
1162
+ if (validateSchema) {
1163
+ extendContext({
1164
+ [VALIDATE_FN]: (schema) => validateFn(schema, params.documentAST, params.rules, params.typeInfo, params.options),
1165
+ });
1166
+ }
1152
1167
  setValidationFn(() => []);
1153
1168
  }
1154
1169
  },
1155
- onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1170
+ async onContextBuilding({ context, extendContext, breakContextBuilding }) {
1171
+ // If schemaSet promise is still ongoing
1156
1172
  if (schemaSet$) {
1157
- const validateFn = contextValue[VALIDATE_FN];
1158
- const errors = validateFn(schema, document);
1173
+ const schema = await schemaSet$;
1174
+ const validateFn = context[VALIDATE_FN];
1175
+ const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema);
1159
1176
  if (errors === null || errors === void 0 ? void 0 : errors.length) {
1160
- setResultAndStopExecution({
1161
- errors,
1177
+ extendContext({
1178
+ [VALIDATION_ERRORS]: errors,
1162
1179
  });
1163
- }
1164
- else {
1165
- return schemaSet$;
1180
+ breakContextBuilding();
1166
1181
  }
1167
1182
  }
1168
- return undefined;
1183
+ },
1184
+ onExecute({ args: { contextValue }, setResultAndStopExecution }) {
1185
+ // If validation errors are set in context
1186
+ if (contextValue[VALIDATION_ERRORS]) {
1187
+ setResultAndStopExecution({
1188
+ errors: contextValue[VALIDATION_ERRORS],
1189
+ });
1190
+ }
1169
1191
  },
1170
1192
  };
1171
1193
  };
package/index.mjs CHANGED
@@ -1088,7 +1088,8 @@ const useSchema = (schema) => {
1088
1088
  };
1089
1089
  };
1090
1090
  const VALIDATE_FN = Symbol('VALIDATE_FN');
1091
- const useLazyLoadedSchema = (schemaLoader) => {
1091
+ const VALIDATION_ERRORS = Symbol('VALIDATION_ERRORS');
1092
+ const useLazyLoadedSchema = (schemaLoader, validateSchema = true) => {
1092
1093
  let schemaSet$;
1093
1094
  return {
1094
1095
  onEnveloped({ setSchema, context }) {
@@ -1096,72 +1097,93 @@ const useLazyLoadedSchema = (schemaLoader) => {
1096
1097
  if (isPromise(schema$)) {
1097
1098
  schemaSet$ = schema$.then(schemaObj => {
1098
1099
  setSchema(schemaObj);
1100
+ schemaSet$ = undefined;
1099
1101
  });
1100
1102
  }
1101
1103
  else {
1102
1104
  setSchema(schema$);
1103
1105
  }
1104
1106
  },
1105
- onValidate({ validateFn, setValidationFn, extendContext }) {
1107
+ onValidate({ validateFn, params, setValidationFn, extendContext }) {
1106
1108
  if (schemaSet$) {
1107
- extendContext({
1108
- [VALIDATE_FN]: validateFn,
1109
- });
1109
+ if (validateSchema) {
1110
+ extendContext({
1111
+ [VALIDATE_FN]: (schema) => validateFn(schema, params.documentAST, params.rules, params.typeInfo, params.options),
1112
+ });
1113
+ }
1110
1114
  setValidationFn(() => []);
1111
1115
  }
1112
1116
  },
1113
- onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1114
- if (schemaSet$) {
1115
- const validateFn = contextValue[VALIDATE_FN];
1116
- const errors = validateFn(schema, document);
1117
+ async onContextBuilding({ context, extendContext, breakContextBuilding }) {
1118
+ if (context[VALIDATE_FN]) {
1119
+ const schema = await schemaSet$;
1120
+ const validateFn = context[VALIDATE_FN];
1121
+ const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema);
1117
1122
  if (errors === null || errors === void 0 ? void 0 : errors.length) {
1118
- setResultAndStopExecution({
1119
- errors,
1123
+ extendContext({
1124
+ [VALIDATION_ERRORS]: errors,
1120
1125
  });
1121
- }
1122
- else {
1123
- return schemaSet$;
1126
+ breakContextBuilding();
1124
1127
  }
1125
1128
  }
1126
- return undefined;
1129
+ },
1130
+ onExecute({ args: { contextValue }, setResultAndStopExecution }) {
1131
+ if (contextValue[VALIDATION_ERRORS]) {
1132
+ setResultAndStopExecution({
1133
+ errors: contextValue[VALIDATION_ERRORS],
1134
+ });
1135
+ }
1127
1136
  },
1128
1137
  };
1129
1138
  };
1130
- const useAsyncSchema = (schema$) => {
1139
+ const useAsyncSchema = (schema$, validateSchema = true) => {
1131
1140
  let schemaSet$;
1132
1141
  return {
1133
1142
  onPluginInit({ setSchema }) {
1134
1143
  if (isPromise(schema$)) {
1135
1144
  schemaSet$ = schema$.then(schemaObj => {
1136
1145
  setSchema(schemaObj);
1146
+ // Once the schema is completely resolved, we don't need to keep the logic below.
1147
+ schemaSet$ = undefined;
1148
+ return schemaObj;
1137
1149
  });
1138
1150
  }
1139
1151
  else {
1140
1152
  setSchema(schema$);
1141
1153
  }
1142
1154
  },
1143
- onValidate({ validateFn, setValidationFn, extendContext }) {
1155
+ onValidate({ validateFn, params, setValidationFn, extendContext }) {
1156
+ // If schemaSet promise is still ongoing
1144
1157
  if (schemaSet$) {
1145
- extendContext({
1146
- [VALIDATE_FN]: validateFn,
1147
- });
1158
+ if (validateSchema) {
1159
+ extendContext({
1160
+ [VALIDATE_FN]: (schema) => validateFn(schema, params.documentAST, params.rules, params.typeInfo, params.options),
1161
+ });
1162
+ }
1148
1163
  setValidationFn(() => []);
1149
1164
  }
1150
1165
  },
1151
- onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1166
+ async onContextBuilding({ context, extendContext, breakContextBuilding }) {
1167
+ // If schemaSet promise is still ongoing
1152
1168
  if (schemaSet$) {
1153
- const validateFn = contextValue[VALIDATE_FN];
1154
- const errors = validateFn(schema, document);
1169
+ const schema = await schemaSet$;
1170
+ const validateFn = context[VALIDATE_FN];
1171
+ const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema);
1155
1172
  if (errors === null || errors === void 0 ? void 0 : errors.length) {
1156
- setResultAndStopExecution({
1157
- errors,
1173
+ extendContext({
1174
+ [VALIDATION_ERRORS]: errors,
1158
1175
  });
1159
- }
1160
- else {
1161
- return schemaSet$;
1176
+ breakContextBuilding();
1162
1177
  }
1163
1178
  }
1164
- return undefined;
1179
+ },
1180
+ onExecute({ args: { contextValue }, setResultAndStopExecution }) {
1181
+ // If validation errors are set in context
1182
+ if (contextValue[VALIDATION_ERRORS]) {
1183
+ setResultAndStopExecution({
1184
+ errors: contextValue[VALIDATION_ERRORS],
1185
+ });
1186
+ }
1165
1187
  },
1166
1188
  };
1167
1189
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "2.3.2-alpha-3ace273.0",
3
+ "version": "2.3.2-alpha-c913692.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
@@ -1,5 +1,5 @@
1
1
  import { GraphQLSchema } from 'graphql';
2
2
  import { DefaultContext, Maybe, Plugin, PromiseOrValue } from '@envelop/types';
3
3
  export declare const useSchema: (schema: GraphQLSchema) => Plugin;
4
- export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => PromiseOrValue<GraphQLSchema>) => Plugin;
5
- export declare const useAsyncSchema: (schema$: PromiseOrValue<GraphQLSchema>) => Plugin;
4
+ export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => PromiseOrValue<GraphQLSchema>, validateSchema?: boolean) => Plugin;
5
+ export declare const useAsyncSchema: (schema$: PromiseOrValue<GraphQLSchema>, validateSchema?: boolean) => Plugin;