@envelop/core 2.3.1 → 2.3.2-alpha-e3ced2d.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;
@@ -1088,18 +1092,47 @@ const useSchema = (schema) => {
1088
1092
  };
1089
1093
  };
1090
1094
  const useLazyLoadedSchema = (schemaLoader) => {
1095
+ let schemaSet$;
1091
1096
  return {
1092
1097
  onEnveloped({ setSchema, context }) {
1093
- setSchema(schemaLoader(context));
1098
+ const schema$ = schemaLoader(context);
1099
+ if (isPromise(schema$)) {
1100
+ schemaSet$ = schema$.then(schemaObj => {
1101
+ setSchema(schemaObj);
1102
+ });
1103
+ }
1104
+ else {
1105
+ setSchema(schema$);
1106
+ }
1107
+ },
1108
+ onExecute() {
1109
+ if (schemaSet$) {
1110
+ return schemaSet$;
1111
+ }
1112
+ return undefined;
1094
1113
  },
1095
1114
  };
1096
1115
  };
1097
- const useAsyncSchema = (schemaPromise) => {
1116
+ const useAsyncSchema = (schema$) => {
1117
+ let schemaSet$;
1098
1118
  return {
1099
1119
  onPluginInit({ setSchema }) {
1100
- schemaPromise.then(schemaObj => {
1101
- setSchema(schemaObj);
1102
- });
1120
+ if (isPromise(schema$)) {
1121
+ schemaSet$ = schema$.then(schemaObj => {
1122
+ setSchema(schemaObj);
1123
+ });
1124
+ }
1125
+ else {
1126
+ setSchema(schema$);
1127
+ }
1128
+ },
1129
+ onContextBuilding() {
1130
+ return () => {
1131
+ if (schemaSet$) {
1132
+ return schemaSet$;
1133
+ }
1134
+ return undefined;
1135
+ };
1103
1136
  },
1104
1137
  };
1105
1138
  };
@@ -1296,6 +1329,7 @@ exports.isIntrospectionOperation = isIntrospectionOperation;
1296
1329
  exports.isIntrospectionOperationString = isIntrospectionOperationString;
1297
1330
  exports.isOperationDefinition = isOperationDefinition;
1298
1331
  exports.isPluginEnabled = isPluginEnabled;
1332
+ exports.isPromise = isPromise;
1299
1333
  exports.makeExecute = makeExecute;
1300
1334
  exports.makeSubscribe = makeSubscribe;
1301
1335
  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;
@@ -1084,18 +1088,47 @@ const useSchema = (schema) => {
1084
1088
  };
1085
1089
  };
1086
1090
  const useLazyLoadedSchema = (schemaLoader) => {
1091
+ let schemaSet$;
1087
1092
  return {
1088
1093
  onEnveloped({ setSchema, context }) {
1089
- setSchema(schemaLoader(context));
1094
+ const schema$ = schemaLoader(context);
1095
+ if (isPromise(schema$)) {
1096
+ schemaSet$ = schema$.then(schemaObj => {
1097
+ setSchema(schemaObj);
1098
+ });
1099
+ }
1100
+ else {
1101
+ setSchema(schema$);
1102
+ }
1103
+ },
1104
+ onExecute() {
1105
+ if (schemaSet$) {
1106
+ return schemaSet$;
1107
+ }
1108
+ return undefined;
1090
1109
  },
1091
1110
  };
1092
1111
  };
1093
- const useAsyncSchema = (schemaPromise) => {
1112
+ const useAsyncSchema = (schema$) => {
1113
+ let schemaSet$;
1094
1114
  return {
1095
1115
  onPluginInit({ setSchema }) {
1096
- schemaPromise.then(schemaObj => {
1097
- setSchema(schemaObj);
1098
- });
1116
+ if (isPromise(schema$)) {
1117
+ schemaSet$ = schema$.then(schemaObj => {
1118
+ setSchema(schemaObj);
1119
+ });
1120
+ }
1121
+ else {
1122
+ setSchema(schema$);
1123
+ }
1124
+ },
1125
+ onContextBuilding() {
1126
+ return () => {
1127
+ if (schemaSet$) {
1128
+ return schemaSet$;
1129
+ }
1130
+ return undefined;
1131
+ };
1099
1132
  },
1100
1133
  };
1101
1134
  };
@@ -1269,4 +1302,4 @@ const useImmediateIntrospection = () => {
1269
1302
  };
1270
1303
  };
1271
1304
 
1272
- 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 };
1305
+ 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.1",
3
+ "version": "2.3.2-alpha-e3ced2d.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>;