@envelop/core
Advanced tools
Comparing version 0.1.4 to 0.2.0-alpha-bd31072.0
@@ -1,7 +0,4 @@ | ||
import { GraphQLSchema } from 'graphql'; | ||
import { Envelop, Plugin } from '@envelop/types'; | ||
export declare function envelop(options: { | ||
export declare function envelop({ plugins }: { | ||
plugins: Plugin[]; | ||
extends?: Envelop[]; | ||
initialSchema?: GraphQLSchema; | ||
}): Envelop; |
390
index.cjs.js
@@ -10,7 +10,5 @@ 'use strict'; | ||
const resolversHooksSymbol = Symbol('RESOLVERS_HOOKS'); | ||
function envelop(options) { | ||
let schema = options.initialSchema; | ||
function envelop({ plugins }) { | ||
let schema = null; | ||
let initDone = false; | ||
const childPlugins = (options.extends || []).reduce((prev, child) => [...prev, ...child._plugins], []); | ||
const plugins = [...childPlugins, ...options.plugins]; | ||
const replaceSchema = (newSchema, ignorePluginIndex = -1) => { | ||
@@ -35,12 +33,28 @@ schema = newSchema; | ||
plugin.onPluginInit({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
plugins.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
} | ||
const customParse = (source, parseOptions) => { | ||
let result = null; | ||
let parseFn = graphql.parse; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onParse && | ||
plugin.onParse({ | ||
const onContextBuildingCbs = []; | ||
const onExecuteCbs = []; | ||
const onParseCbs = []; | ||
const onSubscribeCbs = []; | ||
const onValidateCbs = []; | ||
for (const { onContextBuilding, onExecute, onParse, onSubscribe, onValidate } of plugins) { | ||
onContextBuilding && onContextBuildingCbs.push(onContextBuilding); | ||
onExecute && onExecuteCbs.push(onExecute); | ||
onParse && onParseCbs.push(onParse); | ||
onSubscribe && onSubscribeCbs.push(onSubscribe); | ||
onValidate && onValidateCbs.push(onValidate); | ||
} | ||
const customParse = onParseCbs.length | ||
? (source, parseOptions) => { | ||
let result = null; | ||
let parseFn = graphql.parse; | ||
const afterCalls = []; | ||
for (const onParse of onParseCbs) { | ||
const afterFn = onParse({ | ||
params: { source, options: parseOptions }, | ||
@@ -55,33 +69,37 @@ parseFn, | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
if (result === null) { | ||
try { | ||
result = parseFn(source, parseOptions); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
catch (e) { | ||
result = e; | ||
if (result === null) { | ||
try { | ||
result = parseFn(source, parseOptions); | ||
} | ||
catch (e) { | ||
result = e; | ||
} | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
replaceParseResult: newResult => { | ||
result = newResult; | ||
}, | ||
result, | ||
}); | ||
} | ||
if (result === null) { | ||
throw new Error(`Failed to parse document.`); | ||
} | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
return result; | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
replaceParseResult: newResult => { | ||
result = newResult; | ||
}, | ||
result, | ||
}); | ||
} | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
return result; | ||
}; | ||
const customValidate = (schema, documentAST, rules, typeInfo, validationOptions) => { | ||
let actualRules = rules ? [...rules] : undefined; | ||
let validateFn = graphql.validate; | ||
let result = null; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onValidate && | ||
plugin.onValidate({ | ||
: graphql.parse; | ||
const customValidate = onValidateCbs.length | ||
? (schema, documentAST, rules, typeInfo, validationOptions) => { | ||
let actualRules = rules ? [...rules] : undefined; | ||
let validateFn = graphql.validate; | ||
let result = null; | ||
const afterCalls = []; | ||
for (const onValidate of onValidateCbs) { | ||
const afterFn = onValidate({ | ||
params: { | ||
@@ -108,19 +126,20 @@ schema, | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
if (!result) { | ||
result = validateFn(schema, documentAST, actualRules, typeInfo, validationOptions); | ||
} | ||
const valid = result.length === 0; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ valid, result }); | ||
} | ||
return result; | ||
} | ||
if (result === null) { | ||
result = validateFn(schema, documentAST, actualRules, typeInfo, validationOptions); | ||
} | ||
const valid = result.length === 0; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ valid, result }); | ||
} | ||
return result; | ||
}; | ||
const customContextFactory = async (initialContext) => { | ||
let context = initialContext; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onContextBuilding && | ||
(await plugin.onContextBuilding({ | ||
: graphql.validate; | ||
const customContextFactory = onContextBuildingCbs.length | ||
? async (initialContext) => { | ||
let context = initialContext; | ||
const afterCalls = []; | ||
for (const onContext of onContextBuildingCbs) { | ||
const afterFn = await onContext({ | ||
context, | ||
@@ -138,12 +157,11 @@ extendContext: extension => { | ||
}, | ||
})); | ||
afterFn && afterCalls.push(afterFn); | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ context }); | ||
} | ||
return context; | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ context }); | ||
} | ||
return context; | ||
}; | ||
const beforeExecuteCalls = plugins.filter(p => p.onExecute); | ||
const beforeSubscribeCalls = plugins.filter(p => p.onSubscribe); | ||
: (ctx) => ctx; | ||
const customSubscribe = async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) => { | ||
@@ -153,3 +171,3 @@ const args = argsOrSchema instanceof graphql.GraphQLSchema | ||
schema: argsOrSchema, | ||
document, | ||
document: document, | ||
rootValue, | ||
@@ -167,4 +185,4 @@ contextValue, | ||
let context = args.contextValue; | ||
for (const plugin of beforeSubscribeCalls) { | ||
const after = plugin.onSubscribe({ | ||
for (const onSubscribe of onSubscribeCbs) { | ||
const after = onSubscribe({ | ||
subscribeFn, | ||
@@ -213,75 +231,77 @@ setSubscribeFn: newSubscribeFn => { | ||
}; | ||
const customExecute = async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => { | ||
const args = argsOrSchema instanceof graphql.GraphQLSchema | ||
? { | ||
schema: argsOrSchema, | ||
document, | ||
rootValue, | ||
contextValue, | ||
variableValues, | ||
operationName, | ||
fieldResolver, | ||
typeResolver, | ||
} | ||
: argsOrSchema; | ||
const onResolversHandlers = []; | ||
let executeFn = graphql.execute; | ||
let result; | ||
const afterCalls = []; | ||
let context = args.contextValue; | ||
for (const plugin of beforeExecuteCalls) { | ||
let stopCalled = false; | ||
const after = plugin.onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
stopCalled = true; | ||
result = stopResult; | ||
}, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
context = { | ||
...(context || {}), | ||
...extension, | ||
}; | ||
const customExecute = onExecuteCbs.length | ||
? async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => { | ||
const args = argsOrSchema instanceof graphql.GraphQLSchema | ||
? { | ||
schema: argsOrSchema, | ||
document: document, | ||
rootValue, | ||
contextValue, | ||
variableValues, | ||
operationName, | ||
fieldResolver, | ||
typeResolver, | ||
} | ||
: argsOrSchema; | ||
const onResolversHandlers = []; | ||
let executeFn = graphql.execute; | ||
let result; | ||
const afterCalls = []; | ||
let context = args.contextValue; | ||
for (const onExecute of onExecuteCbs) { | ||
let stopCalled = false; | ||
const after = onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
stopCalled = true; | ||
result = stopResult; | ||
}, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
context = { | ||
...(context || {}), | ||
...extension, | ||
}; | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args, | ||
}); | ||
if (stopCalled) { | ||
return result; | ||
} | ||
if (after) { | ||
if (after.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
if (after.onResolverCalled) { | ||
onResolversHandlers.push(after.onResolverCalled); | ||
} | ||
}, | ||
args, | ||
}); | ||
if (stopCalled) { | ||
return result; | ||
} | ||
if (after) { | ||
if (after.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
if (after.onResolverCalled) { | ||
onResolversHandlers.push(after.onResolverCalled); | ||
} | ||
} | ||
} | ||
if (onResolversHandlers.length) { | ||
context[resolversHooksSymbol] = onResolversHandlers; | ||
} | ||
result = await executeFn({ | ||
...args, | ||
contextValue: context, | ||
}); | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
if (onResolversHandlers.length) { | ||
context[resolversHooksSymbol] = onResolversHandlers; | ||
} | ||
result = await executeFn({ | ||
...args, | ||
contextValue: context, | ||
}); | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
} | ||
return result; | ||
} | ||
return result; | ||
}; | ||
: graphql.execute; | ||
function prepareSchema() { | ||
if (schema[trackedSchemaSymbol]) { | ||
if (!schema || schema[trackedSchemaSymbol]) { | ||
return; | ||
@@ -419,4 +439,5 @@ } | ||
}; | ||
return { | ||
onContextBuilding() { | ||
const result = {}; | ||
if (options.onContextBuildingMeasurement) { | ||
result.onContextBuilding = () => { | ||
const contextStartTime = process.hrtime(); | ||
@@ -426,4 +447,6 @@ return () => { | ||
}; | ||
}, | ||
onParse({ params }) { | ||
}; | ||
} | ||
if (options.onParsingMeasurement) { | ||
result.onParse = ({ params }) => { | ||
const parseStartTime = process.hrtime(); | ||
@@ -433,4 +456,6 @@ return () => { | ||
}; | ||
}, | ||
onValidate({ params }) { | ||
}; | ||
} | ||
if (options.onValidationMeasurement) { | ||
result.onValidate = ({ params }) => { | ||
const validateStartTime = process.hrtime(); | ||
@@ -440,32 +465,61 @@ return () => { | ||
}; | ||
}, | ||
onExecute({ args }) { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
} | ||
if (options.onExecutionMeasurement) { | ||
if (options.onResolverMeasurement) { | ||
result.onExecute = ({ args }) => { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
}; | ||
}, | ||
onSubscribe({ args }) { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
} | ||
else { | ||
result.onExecute = ({ args }) => { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
}; | ||
}; | ||
}, | ||
}; | ||
} | ||
} | ||
if (options.onSubscriptionMeasurement) { | ||
if (options.onResolverMeasurement) { | ||
result.onSubscribe = ({ args }) => { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
}; | ||
} | ||
else { | ||
result.onSubscribe = ({ args }) => { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
}; | ||
}; | ||
} | ||
} | ||
return result; | ||
}; | ||
@@ -486,3 +540,3 @@ | ||
var _a; | ||
if (((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) { | ||
errorHandler(result.errors); | ||
@@ -489,0 +543,0 @@ } |
392
index.esm.js
export * from '@envelop/types'; | ||
import { specifiedRules, isIntrospectionType, isObjectType, defaultFieldResolver, parse, validate, GraphQLSchema, subscribe, execute, getOperationAST } from 'graphql'; | ||
import { parse, specifiedRules, validate, execute, isIntrospectionType, isObjectType, defaultFieldResolver, GraphQLSchema, subscribe, getOperationAST } from 'graphql'; | ||
const trackedSchemaSymbol = Symbol('TRACKED_SCHEMA'); | ||
const resolversHooksSymbol = Symbol('RESOLVERS_HOOKS'); | ||
function envelop(options) { | ||
let schema = options.initialSchema; | ||
function envelop({ plugins }) { | ||
let schema = null; | ||
let initDone = false; | ||
const childPlugins = (options.extends || []).reduce((prev, child) => [...prev, ...child._plugins], []); | ||
const plugins = [...childPlugins, ...options.plugins]; | ||
const replaceSchema = (newSchema, ignorePluginIndex = -1) => { | ||
@@ -30,12 +28,28 @@ schema = newSchema; | ||
plugin.onPluginInit({ | ||
plugins, | ||
addPlugin: newPlugin => { | ||
plugins.push(newPlugin); | ||
}, | ||
setSchema: modifiedSchema => replaceSchema(modifiedSchema, i), | ||
}); | ||
} | ||
const customParse = (source, parseOptions) => { | ||
let result = null; | ||
let parseFn = parse; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onParse && | ||
plugin.onParse({ | ||
const onContextBuildingCbs = []; | ||
const onExecuteCbs = []; | ||
const onParseCbs = []; | ||
const onSubscribeCbs = []; | ||
const onValidateCbs = []; | ||
for (const { onContextBuilding, onExecute, onParse, onSubscribe, onValidate } of plugins) { | ||
onContextBuilding && onContextBuildingCbs.push(onContextBuilding); | ||
onExecute && onExecuteCbs.push(onExecute); | ||
onParse && onParseCbs.push(onParse); | ||
onSubscribe && onSubscribeCbs.push(onSubscribe); | ||
onValidate && onValidateCbs.push(onValidate); | ||
} | ||
const customParse = onParseCbs.length | ||
? (source, parseOptions) => { | ||
let result = null; | ||
let parseFn = parse; | ||
const afterCalls = []; | ||
for (const onParse of onParseCbs) { | ||
const afterFn = onParse({ | ||
params: { source, options: parseOptions }, | ||
@@ -50,33 +64,37 @@ parseFn, | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
if (result === null) { | ||
try { | ||
result = parseFn(source, parseOptions); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
catch (e) { | ||
result = e; | ||
if (result === null) { | ||
try { | ||
result = parseFn(source, parseOptions); | ||
} | ||
catch (e) { | ||
result = e; | ||
} | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
replaceParseResult: newResult => { | ||
result = newResult; | ||
}, | ||
result, | ||
}); | ||
} | ||
if (result === null) { | ||
throw new Error(`Failed to parse document.`); | ||
} | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
return result; | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
replaceParseResult: newResult => { | ||
result = newResult; | ||
}, | ||
result, | ||
}); | ||
} | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
return result; | ||
}; | ||
const customValidate = (schema, documentAST, rules, typeInfo, validationOptions) => { | ||
let actualRules = rules ? [...rules] : undefined; | ||
let validateFn = validate; | ||
let result = null; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onValidate && | ||
plugin.onValidate({ | ||
: parse; | ||
const customValidate = onValidateCbs.length | ||
? (schema, documentAST, rules, typeInfo, validationOptions) => { | ||
let actualRules = rules ? [...rules] : undefined; | ||
let validateFn = validate; | ||
let result = null; | ||
const afterCalls = []; | ||
for (const onValidate of onValidateCbs) { | ||
const afterFn = onValidate({ | ||
params: { | ||
@@ -103,19 +121,20 @@ schema, | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
if (!result) { | ||
result = validateFn(schema, documentAST, actualRules, typeInfo, validationOptions); | ||
} | ||
const valid = result.length === 0; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ valid, result }); | ||
} | ||
return result; | ||
} | ||
if (result === null) { | ||
result = validateFn(schema, documentAST, actualRules, typeInfo, validationOptions); | ||
} | ||
const valid = result.length === 0; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ valid, result }); | ||
} | ||
return result; | ||
}; | ||
const customContextFactory = async (initialContext) => { | ||
let context = initialContext; | ||
const afterCalls = []; | ||
for (const plugin of plugins) { | ||
const afterFn = plugin.onContextBuilding && | ||
(await plugin.onContextBuilding({ | ||
: validate; | ||
const customContextFactory = onContextBuildingCbs.length | ||
? async (initialContext) => { | ||
let context = initialContext; | ||
const afterCalls = []; | ||
for (const onContext of onContextBuildingCbs) { | ||
const afterFn = await onContext({ | ||
context, | ||
@@ -133,12 +152,11 @@ extendContext: extension => { | ||
}, | ||
})); | ||
afterFn && afterCalls.push(afterFn); | ||
}); | ||
afterFn && afterCalls.push(afterFn); | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ context }); | ||
} | ||
return context; | ||
} | ||
for (const afterCb of afterCalls) { | ||
afterCb({ context }); | ||
} | ||
return context; | ||
}; | ||
const beforeExecuteCalls = plugins.filter(p => p.onExecute); | ||
const beforeSubscribeCalls = plugins.filter(p => p.onSubscribe); | ||
: (ctx) => ctx; | ||
const customSubscribe = async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) => { | ||
@@ -148,3 +166,3 @@ const args = argsOrSchema instanceof GraphQLSchema | ||
schema: argsOrSchema, | ||
document, | ||
document: document, | ||
rootValue, | ||
@@ -162,4 +180,4 @@ contextValue, | ||
let context = args.contextValue; | ||
for (const plugin of beforeSubscribeCalls) { | ||
const after = plugin.onSubscribe({ | ||
for (const onSubscribe of onSubscribeCbs) { | ||
const after = onSubscribe({ | ||
subscribeFn, | ||
@@ -208,75 +226,77 @@ setSubscribeFn: newSubscribeFn => { | ||
}; | ||
const customExecute = async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => { | ||
const args = argsOrSchema instanceof GraphQLSchema | ||
? { | ||
schema: argsOrSchema, | ||
document, | ||
rootValue, | ||
contextValue, | ||
variableValues, | ||
operationName, | ||
fieldResolver, | ||
typeResolver, | ||
} | ||
: argsOrSchema; | ||
const onResolversHandlers = []; | ||
let executeFn = execute; | ||
let result; | ||
const afterCalls = []; | ||
let context = args.contextValue; | ||
for (const plugin of beforeExecuteCalls) { | ||
let stopCalled = false; | ||
const after = plugin.onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
stopCalled = true; | ||
result = stopResult; | ||
}, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
context = { | ||
...(context || {}), | ||
...extension, | ||
}; | ||
const customExecute = onExecuteCbs.length | ||
? async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => { | ||
const args = argsOrSchema instanceof GraphQLSchema | ||
? { | ||
schema: argsOrSchema, | ||
document: document, | ||
rootValue, | ||
contextValue, | ||
variableValues, | ||
operationName, | ||
fieldResolver, | ||
typeResolver, | ||
} | ||
: argsOrSchema; | ||
const onResolversHandlers = []; | ||
let executeFn = execute; | ||
let result; | ||
const afterCalls = []; | ||
let context = args.contextValue; | ||
for (const onExecute of onExecuteCbs) { | ||
let stopCalled = false; | ||
const after = onExecute({ | ||
executeFn, | ||
setExecuteFn: newExecuteFn => { | ||
executeFn = newExecuteFn; | ||
}, | ||
setResultAndStopExecution: stopResult => { | ||
stopCalled = true; | ||
result = stopResult; | ||
}, | ||
extendContext: extension => { | ||
if (typeof extension === 'object') { | ||
context = { | ||
...(context || {}), | ||
...extension, | ||
}; | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
} | ||
}, | ||
args, | ||
}); | ||
if (stopCalled) { | ||
return result; | ||
} | ||
if (after) { | ||
if (after.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
else { | ||
throw new Error(`Invalid context extension provided! Expected "object", got: "${JSON.stringify(extension)}" (${typeof extension})`); | ||
if (after.onResolverCalled) { | ||
onResolversHandlers.push(after.onResolverCalled); | ||
} | ||
}, | ||
args, | ||
}); | ||
if (stopCalled) { | ||
return result; | ||
} | ||
if (after) { | ||
if (after.onExecuteDone) { | ||
afterCalls.push(after.onExecuteDone); | ||
} | ||
if (after.onResolverCalled) { | ||
onResolversHandlers.push(after.onResolverCalled); | ||
} | ||
} | ||
} | ||
if (onResolversHandlers.length) { | ||
context[resolversHooksSymbol] = onResolversHandlers; | ||
} | ||
result = await executeFn({ | ||
...args, | ||
contextValue: context, | ||
}); | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
if (onResolversHandlers.length) { | ||
context[resolversHooksSymbol] = onResolversHandlers; | ||
} | ||
result = await executeFn({ | ||
...args, | ||
contextValue: context, | ||
}); | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
result, | ||
setResult: newResult => { | ||
result = newResult; | ||
}, | ||
}); | ||
} | ||
return result; | ||
} | ||
return result; | ||
}; | ||
: execute; | ||
function prepareSchema() { | ||
if (schema[trackedSchemaSymbol]) { | ||
if (!schema || schema[trackedSchemaSymbol]) { | ||
return; | ||
@@ -414,4 +434,5 @@ } | ||
}; | ||
return { | ||
onContextBuilding() { | ||
const result = {}; | ||
if (options.onContextBuildingMeasurement) { | ||
result.onContextBuilding = () => { | ||
const contextStartTime = process.hrtime(); | ||
@@ -421,4 +442,6 @@ return () => { | ||
}; | ||
}, | ||
onParse({ params }) { | ||
}; | ||
} | ||
if (options.onParsingMeasurement) { | ||
result.onParse = ({ params }) => { | ||
const parseStartTime = process.hrtime(); | ||
@@ -428,4 +451,6 @@ return () => { | ||
}; | ||
}, | ||
onValidate({ params }) { | ||
}; | ||
} | ||
if (options.onValidationMeasurement) { | ||
result.onValidate = ({ params }) => { | ||
const validateStartTime = process.hrtime(); | ||
@@ -435,32 +460,61 @@ return () => { | ||
}; | ||
}, | ||
onExecute({ args }) { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
} | ||
if (options.onExecutionMeasurement) { | ||
if (options.onResolverMeasurement) { | ||
result.onExecute = ({ args }) => { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
}; | ||
}, | ||
onSubscribe({ args }) { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
} | ||
else { | ||
result.onExecute = ({ args }) => { | ||
const executeStartTime = process.hrtime(); | ||
return { | ||
onExecuteDone: () => { | ||
options.onExecutionMeasurement(args, deltaFrom(executeStartTime)); | ||
}, | ||
}; | ||
}; | ||
}, | ||
}; | ||
} | ||
} | ||
if (options.onSubscriptionMeasurement) { | ||
if (options.onResolverMeasurement) { | ||
result.onSubscribe = ({ args }) => { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
onResolverCalled: ({ info }) => { | ||
const resolverStartTime = process.hrtime(); | ||
return () => { | ||
options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime)); | ||
}; | ||
}, | ||
}; | ||
}; | ||
} | ||
else { | ||
result.onSubscribe = ({ args }) => { | ||
const subscribeStartTime = process.hrtime(); | ||
return { | ||
onSubscribeResult: () => { | ||
options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime)); | ||
}, | ||
}; | ||
}; | ||
} | ||
} | ||
return result; | ||
}; | ||
@@ -481,3 +535,3 @@ | ||
var _a; | ||
if (((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) { | ||
errorHandler(result.errors); | ||
@@ -484,0 +538,0 @@ } |
{ | ||
"name": "@envelop/core", | ||
"version": "0.1.4", | ||
"version": "0.2.0-alpha-bd31072.0", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
@@ -15,2 +15,2 @@ import { Plugin } from '@envelop/types'; | ||
}; | ||
export declare const useTiming: (rawOptions?: TimingPluginOptions) => Plugin; | ||
export declare const useTiming: (rawOptions?: TimingPluginOptions | undefined) => Plugin; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128353
15
1151