@envelop/core 2.3.0 → 2.3.2-alpha-3ace273.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
@@ -299,6 +299,10 @@ function errorAsyncIterator(source, onError) {
299
299
  };
300
300
  return stream;
301
301
  }
302
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
303
+ function isPromise(obj) {
304
+ return obj != null && obj.then;
305
+ }
302
306
 
303
307
  function createEnvelopOrchestrator(plugins) {
304
308
  let schema = null;
@@ -1087,19 +1091,81 @@ const useSchema = (schema) => {
1087
1091
  },
1088
1092
  };
1089
1093
  };
1094
+ const VALIDATE_FN = Symbol('VALIDATE_FN');
1090
1095
  const useLazyLoadedSchema = (schemaLoader) => {
1096
+ let schemaSet$;
1091
1097
  return {
1092
1098
  onEnveloped({ setSchema, context }) {
1093
- setSchema(schemaLoader(context));
1099
+ const schema$ = schemaLoader(context);
1100
+ if (isPromise(schema$)) {
1101
+ schemaSet$ = schema$.then(schemaObj => {
1102
+ setSchema(schemaObj);
1103
+ });
1104
+ }
1105
+ else {
1106
+ setSchema(schema$);
1107
+ }
1108
+ },
1109
+ onValidate({ validateFn, setValidationFn, extendContext }) {
1110
+ if (schemaSet$) {
1111
+ extendContext({
1112
+ [VALIDATE_FN]: validateFn,
1113
+ });
1114
+ setValidationFn(() => []);
1115
+ }
1116
+ },
1117
+ onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1118
+ if (schemaSet$) {
1119
+ const validateFn = contextValue[VALIDATE_FN];
1120
+ const errors = validateFn(schema, document);
1121
+ if (errors === null || errors === void 0 ? void 0 : errors.length) {
1122
+ setResultAndStopExecution({
1123
+ errors,
1124
+ });
1125
+ }
1126
+ else {
1127
+ return schemaSet$;
1128
+ }
1129
+ }
1130
+ return undefined;
1094
1131
  },
1095
1132
  };
1096
1133
  };
1097
- const useAsyncSchema = (schemaPromise) => {
1134
+ const useAsyncSchema = (schema$) => {
1135
+ let schemaSet$;
1098
1136
  return {
1099
1137
  onPluginInit({ setSchema }) {
1100
- schemaPromise.then(schemaObj => {
1101
- setSchema(schemaObj);
1102
- });
1138
+ if (isPromise(schema$)) {
1139
+ schemaSet$ = schema$.then(schemaObj => {
1140
+ setSchema(schemaObj);
1141
+ });
1142
+ }
1143
+ else {
1144
+ setSchema(schema$);
1145
+ }
1146
+ },
1147
+ onValidate({ validateFn, setValidationFn, extendContext }) {
1148
+ if (schemaSet$) {
1149
+ extendContext({
1150
+ [VALIDATE_FN]: validateFn,
1151
+ });
1152
+ setValidationFn(() => []);
1153
+ }
1154
+ },
1155
+ onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1156
+ if (schemaSet$) {
1157
+ const validateFn = contextValue[VALIDATE_FN];
1158
+ const errors = validateFn(schema, document);
1159
+ if (errors === null || errors === void 0 ? void 0 : errors.length) {
1160
+ setResultAndStopExecution({
1161
+ errors,
1162
+ });
1163
+ }
1164
+ else {
1165
+ return schemaSet$;
1166
+ }
1167
+ }
1168
+ return undefined;
1103
1169
  },
1104
1170
  };
1105
1171
  };
