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