@envelop/core 2.2.0 → 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;
@@ -486,8 +490,9 @@ function createEnvelopOrchestrator(plugins) {
486
490
  const customContextFactory = beforeCallbacks.context.length
487
491
  ? initialContext => async (orchestratorCtx) => {
488
492
  const afterCalls = [];
493
+ // In order to have access to the "last working" context object we keep this outside of the try block:
494
+ let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
489
495
  try {
490
- let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
491
496
  let isBreakingContextBuilding = false;
492
497
  for (const onContext of beforeCallbacks.context) {
493
498
  const afterHookResult = await onContext({
@@ -520,6 +525,7 @@ function createEnvelopOrchestrator(plugins) {
520
525
  let error = err;
521
526
  for (const errorCb of contextErrorHandlers) {
522
527
  errorCb({
528
+ context,
523
529
  error,
524
530
  setError: err => {
525
531
  error = err;
@@ -1086,18 +1092,47 @@ const useSchema = (schema) => {
1086
1092
  };
1087
1093
  };
1088
1094
  const useLazyLoadedSchema = (schemaLoader) => {
1095
+ let schemaSet$;
1089
1096
  return {
1090
1097
  onEnveloped({ setSchema, context }) {
1091
- 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;
1092
1113
  },
1093
1114
  };
1094
1115
  };
1095
- const useAsyncSchema = (schemaPromise) => {
1116
+ const useAsyncSchema = (schema$) => {
1117
+ let schemaSet$;
1096
1118
  return {
1097
1119
  onPluginInit({ setSchema }) {
1098
- schemaPromise.then(schemaObj => {
1099
- setSchema(schemaObj);
1100
- });
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
+ };
1101
1136
  },
1102
1137
  };
1103
1138
  };
@@ -1227,9 +1262,15 @@ const useMaskedErrors = (opts) => {
1227
1262
 
1228
1263
  const OnNonIntrospectionFieldReachedValidationRule = (onNonIntrospectionField) => ctx => {
1229
1264
  const rootQueryType = ctx.getSchema().getQueryType();
1265
+ const rootMutationType = ctx.getSchema().getMutationType();
1266
+ const rootSubscriptionType = ctx.getSchema().getSubscriptionType();
1230
1267
  return {
1231
1268
  Field(field) {
1232
- if (ctx.getParentType() === rootQueryType && !field.name.value.startsWith('__')) {
1269
+ const parentType = ctx.getParentType();
1270
+ const isQuery = parentType === rootQueryType;
1271
+ const isMutation = parentType === rootMutationType;
1272
+ const isSubscription = parentType === rootSubscriptionType;
1273
+ if ((isQuery && !field.name.value.startsWith('__')) || isMutation || isSubscription) {
1233
1274
  onNonIntrospectionField();
1234
1275
  return graphql.BREAK;
1235
1276
  }
@@ -1288,6 +1329,7 @@ exports.isIntrospectionOperation = isIntrospectionOperation;
1288
1329
  exports.isIntrospectionOperationString = isIntrospectionOperationString;
1289
1330
  exports.isOperationDefinition = isOperationDefinition;
1290
1331
  exports.isPluginEnabled = isPluginEnabled;
1332
+ exports.isPromise = isPromise;
1291
1333
  exports.makeExecute = makeExecute;
1292
1334
  exports.makeSubscribe = makeSubscribe;
1293
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;
@@ -482,8 +486,9 @@ function createEnvelopOrchestrator(plugins) {
482
486
  const customContextFactory = beforeCallbacks.context.length
483
487
  ? initialContext => async (orchestratorCtx) => {
484
488
  const afterCalls = [];
489
+ // In order to have access to the "last working" context object we keep this outside of the try block:
490
+ let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
485
491
  try {
486
- let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
487
492
  let isBreakingContextBuilding = false;
488
493
  for (const onContext of beforeCallbacks.context) {
489
494
  const afterHookResult = await onContext({
@@ -516,6 +521,7 @@ function createEnvelopOrchestrator(plugins) {
516
521
  let error = err;
517
522
  for (const errorCb of contextErrorHandlers) {
518
523
  errorCb({
524
+ context,
519
525
  error,
520
526
  setError: err => {
521
527
  error = err;
@@ -1082,18 +1088,47 @@ const useSchema = (schema) => {
1082
1088
  };
1083
1089
  };
1084
1090
  const useLazyLoadedSchema = (schemaLoader) => {
1091
+ let schemaSet$;
1085
1092
  return {
1086
1093
  onEnveloped({ setSchema, context }) {
1087
- 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;
1088
1109
  },
1089
1110
  };
1090
1111
  };
1091
- const useAsyncSchema = (schemaPromise) => {
1112
+ const useAsyncSchema = (schema$) => {
1113
+ let schemaSet$;
1092
1114
  return {
1093
1115
  onPluginInit({ setSchema }) {
1094
- schemaPromise.then(schemaObj => {
1095
- setSchema(schemaObj);
1096
- });
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
+ };
1097
1132
  },
1098
1133
  };
1099
1134
  };
@@ -1223,9 +1258,15 @@ const useMaskedErrors = (opts) => {
1223
1258
 
1224
1259
  const OnNonIntrospectionFieldReachedValidationRule = (onNonIntrospectionField) => ctx => {
1225
1260
  const rootQueryType = ctx.getSchema().getQueryType();
1261
+ const rootMutationType = ctx.getSchema().getMutationType();
1262
+ const rootSubscriptionType = ctx.getSchema().getSubscriptionType();
1226
1263
  return {
1227
1264
  Field(field) {
1228
- if (ctx.getParentType() === rootQueryType && !field.name.value.startsWith('__')) {
1265
+ const parentType = ctx.getParentType();
1266
+ const isQuery = parentType === rootQueryType;
1267
+ const isMutation = parentType === rootMutationType;
1268
+ const isSubscription = parentType === rootSubscriptionType;
1269
+ if ((isQuery && !field.name.value.startsWith('__')) || isMutation || isSubscription) {
1229
1270
  onNonIntrospectionField();
1230
1271
  return BREAK;
1231
1272
  }
@@ -1261,4 +1302,4 @@ const useImmediateIntrospection = () => {
1261
1302
  };
1262
1303
  };
1263
1304
 
1264
- 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,12 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "2.2.0",
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"
7
7
  },
8
8
  "dependencies": {
9
- "@envelop/types": "2.1.0"
9
+ "@envelop/types": "2.2.0"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -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>;