@envelop/core
Advanced tools
Comparing version
@@ -5,2 +5,3 @@ "use strict"; | ||
const instruments_1 = require("@envelop/instruments"); | ||
const promise_helpers_1 = require("@whatwg-node/promise-helpers"); | ||
const document_string_map_js_1 = require("./document-string-map.js"); | ||
@@ -46,11 +47,10 @@ const utils_js_1 = require("./utils.js"); | ||
const pluginsToAdd = []; | ||
plugin.onPluginInit && | ||
plugin.onPluginInit({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
pluginsToAdd.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
registerContextErrorHandler: handler => contextErrorHandlers.push(handler), | ||
}); | ||
plugin.onPluginInit?.({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
pluginsToAdd.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
registerContextErrorHandler: handler => contextErrorHandlers.push(handler), | ||
}); | ||
pluginsToAdd.length && plugins.splice(i + 1, 0, ...pluginsToAdd); | ||
@@ -113,3 +113,5 @@ } | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
if (afterFn) { | ||
afterCalls.push(afterFn); | ||
} | ||
} | ||
@@ -207,3 +209,3 @@ if (result === null) { | ||
const customContextFactory = beforeCallbacks.context.length | ||
? initialContext => async (orchestratorCtx) => { | ||
? initialContext => orchestratorCtx => { | ||
const afterCalls = []; | ||
@@ -215,32 +217,23 @@ // In order to have access to the "last working" context object we keep this outside of the try block: | ||
} | ||
try { | ||
let isBreakingContextBuilding = false; | ||
for (const onContext of beforeCallbacks.context) { | ||
const afterHookResult = await onContext({ | ||
let isBreakingContextBuilding = false; | ||
return (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(beforeCallbacks.context, (onContext, stopEarly) => onContext({ | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
breakContextBuilding: () => { | ||
isBreakingContextBuilding = true; | ||
stopEarly(); | ||
}, | ||
}), afterCalls), () => { | ||
if (!isBreakingContextBuilding) { | ||
return (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(afterCalls, afterCb => afterCb({ | ||
context, | ||
extendContext: extension => { | ||
extendContext(extension) { | ||
Object.assign(context, extension); | ||
}, | ||
breakContextBuilding: () => { | ||
isBreakingContextBuilding = true; | ||
}, | ||
}); | ||
if (typeof afterHookResult === 'function') { | ||
afterCalls.push(afterHookResult); | ||
} | ||
if (isBreakingContextBuilding === true) { | ||
break; | ||
} | ||
})), () => context); | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
}); | ||
} | ||
return context; | ||
} | ||
catch (err) { | ||
}, err => { | ||
let error = err; | ||
@@ -257,3 +250,3 @@ for (const errorCb of contextErrorHandlers) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
@@ -268,99 +261,83 @@ : initialContext => orchestratorCtx => { | ||
const customSubscribe = useCustomSubscribe | ||
? (0, utils_js_1.makeSubscribe)(async (args) => { | ||
? (0, utils_js_1.makeSubscribe)(args => { | ||
let subscribeFn = subscribe; | ||
const afterCalls = []; | ||
const subscribeErrorHandlers = []; | ||
const afterCallbacks = []; | ||
const context = args.contextValue || {}; | ||
let result; | ||
for (const onSubscribe of beforeCallbacks.subscribe) { | ||
const after = await onSubscribe({ | ||
subscribeFn, | ||
setSubscribeFn: newSubscribeFn => { | ||
subscribeFn = newSubscribeFn; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
args: args, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
}, | ||
}); | ||
if (after) { | ||
if (after.onSubscribeResult) { | ||
afterCalls.push(after.onSubscribeResult); | ||
return (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(beforeCallbacks.subscribe, (onSubscribe, endEarly) => onSubscribe({ | ||
subscribeFn, | ||
setSubscribeFn: newSubscribeFn => { | ||
subscribeFn = newSubscribeFn; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
args: args, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
endEarly(); | ||
}, | ||
}), afterCallbacks), () => { | ||
const afterCalls = []; | ||
const subscribeErrorHandlers = []; | ||
for (const { onSubscribeResult, onSubscribeError } of afterCallbacks) { | ||
if (onSubscribeResult) { | ||
afterCalls.push(onSubscribeResult); | ||
} | ||
if (after.onSubscribeError) { | ||
subscribeErrorHandlers.push(after.onSubscribeError); | ||
if (onSubscribeError) { | ||
subscribeErrorHandlers.push(onSubscribeError); | ||
} | ||
} | ||
if (result !== undefined) { | ||
break; | ||
} | ||
} | ||
if (result === undefined) { | ||
result = await subscribeFn({ | ||
...args, | ||
contextValue: context, | ||
// Casted for GraphQL.js 15 compatibility | ||
// Can be removed once we drop support for GraphQL.js 15 | ||
}); | ||
} | ||
if (!result) { | ||
return; | ||
} | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
return (0, promise_helpers_1.handleMaybePromise)(() => result || subscribeFn(args), result => { | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.mapAsyncIterator)(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ | ||
if (onNextHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.mapAsyncIterator)(result, (result) => (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(onNextHandler, onNext => onNext({ | ||
args: args, | ||
result, | ||
setResult: newResult => (result = newResult), | ||
})), () => result)); | ||
} | ||
if (onEndHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.finalAsyncIterator)(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.finalAsyncIterator)(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
if (subscribeErrorHandlers.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.errorAsyncIterator)(result, err => { | ||
let error = err; | ||
for (const handler of subscribeErrorHandlers) { | ||
handler({ | ||
error, | ||
setError: err => { | ||
error = err; | ||
}, | ||
if (subscribeErrorHandlers.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.errorAsyncIterator)(result, err => { | ||
let error = err; | ||
for (const handler of subscribeErrorHandlers) { | ||
handler({ | ||
error, | ||
setError: err => { | ||
error = err; | ||
}, | ||
}); | ||
} | ||
throw error; | ||
}); | ||
} | ||
throw error; | ||
return result; | ||
}); | ||
} | ||
return result; | ||
}); | ||
}) | ||
@@ -370,81 +347,66 @@ : (0, utils_js_1.makeSubscribe)(subscribe); | ||
const customExecute = useCustomExecute | ||
? (0, utils_js_1.makeExecute)(async (args) => { | ||
? (0, utils_js_1.makeExecute)(args => { | ||
let executeFn = execute; | ||
let result; | ||
const afterCalls = []; | ||
const afterDoneCalls = []; | ||
const context = args.contextValue || {}; | ||
for (const onExecute of beforeCallbacks.execute) { | ||
const after = await onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
Object.assign(context, extension); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args: args, | ||
}); | ||
if (after?.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
if (result !== undefined) { | ||
break; | ||
} | ||
} | ||
if (result === undefined) { | ||
result = (await executeFn({ | ||
return (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(beforeCallbacks.execute, (onExecute, endEarly) => onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
endEarly(); | ||
}, | ||
context, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
Object.assign(context, extension); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args: args, | ||
}), afterCalls), () => (0, promise_helpers_1.handleMaybePromise)(() => result || | ||
executeFn({ | ||
...args, | ||
contextValue: context, | ||
})); | ||
} | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = await afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
}), result => (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsync)(afterCalls, afterCb => afterCb.onExecuteDone?.({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}), afterDoneCalls), () => { | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const { onNext, onEnd } of afterDoneCalls) { | ||
if (onNext) { | ||
onNextHandler.push(onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
if (onEnd) { | ||
onEndHandler.push(onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.mapAsyncIterator)(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.finalAsyncIterator)(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
if (onNextHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.mapAsyncIterator)(result, result => (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.iterateAsyncVoid)(onNextHandler, onNext => onNext({ | ||
args: args, | ||
result: result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
})), () => result)); | ||
} | ||
if (onEndHandler.length && (0, utils_js_1.isAsyncIterable)(result)) { | ||
result = (0, utils_js_1.finalAsyncIterator)(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
}))); | ||
}) | ||
@@ -457,7 +419,6 @@ : (0, utils_js_1.makeExecute)(execute); | ||
for (const [i, plugin] of plugins.entries()) { | ||
plugin.onSchemaChange && | ||
plugin.onSchemaChange({ | ||
schema, | ||
replaceSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
plugin.onSchemaChange?.({ | ||
schema, | ||
replaceSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
} | ||
@@ -464,0 +425,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useExtendContext = void 0; | ||
const promise_helpers_1 = require("@whatwg-node/promise-helpers"); | ||
const useExtendContext = (contextFactory) => ({ | ||
async onContextBuilding({ context, extendContext }) { | ||
extendContext((await contextFactory(context))); | ||
onContextBuilding({ context, extendContext }) { | ||
return (0, promise_helpers_1.handleMaybePromise)(() => contextFactory(context), result => extendContext(result)); | ||
}, | ||
}); | ||
exports.useExtendContext = useExtendContext; |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.errorAsyncIterator = errorAsyncIterator; | ||
const promise_helpers_1 = require("@whatwg-node/promise-helpers"); | ||
exports.envelopIsIntrospectionSymbol = Symbol('ENVELOP_IS_INTROSPECTION'); | ||
@@ -33,4 +34,4 @@ function isIntrospectionOperationString(operation) { | ||
exports.makeSubscribe = makeSubscribe; | ||
var promise_helpers_1 = require("@whatwg-node/promise-helpers"); | ||
Object.defineProperty(exports, "mapAsyncIterator", { enumerable: true, get: function () { return promise_helpers_1.mapAsyncIterator; } }); | ||
var promise_helpers_2 = require("@whatwg-node/promise-helpers"); | ||
Object.defineProperty(exports, "mapAsyncIterator", { enumerable: true, get: function () { return promise_helpers_2.mapAsyncIterator; } }); | ||
function getExecuteArgs(args) { | ||
@@ -91,11 +92,12 @@ return args.length === 1 | ||
}, | ||
async next() { | ||
const result = await iterator.next(); | ||
if (result.done && isDone === false) { | ||
isDone = true; | ||
onFinal(); | ||
} | ||
return result; | ||
next() { | ||
return iterator.next().then(result => { | ||
if (result.done && isDone === false) { | ||
isDone = true; | ||
onFinal(); | ||
} | ||
return result; | ||
}); | ||
}, | ||
async return() { | ||
return() { | ||
const promise = iterator.return?.(); | ||
@@ -106,8 +108,8 @@ if (isDone === false) { | ||
} | ||
return promise ? await promise : { done: true, value: undefined }; | ||
return promise || (0, promise_helpers_1.fakePromise)({ done: true, value: undefined }); | ||
}, | ||
async throw(error) { | ||
throw(error) { | ||
const promise = iterator.throw?.(); | ||
if (promise) { | ||
return await promise; | ||
return promise; | ||
} | ||
@@ -127,19 +129,16 @@ // if the source has no throw method we just re-throw error | ||
}, | ||
async next() { | ||
try { | ||
return await iterator.next(); | ||
} | ||
catch (error) { | ||
next() { | ||
return iterator.next().catch(error => { | ||
onError(error); | ||
return { done: true, value: undefined }; | ||
} | ||
}); | ||
}, | ||
async return() { | ||
return() { | ||
const promise = iterator.return?.(); | ||
return promise ? await promise : { done: true, value: undefined }; | ||
return promise || (0, promise_helpers_1.fakePromise)({ done: true, value: undefined }); | ||
}, | ||
async throw(error) { | ||
throw(error) { | ||
const promise = iterator.throw?.(); | ||
if (promise) { | ||
return await promise; | ||
return promise; | ||
} | ||
@@ -153,4 +152,4 @@ // if the source has no throw method we just re-throw error | ||
} | ||
var promise_helpers_2 = require("@whatwg-node/promise-helpers"); | ||
Object.defineProperty(exports, "mapMaybePromise", { enumerable: true, get: function () { return promise_helpers_2.mapMaybePromise; } }); | ||
Object.defineProperty(exports, "isPromise", { enumerable: true, get: function () { return promise_helpers_2.isPromise; } }); | ||
var promise_helpers_3 = require("@whatwg-node/promise-helpers"); | ||
Object.defineProperty(exports, "mapMaybePromise", { enumerable: true, get: function () { return promise_helpers_3.mapMaybePromise; } }); | ||
Object.defineProperty(exports, "isPromise", { enumerable: true, get: function () { return promise_helpers_3.isPromise; } }); |
import { chain } from '@envelop/instruments'; | ||
import { handleMaybePromise, iterateAsync, iterateAsyncVoid } from '@whatwg-node/promise-helpers'; | ||
import { documentStringMap } from './document-string-map.js'; | ||
@@ -42,11 +43,10 @@ import { errorAsyncIterator, finalAsyncIterator, isAsyncIterable, makeExecute, makeSubscribe, mapAsyncIterator, } from './utils.js'; | ||
const pluginsToAdd = []; | ||
plugin.onPluginInit && | ||
plugin.onPluginInit({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
pluginsToAdd.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
registerContextErrorHandler: handler => contextErrorHandlers.push(handler), | ||
}); | ||
plugin.onPluginInit?.({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
pluginsToAdd.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
registerContextErrorHandler: handler => contextErrorHandlers.push(handler), | ||
}); | ||
pluginsToAdd.length && plugins.splice(i + 1, 0, ...pluginsToAdd); | ||
@@ -109,3 +109,5 @@ } | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
if (afterFn) { | ||
afterCalls.push(afterFn); | ||
} | ||
} | ||
@@ -203,3 +205,3 @@ if (result === null) { | ||
const customContextFactory = beforeCallbacks.context.length | ||
? initialContext => async (orchestratorCtx) => { | ||
? initialContext => orchestratorCtx => { | ||
const afterCalls = []; | ||
@@ -211,32 +213,23 @@ // In order to have access to the "last working" context object we keep this outside of the try block: | ||
} | ||
try { | ||
let isBreakingContextBuilding = false; | ||
for (const onContext of beforeCallbacks.context) { | ||
const afterHookResult = await onContext({ | ||
let isBreakingContextBuilding = false; | ||
return handleMaybePromise(() => iterateAsync(beforeCallbacks.context, (onContext, stopEarly) => onContext({ | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
breakContextBuilding: () => { | ||
isBreakingContextBuilding = true; | ||
stopEarly(); | ||
}, | ||
}), afterCalls), () => { | ||
if (!isBreakingContextBuilding) { | ||
return handleMaybePromise(() => iterateAsync(afterCalls, afterCb => afterCb({ | ||
context, | ||
extendContext: extension => { | ||
extendContext(extension) { | ||
Object.assign(context, extension); | ||
}, | ||
breakContextBuilding: () => { | ||
isBreakingContextBuilding = true; | ||
}, | ||
}); | ||
if (typeof afterHookResult === 'function') { | ||
afterCalls.push(afterHookResult); | ||
} | ||
if (isBreakingContextBuilding === true) { | ||
break; | ||
} | ||
})), () => context); | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
}); | ||
} | ||
return context; | ||
} | ||
catch (err) { | ||
}, err => { | ||
let error = err; | ||
@@ -253,3 +246,3 @@ for (const errorCb of contextErrorHandlers) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
@@ -264,99 +257,83 @@ : initialContext => orchestratorCtx => { | ||
const customSubscribe = useCustomSubscribe | ||
? makeSubscribe(async (args) => { | ||
? makeSubscribe(args => { | ||
let subscribeFn = subscribe; | ||
const afterCalls = []; | ||
const subscribeErrorHandlers = []; | ||
const afterCallbacks = []; | ||
const context = args.contextValue || {}; | ||
let result; | ||
for (const onSubscribe of beforeCallbacks.subscribe) { | ||
const after = await onSubscribe({ | ||
subscribeFn, | ||
setSubscribeFn: newSubscribeFn => { | ||
subscribeFn = newSubscribeFn; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
args: args, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
}, | ||
}); | ||
if (after) { | ||
if (after.onSubscribeResult) { | ||
afterCalls.push(after.onSubscribeResult); | ||
return handleMaybePromise(() => iterateAsync(beforeCallbacks.subscribe, (onSubscribe, endEarly) => onSubscribe({ | ||
subscribeFn, | ||
setSubscribeFn: newSubscribeFn => { | ||
subscribeFn = newSubscribeFn; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
Object.assign(context, extension); | ||
}, | ||
args: args, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
endEarly(); | ||
}, | ||
}), afterCallbacks), () => { | ||
const afterCalls = []; | ||
const subscribeErrorHandlers = []; | ||
for (const { onSubscribeResult, onSubscribeError } of afterCallbacks) { | ||
if (onSubscribeResult) { | ||
afterCalls.push(onSubscribeResult); | ||
} | ||
if (after.onSubscribeError) { | ||
subscribeErrorHandlers.push(after.onSubscribeError); | ||
if (onSubscribeError) { | ||
subscribeErrorHandlers.push(onSubscribeError); | ||
} | ||
} | ||
if (result !== undefined) { | ||
break; | ||
} | ||
} | ||
if (result === undefined) { | ||
result = await subscribeFn({ | ||
...args, | ||
contextValue: context, | ||
// Casted for GraphQL.js 15 compatibility | ||
// Can be removed once we drop support for GraphQL.js 15 | ||
}); | ||
} | ||
if (!result) { | ||
return; | ||
} | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
return handleMaybePromise(() => result || subscribeFn(args), result => { | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, (result) => handleMaybePromise(() => iterateAsync(onNextHandler, onNext => onNext({ | ||
args: args, | ||
result, | ||
setResult: newResult => (result = newResult), | ||
})), () => result)); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
if (subscribeErrorHandlers.length && isAsyncIterable(result)) { | ||
result = errorAsyncIterator(result, err => { | ||
let error = err; | ||
for (const handler of subscribeErrorHandlers) { | ||
handler({ | ||
error, | ||
setError: err => { | ||
error = err; | ||
}, | ||
if (subscribeErrorHandlers.length && isAsyncIterable(result)) { | ||
result = errorAsyncIterator(result, err => { | ||
let error = err; | ||
for (const handler of subscribeErrorHandlers) { | ||
handler({ | ||
error, | ||
setError: err => { | ||
error = err; | ||
}, | ||
}); | ||
} | ||
throw error; | ||
}); | ||
} | ||
throw error; | ||
return result; | ||
}); | ||
} | ||
return result; | ||
}); | ||
}) | ||
@@ -366,81 +343,66 @@ : makeSubscribe(subscribe); | ||
const customExecute = useCustomExecute | ||
? makeExecute(async (args) => { | ||
? makeExecute(args => { | ||
let executeFn = execute; | ||
let result; | ||
const afterCalls = []; | ||
const afterDoneCalls = []; | ||
const context = args.contextValue || {}; | ||
for (const onExecute of beforeCallbacks.execute) { | ||
const after = await onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
}, | ||
context, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
Object.assign(context, extension); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args: args, | ||
}); | ||
if (after?.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
if (result !== undefined) { | ||
break; | ||
} | ||
} | ||
if (result === undefined) { | ||
result = (await executeFn({ | ||
return handleMaybePromise(() => iterateAsync(beforeCallbacks.execute, (onExecute, endEarly) => onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
result = stopResult; | ||
endEarly(); | ||
}, | ||
context, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
Object.assign(context, extension); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args: args, | ||
}), afterCalls), () => handleMaybePromise(() => result || | ||
executeFn({ | ||
...args, | ||
contextValue: context, | ||
})); | ||
} | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
const hookResult = await afterCb({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
}), result => handleMaybePromise(() => iterateAsync(afterCalls, afterCb => afterCb.onExecuteDone?.({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}), afterDoneCalls), () => { | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const { onNext, onEnd } of afterDoneCalls) { | ||
if (onNext) { | ||
onNextHandler.push(onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
if (onEnd) { | ||
onEndHandler.push(onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ | ||
args: args, | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, result => handleMaybePromise(() => iterateAsyncVoid(onNextHandler, onNext => onNext({ | ||
args: args, | ||
result: result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
})), () => result)); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
}))); | ||
}) | ||
@@ -453,7 +415,6 @@ : makeExecute(execute); | ||
for (const [i, plugin] of plugins.entries()) { | ||
plugin.onSchemaChange && | ||
plugin.onSchemaChange({ | ||
schema, | ||
replaceSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
plugin.onSchemaChange?.({ | ||
schema, | ||
replaceSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
} | ||
@@ -460,0 +421,0 @@ } |
@@ -0,5 +1,6 @@ | ||
import { handleMaybePromise } from '@whatwg-node/promise-helpers'; | ||
export const useExtendContext = (contextFactory) => ({ | ||
async onContextBuilding({ context, extendContext }) { | ||
extendContext((await contextFactory(context))); | ||
onContextBuilding({ context, extendContext }) { | ||
return handleMaybePromise(() => contextFactory(context), result => extendContext(result)); | ||
}, | ||
}); |
@@ -0,1 +1,2 @@ | ||
import { fakePromise } from '@whatwg-node/promise-helpers'; | ||
export const envelopIsIntrospectionSymbol = Symbol('ENVELOP_IS_INTROSPECTION'); | ||
@@ -78,11 +79,12 @@ export function isIntrospectionOperationString(operation) { | ||
}, | ||
async next() { | ||
const result = await iterator.next(); | ||
if (result.done && isDone === false) { | ||
isDone = true; | ||
onFinal(); | ||
} | ||
return result; | ||
next() { | ||
return iterator.next().then(result => { | ||
if (result.done && isDone === false) { | ||
isDone = true; | ||
onFinal(); | ||
} | ||
return result; | ||
}); | ||
}, | ||
async return() { | ||
return() { | ||
const promise = iterator.return?.(); | ||
@@ -93,8 +95,8 @@ if (isDone === false) { | ||
} | ||
return promise ? await promise : { done: true, value: undefined }; | ||
return promise || fakePromise({ done: true, value: undefined }); | ||
}, | ||
async throw(error) { | ||
throw(error) { | ||
const promise = iterator.throw?.(); | ||
if (promise) { | ||
return await promise; | ||
return promise; | ||
} | ||
@@ -114,19 +116,16 @@ // if the source has no throw method we just re-throw error | ||
}, | ||
async next() { | ||
try { | ||
return await iterator.next(); | ||
} | ||
catch (error) { | ||
next() { | ||
return iterator.next().catch(error => { | ||
onError(error); | ||
return { done: true, value: undefined }; | ||
} | ||
}); | ||
}, | ||
async return() { | ||
return() { | ||
const promise = iterator.return?.(); | ||
return promise ? await promise : { done: true, value: undefined }; | ||
return promise || fakePromise({ done: true, value: undefined }); | ||
}, | ||
async throw(error) { | ||
throw(error) { | ||
const promise = iterator.throw?.(); | ||
if (promise) { | ||
return await promise; | ||
return promise; | ||
} | ||
@@ -133,0 +132,0 @@ // if the source has no throw method we just re-throw error |
{ | ||
"name": "@envelop/core", | ||
"version": "5.2.0", | ||
"version": "5.2.1-alpha-20250305102026-84e6ced64feb11e4c4601527bab60b91bd3f29d2", | ||
"sideEffects": false, | ||
"dependencies": { | ||
"@whatwg-node/promise-helpers": "^1.0.0", | ||
"@whatwg-node/promise-helpers": "^1.2.1", | ||
"tslib": "^2.5.0", | ||
@@ -8,0 +8,0 @@ "@envelop/instruments": "^1.0.0", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
89451
-0.51%1945
-3.86%2
100%