@@ -1229,9 +1295,15 @@ const useMaskedErrors = (opts) => {
1229
1295
 
1230
1296
  const OnNonIntrospectionFieldReachedValidationRule = (onNonIntrospectionField) => ctx => {
1231
1297
  const rootQueryType = ctx.getSchema().getQueryType();
1298
+ const rootMutationType = ctx.getSchema().getMutationType();
1299
+ const rootSubscriptionType = ctx.getSchema().getSubscriptionType();
1232
1300
  return {
1233
1301
  Field(field) {
1234
- if (ctx.getParentType() === rootQueryType && !field.name.value.startsWith('__')) {
1302
+ const parentType = ctx.getParentType();
1303
+ const isQuery = parentType === rootQueryType;
1304
+ const isMutation = parentType === rootMutationType;
1305
+ const isSubscription = parentType === rootSubscriptionType;
1306
+ if ((isQuery && !field.name.value.startsWith('__')) || isMutation || isSubscription) {
1235
1307
  onNonIntrospectionField();
1236
1308
  return graphql.BREAK;
1237
1309
  }
@@ -1290,6 +1362,7 @@ exports.isIntrospectionOperation = isIntrospectionOperation;
1290
1362
  exports.isIntrospectionOperationString = isIntrospectionOperationString;
1291
1363
  exports.isOperationDefinition = isOperationDefinition;
1292
1364
  exports.isPluginEnabled = isPluginEnabled;
1365
+ exports.isPromise = isPromise;
1293
1366
  exports.makeExecute = makeExecute;
1294
1367
  exports.makeSubscribe = makeSubscribe;
1295
1368
  exports.mapAsyncIterator = mapAsyncIterator;
package/index.mjs CHANGED
@@ -295,6 +295,10 @@ function errorAsyncIterator(source, onError) {
295
295
  };
296
296
  return stream;
297
297
  }
298
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
299
+ function isPromise(obj) {
300
+ return obj != null && obj.then;
301
+ }
298
302
 
299
303
  function createEnvelopOrchestrator(plugins) {
300
304
  let schema = null;
@@ -1083,19 +1087,81 @@ const useSchema = (schema) => {
1083
1087
  },
1084
1088
  };
1085
1089
  };
1090
+ const VALIDATE_FN = Symbol('VALIDATE_FN');
1086
1091
  const useLazyLoadedSchema = (schemaLoader) => {
1092
+ let schemaSet$;
1087
1093
  return {
1088
1094
  onEnveloped({ setSchema, context }) {
1089
- setSchema(schemaLoader(context));
1095
+ const schema$ = schemaLoader(context);
1096
+ if (isPromise(schema$)) {
1097
+ schemaSet$ = schema$.then(schemaObj => {
1098
+ setSchema(schemaObj);
1099
+ });
1100
+ }
1101
+ else {
1102
+ setSchema(schema$);
1103
+ }
1104
+ },
1105
+ onValidate({ validateFn, setValidationFn, extendContext }) {
1106
+ if (schemaSet$) {
1107
+ extendContext({
1108
+ [VALIDATE_FN]: validateFn,
1109
+ });
1110
+ setValidationFn(() => []);
1111
+ }
1112
+ },
1113
+ onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1114
+ if (schemaSet$) {
1115
+ const validateFn = contextValue[VALIDATE_FN];
1116
+ const errors = validateFn(schema, document);
1117
+ if (errors === null || errors === void 0 ? void 0 : errors.length) {
1118
+ setResultAndStopExecution({
1119
+ errors,
1120
+ });
1121
+ }
1122
+ else {
1123
+ return schemaSet$;
1124
+ }
1125
+ }
1126
+ return undefined;
1090
1127
  },
1091
1128
  };
1092
1129
  };
1093
- const useAsyncSchema = (schemaPromise) => {
1130
+ const useAsyncSchema = (schema$) => {
1131
+ let schemaSet$;
1094
1132
  return {
1095
1133
  onPluginInit({ setSchema }) {
1096
- schemaPromise.then(schemaObj => {
1097
- setSchema(schemaObj);
1098
- });
1134
+ if (isPromise(schema$)) {
1135
+ schemaSet$ = schema$.then(schemaObj => {
1136
+ setSchema(schemaObj);
1137
+ });
1138
+ }
1139
+ else {
1140
+ setSchema(schema$);
1141
+ }
1142
+ },
1143
+ onValidate({ validateFn, setValidationFn, extendContext }) {
1144
+ if (schemaSet$) {
1145
+ extendContext({
1146
+ [VALIDATE_FN]: validateFn,
1147
+ });
1148
+ setValidationFn(() => []);
1149
+ }
1150
+ },
1151
+ onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
1152
+ if (schemaSet$) {
1153
+ const validateFn = contextValue[VALIDATE_FN];
1154
+ const errors = validateFn(schema, document);
1155
+ if (errors === null || errors === void 0 ? void 0 : errors.length) {
1156
+ setResultAndStopExecution({
1157
+ errors,
1158
+ });
1159
+ }
1160
+ else {
1161
+ return schemaSet$;
1162
+ }
1163
+ }
1164
+ return undefined;
1099
1165
  },
1100
1166
  };
1101
1167
  };
@@ -1225,9 +1291,15 @@ const useMaskedErrors = (opts) => {
1225
1291
 
1226
1292
  const OnNonIntrospectionFieldReachedValidationRule = (onNonIntrospectionField) => ctx => {
1227
1293
  const rootQueryType = ctx.getSchema().getQueryType();
1294
+ const rootMutationType = ctx.getSchema().getMutationType();
1295
+ const rootSubscriptionType = ctx.getSchema().getSubscriptionType();
1228
1296
  return {
1229
1297
  Field(field) {
1230
- if (ctx.getParentType() === rootQueryType && !field.name.value.startsWith('__')) {
1298
+ const parentType = ctx.getParentType();
1299
+ const isQuery = parentType === rootQueryType;
1300
+ const isMutation = parentType === rootMutationType;
1301
+ const isSubscription = parentType === rootSubscriptionType;
1302
+ if ((isQuery && !field.name.value.startsWith('__')) || isMutation || isSubscription) {
1231
1303
  onNonIntrospectionField();
1232
1304
  return BREAK;
1233
1305
  }
@@ -1263,4 +1335,4 @@ const useImmediateIntrospection = () => {
1263
1335
  };
1264
1336
  };
1265
1337
 
1266
- export { DEFAULT_ERROR_MESSAGE, EnvelopError, enableIf, envelop, envelopIsIntrospectionSymbol, errorAsyncIterator, finalAsyncIterator, formatError, handleStreamOrSingleExecutionResult, isAsyncIterable, isIntrospectionDocument, isIntrospectionOperation, isIntrospectionOperationString, isOperationDefinition, isPluginEnabled, makeExecute, makeSubscribe, mapAsyncIterator, useAsyncSchema, useEnvelop, useErrorHandler, useExtendContext, useImmediateIntrospection, useLazyLoadedSchema, useLogger, useMaskedErrors, usePayloadFormatter, useSchema, useTiming };
1338
+ export { DEFAULT_ERROR_MESSAGE, EnvelopError, enableIf, envelop, envelopIsIntrospectionSymbol, errorAsyncIterator, finalAsyncIterator, formatError, handleStreamOrSingleExecutionResult, isAsyncIterable, isIntrospectionDocument, isIntrospectionOperation, isIntrospectionOperationString, isOperationDefinition, isPluginEnabled, isPromise, makeExecute, makeSubscribe, mapAsyncIterator, useAsyncSchema, useEnvelop, useErrorHandler, useExtendContext, useImmediateIntrospection, useLazyLoadedSchema, useLogger, useMaskedErrors, usePayloadFormatter, useSchema, useTiming };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "2.3.0",
3
+ "version": "2.3.2-alpha-3ace273.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
- import { DefaultContext, Maybe, Plugin } from '@envelop/types';
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>) => GraphQLSchema) => Plugin;
5
- export declare const useAsyncSchema: (schemaPromise: Promise<GraphQLSchema>) => Plugin;
4
+ export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => PromiseOrValue<GraphQLSchema>) => Plugin;
5
+ export declare const useAsyncSchema: (schema$: PromiseOrValue<GraphQLSchema>) => Plugin;
package/utils.d.ts CHANGED
@@ -31,3 +31,4 @@ export declare function isAsyncIterable<TType>(maybeAsyncIterable: unknown): may
31
31
  export declare function handleStreamOrSingleExecutionResult<ContextType = DefaultContext>(payload: OnExecuteDoneEventPayload<ContextType>, fn: OnExecuteDoneHookResultOnNextHook<ContextType>): void | OnExecuteDoneHookResult<ContextType>;
32
32
  export declare function finalAsyncIterator<TInput>(source: AsyncIterable<TInput>, onFinal: () => void): AsyncGenerator<TInput>;
33
33
  export declare function errorAsyncIterator<TInput>(source: AsyncIterable<TInput>, onError: (err: unknown) => void): AsyncGenerator<TInput>;
34
+ export declare function isPromise<T>(obj: any): obj is Promise<T>;