@envelop/core 2.3.1 → 2.3.2-alpha-a0c2439.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 +77 -6
- package/index.mjs +77 -7
- package/package.json +1 -1
- package/plugins/use-schema.d.ts +3 -3
- package/utils.d.ts +1 -0
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,85 @@ const useSchema = (schema) => {
|
|
|
1087
1091
|
},
|
|
1088
1092
|
};
|
|
1089
1093
|
};
|
|
1090
|
-
const
|
|
1094
|
+
const VALIDATE_FN = Symbol('VALIDATE_FN');
|
|
1095
|
+
const useLazyLoadedSchema = (schemaLoader, validateSchema = true) => {
|
|
1096
|
+
let schemaSet$;
|
|
1091
1097
|
return {
|
|
1092
1098
|
onEnveloped({ setSchema, context }) {
|
|
1093
|
-
|
|
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
|
+
if (validateSchema) {
|
|
1112
|
+
extendContext({
|
|
1113
|
+
[VALIDATE_FN]: validateFn,
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
setValidationFn(() => []);
|
|
1117
|
+
}
|
|
1118
|
+
},
|
|
1119
|
+
onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
|
|
1120
|
+
if (schemaSet$) {
|
|
1121
|
+
const validateFn = contextValue[VALIDATE_FN];
|
|
1122
|
+
const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema, document);
|
|
1123
|
+
if (errors === null || errors === void 0 ? void 0 : errors.length) {
|
|
1124
|
+
setResultAndStopExecution({
|
|
1125
|
+
errors,
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
else {
|
|
1129
|
+
return schemaSet$;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return undefined;
|
|
1094
1133
|
},
|
|
1095
1134
|
};
|
|
1096
1135
|
};
|
|
1097
|
-
const useAsyncSchema = (
|
|
1136
|
+
const useAsyncSchema = (schema$, validateSchema = true) => {
|
|
1137
|
+
let schemaSet$;
|
|
1098
1138
|
return {
|
|
1099
1139
|
onPluginInit({ setSchema }) {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1140
|
+
if (isPromise(schema$)) {
|
|
1141
|
+
schemaSet$ = schema$.then(schemaObj => {
|
|
1142
|
+
setSchema(schemaObj);
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
else {
|
|
1146
|
+
setSchema(schema$);
|
|
1147
|
+
}
|
|
1148
|
+
},
|
|
1149
|
+
onValidate({ validateFn, setValidationFn, extendContext }) {
|
|
1150
|
+
if (schemaSet$) {
|
|
1151
|
+
if (validateSchema) {
|
|
1152
|
+
extendContext({
|
|
1153
|
+
[VALIDATE_FN]: validateFn,
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
setValidationFn(() => []);
|
|
1157
|
+
}
|
|
1158
|
+
},
|
|
1159
|
+
onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
|
|
1160
|
+
if (schemaSet$) {
|
|
1161
|
+
const validateFn = contextValue[VALIDATE_FN];
|
|
1162
|
+
const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema, document);
|
|
1163
|
+
if (errors === null || errors === void 0 ? void 0 : errors.length) {
|
|
1164
|
+
setResultAndStopExecution({
|
|
1165
|
+
errors,
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
else {
|
|
1169
|
+
return schemaSet$;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
return undefined;
|
|
1103
1173
|
},
|
|
1104
1174
|
};
|
|
1105
1175
|
};
|
|
@@ -1296,6 +1366,7 @@ exports.isIntrospectionOperation = isIntrospectionOperation;
|
|
|
1296
1366
|
exports.isIntrospectionOperationString = isIntrospectionOperationString;
|
|
1297
1367
|
exports.isOperationDefinition = isOperationDefinition;
|
|
1298
1368
|
exports.isPluginEnabled = isPluginEnabled;
|
|
1369
|
+
exports.isPromise = isPromise;
|
|
1299
1370
|
exports.makeExecute = makeExecute;
|
|
1300
1371
|
exports.makeSubscribe = makeSubscribe;
|
|
1301
1372
|
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,85 @@ const useSchema = (schema) => {
|
|
|
1083
1087
|
},
|
|
1084
1088
|
};
|
|
1085
1089
|
};
|
|
1086
|
-
const
|
|
1090
|
+
const VALIDATE_FN = Symbol('VALIDATE_FN');
|
|
1091
|
+
const useLazyLoadedSchema = (schemaLoader, validateSchema = true) => {
|
|
1092
|
+
let schemaSet$;
|
|
1087
1093
|
return {
|
|
1088
1094
|
onEnveloped({ setSchema, context }) {
|
|
1089
|
-
|
|
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
|
+
if (validateSchema) {
|
|
1108
|
+
extendContext({
|
|
1109
|
+
[VALIDATE_FN]: validateFn,
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
setValidationFn(() => []);
|
|
1113
|
+
}
|
|
1114
|
+
},
|
|
1115
|
+
onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) {
|
|
1116
|
+
if (schemaSet$) {
|
|
1117
|
+
const validateFn = contextValue[VALIDATE_FN];
|
|
1118
|
+
const errors = validateFn === null || validateFn === void 0 ? void 0 : validateFn(schema, document);
|
|
1119
|
+
if (errors === null || errors === void 0 ? void 0 : errors.length) {
|
|
1120
|
+
setResultAndStopExecution({
|
|
1121
|
+
errors,
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
else {
|
|
1125
|
+
return schemaSet$;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
return undefined;
|
|
1090
1129
|
},
|
|
1091
1130
|
};
|
|
1092
1131
|
};
|
|
1093
|
-
const useAsyncSchema = (
|
|
1132
|
+
const useAsyncSchema = (schema$, validateSchema = true) => {
|
|
1133
|
+
let schemaSet$;
|
|
1094
1134
|
return {
|
|
1095
1135
|
onPluginInit({ setSchema }) {
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1136
|
+
if (isPromise(schema$)) {
|
|
1137
|
+
schemaSet$ = schema$.then(schemaObj => {
|
|
1138
|
+
setSchema(schemaObj);
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
else {
|
|
1142
|
+
setSchema(schema$);
|
|
1143
|
+
}
|
|
1144
|
+
},
|
|
1145
|
+
onValidate({ validateFn, setValidationFn, extendContext }) {
|
|
1146
|
+
if (schemaSet$) {
|
|
1147
|
+
if (validateSchema) {
|
|
1148
|
+
extendContext({
|
|
1149
|
+
[VALIDATE_FN]: validateFn,
|
|
1150
|
+
});
|
|
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 === null || validateFn === void 0 ? void 0 : 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;
|
|
1099
1169
|
},
|
|
1100
1170
|
};
|
|
1101
1171
|
};
|
|
@@ -1269,4 +1339,4 @@ const useImmediateIntrospection = () => {
|
|
|
1269
1339
|
};
|
|
1270
1340
|
};
|
|
1271
1341
|
|
|
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 };
|
|
1342
|
+
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
package/plugins/use-schema.d.ts
CHANGED
|
@@ -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: (
|
|
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;
|
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>;
|