@envelop/core
Advanced tools
Comparing version 0.5.2 to 0.6.0-alpha-3faaeae.0
268
index.js
@@ -5,3 +5,6 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
const types = require('@envelop/types'); | ||
const isAsyncIterable = _interopDefault(require('graphql/jsutils/isAsyncIterable')); | ||
const graphql = require('graphql'); | ||
@@ -73,2 +76,78 @@ | ||
const envelopIsIntrospectionSymbol = Symbol('ENVELOP_IS_INTROSPECTION'); | ||
function isOperationDefinition(def) { | ||
return def.kind === graphql.Kind.OPERATION_DEFINITION; | ||
} | ||
function isIntrospectionOperation(operation) { | ||
if (operation.kind === 'OperationDefinition') { | ||
let hasIntrospectionField = false; | ||
graphql.visit(operation, { | ||
Field: node => { | ||
if (node.name.value === '__schema') { | ||
hasIntrospectionField = true; | ||
return graphql.BREAK; | ||
} | ||
}, | ||
}); | ||
return hasIntrospectionField; | ||
} | ||
return false; | ||
} | ||
function isIntrospectionDocument(document) { | ||
const operations = document.definitions.filter(isOperationDefinition); | ||
return operations.some(op => isIntrospectionOperation(op)); | ||
} | ||
function isIntrospectionOperationString(operation) { | ||
return (typeof operation === 'string' ? operation : operation.body).indexOf('__schema') !== -1; | ||
} | ||
function getSubscribeArgs(args) { | ||
return args.length === 1 | ||
? args[0] | ||
: { | ||
schema: args[0], | ||
document: args[1], | ||
rootValue: args[2], | ||
contextValue: args[3], | ||
variableValues: args[4], | ||
operationName: args[5], | ||
fieldResolver: args[6], | ||
subscribeFieldResolver: args[7], | ||
}; | ||
} | ||
/** | ||
* Utility function for making a subscribe function that handles polymorphic arguments. | ||
*/ | ||
const makeSubscribe = (subscribeFn) => (...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)); | ||
async function* mapAsyncIterator(asyncIterable, map) { | ||
for await (const value of asyncIterable) { | ||
yield map(value); | ||
} | ||
} | ||
function getExecuteArgs(args) { | ||
return args.length === 1 | ||
? args[0] | ||
: { | ||
schema: args[0], | ||
document: args[1], | ||
rootValue: args[2], | ||
contextValue: args[3], | ||
variableValues: args[4], | ||
operationName: args[5], | ||
fieldResolver: args[6], | ||
typeResolver: args[7], | ||
}; | ||
} | ||
/** | ||
* Utility function for making a execute function that handles polymorphic arguments. | ||
*/ | ||
const makeExecute = (executeFn) => (...polyArgs) => executeFn(getExecuteArgs(polyArgs)); | ||
async function* finalAsyncIterator(asyncIterable, onFinal) { | ||
try { | ||
yield* asyncIterable; | ||
} | ||
finally { | ||
onFinal(); | ||
} | ||
} | ||
function createEnvelopOrchestrator(plugins) { | ||
@@ -268,15 +347,3 @@ let schema = null; | ||
: initialContext => orchestratorCtx => orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext; | ||
const customSubscribe = async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) => { | ||
const args = argsOrSchema instanceof graphql.GraphQLSchema | ||
? { | ||
schema: argsOrSchema, | ||
document: document, | ||
rootValue, | ||
contextValue, | ||
variableValues, | ||
operationName, | ||
fieldResolver, | ||
subscribeFieldResolver, | ||
} | ||
: argsOrSchema; | ||
const customSubscribe = makeSubscribe(async (args) => { | ||
const onResolversHandlers = []; | ||
@@ -313,4 +380,6 @@ let subscribeFn = graphql.subscribe; | ||
}); | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
const hookResult = afterCb({ | ||
result, | ||
@@ -321,19 +390,30 @@ setResult: newResult => { | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ result, setResult: newResult => (result = newResult) }); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
}; | ||
}); | ||
const customExecute = beforeCallbacks.execute.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; | ||
? makeExecute(async (args) => { | ||
const onResolversHandlers = []; | ||
@@ -387,4 +467,6 @@ let executeFn = graphql.execute; | ||
}); | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
const hookResult = afterCb({ | ||
result, | ||
@@ -395,5 +477,28 @@ setResult: newResult => { | ||
}); | ||
if (hookResult) { | ||
if (hookResult.onNext) { | ||
onNextHandler.push(hookResult.onNext); | ||
} | ||
if (hookResult.onEnd) { | ||
onEndHandler.push(hookResult.onEnd); | ||
} | ||
} | ||
} | ||
if (onNextHandler.length && isAsyncIterable(result)) { | ||
result = mapAsyncIterator(result, async (result) => { | ||
for (const onNext of onNextHandler) { | ||
await onNext({ result, setResult: newResult => (result = newResult) }); | ||
} | ||
return result; | ||
}); | ||
} | ||
if (onEndHandler.length && isAsyncIterable(result)) { | ||
result = finalAsyncIterator(result, () => { | ||
for (const onEnd of onEndHandler) { | ||
onEnd(); | ||
} | ||
}); | ||
} | ||
return result; | ||
} | ||
}) | ||
: graphql.execute; | ||
@@ -495,4 +600,10 @@ initDone = true; | ||
done(); | ||
result.extensions = result.extensions || {}; | ||
result.extensions.envelopTracing = args.contextValue._envelopTracing; | ||
if (!isAsyncIterable(result)) { | ||
result.extensions = result.extensions || {}; | ||
result.extensions.envelopTracing = args.contextValue._envelopTracing; | ||
} | ||
else { | ||
// eslint-disable-next-line no-console | ||
console.warn(`"traceOrchestrator" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`); | ||
} | ||
return result; | ||
@@ -562,29 +673,2 @@ } | ||
const envelopIsIntrospectionSymbol = Symbol('ENVELOP_IS_INTROSPECTION'); | ||
function isOperationDefinition(def) { | ||
return def.kind === graphql.Kind.OPERATION_DEFINITION; | ||
} | ||
function isIntrospectionOperation(operation) { | ||
if (operation.kind === 'OperationDefinition') { | ||
let hasIntrospectionField = false; | ||
graphql.visit(operation, { | ||
Field: node => { | ||
if (node.name.value === '__schema') { | ||
hasIntrospectionField = true; | ||
return graphql.BREAK; | ||
} | ||
}, | ||
}); | ||
return hasIntrospectionField; | ||
} | ||
return false; | ||
} | ||
function isIntrospectionDocument(document) { | ||
const operations = document.definitions.filter(isOperationDefinition); | ||
return operations.some(op => isIntrospectionOperation(op)); | ||
} | ||
function isIntrospectionOperationString(operation) { | ||
return (typeof operation === 'string' ? operation : operation.body).indexOf('__schema') !== -1; | ||
} | ||
const useEnvelop = (envelop) => { | ||
@@ -791,10 +875,22 @@ return { | ||
const makeHandleResult = (errorHandler) => ({ result }) => { | ||
var _a; | ||
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) { | ||
errorHandler(result.errors); | ||
} | ||
}; | ||
const useErrorHandler = (errorHandler) => ({ | ||
onExecute() { | ||
const handleResult = makeHandleResult(errorHandler); | ||
return { | ||
onExecuteDone: ({ result }) => { | ||
var _a; | ||
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) { | ||
errorHandler(result.errors); | ||
onExecuteDone({ result }) { | ||
if (isAsyncIterable(result)) { | ||
return { | ||
onNext({ result }) { | ||
handleResult({ result }); | ||
}, | ||
}; | ||
} | ||
handleResult({ result }); | ||
return undefined; | ||
}, | ||
@@ -811,10 +907,22 @@ }; | ||
const makeHandleResult$1 = (formatter) => ({ result, setResult }) => { | ||
const modified = formatter(result); | ||
if (modified !== false) { | ||
setResult(modified); | ||
} | ||
}; | ||
const usePayloadFormatter = (formatter) => ({ | ||
onExecute() { | ||
const handleResult = makeHandleResult$1(formatter); | ||
return { | ||
onExecuteDone: ({ result, setResult }) => { | ||
const modified = formatter(result); | ||
if (modified !== false) { | ||
setResult(modified); | ||
onExecuteDone({ result, setResult }) { | ||
if (isAsyncIterable(result)) { | ||
return { | ||
onNext({ result, setResult }) { | ||
handleResult({ result, setResult }); | ||
}, | ||
}; | ||
} | ||
handleResult({ result, setResult }); | ||
return undefined; | ||
}, | ||
@@ -836,12 +944,24 @@ }; | ||
}; | ||
const makeHandleResult$2 = (format) => ({ result, setResult }) => { | ||
if (result.errors != null) { | ||
setResult({ ...result, errors: result.errors.map(error => format(error)) }); | ||
} | ||
}; | ||
const useMaskedErrors = (opts) => { | ||
var _a; | ||
const format = (_a = opts === null || opts === void 0 ? void 0 : opts.formatError) !== null && _a !== void 0 ? _a : formatError; | ||
const handleResult = makeHandleResult$2(format); | ||
return { | ||
onExecute: () => { | ||
onExecute() { | ||
return { | ||
onExecuteDone: ({ result, setResult }) => { | ||
if (result.errors != null) { | ||
setResult({ ...result, errors: result.errors.map(error => format(error)) }); | ||
onExecuteDone({ result, setResult }) { | ||
if (isAsyncIterable(result)) { | ||
return { | ||
onNext: ({ result, setResult }) => { | ||
handleResult({ result, setResult }); | ||
}, | ||
}; | ||
} | ||
handleResult({ result, setResult }); | ||
return undefined; | ||
}, | ||
@@ -864,3 +984,6 @@ }; | ||
exports.envelopIsIntrospectionSymbol = envelopIsIntrospectionSymbol; | ||
exports.finalAsyncIterator = finalAsyncIterator; | ||
exports.formatError = formatError; | ||
exports.getExecuteArgs = getExecuteArgs; | ||
exports.getSubscribeArgs = getSubscribeArgs; | ||
exports.isIntrospectionDocument = isIntrospectionDocument; | ||
@@ -870,2 +993,5 @@ exports.isIntrospectionOperation = isIntrospectionOperation; | ||
exports.isOperationDefinition = isOperationDefinition; | ||
exports.makeExecute = makeExecute; | ||
exports.makeSubscribe = makeSubscribe; | ||
exports.mapAsyncIterator = mapAsyncIterator; | ||
exports.useAsyncSchema = useAsyncSchema; | ||
@@ -872,0 +998,0 @@ exports.useEnvelop = useEnvelop; |
{ | ||
"name": "@envelop/core", | ||
"version": "0.5.2", | ||
"version": "0.6.0-alpha-3faaeae.0", | ||
"sideEffects": false, | ||
@@ -9,3 +9,3 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@envelop/types": "0.4.2" | ||
"@envelop/types": "0.5.0-alpha-3faaeae.0" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
import { Plugin } from '@envelop/types'; | ||
import { GraphQLError } from 'graphql'; | ||
export declare const useErrorHandler: (errorHandler: (errors: readonly GraphQLError[]) => void) => Plugin; | ||
export declare type ErrorHandler = (errors: readonly GraphQLError[]) => void; | ||
export declare const useErrorHandler: (errorHandler: ErrorHandler) => Plugin; |
import { Plugin } from '@envelop/types'; | ||
import { ExecutionResult } from 'graphql'; | ||
export declare const usePayloadFormatter: (formatter: (result: ExecutionResult<any, any>) => false | ExecutionResult<any, any>) => Plugin; | ||
export declare type FormatterFunction = (result: ExecutionResult<any, any>) => false | ExecutionResult<any, any>; | ||
export declare const usePayloadFormatter: (formatter: FormatterFunction) => Plugin; |
@@ -1,2 +0,4 @@ | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source } from 'graphql'; | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs, ExecutionArgs } from 'graphql'; | ||
import { AsyncIterableIteratorOrValue, PolymorphicExecuteArguments, PolymorphicSubscribeArguments } from '@envelop/types'; | ||
import { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue'; | ||
export declare const envelopIsIntrospectionSymbol: unique symbol; | ||
@@ -7,1 +9,13 @@ export declare function isOperationDefinition(def: ASTNode): def is OperationDefinitionNode; | ||
export declare function isIntrospectionOperationString(operation: string | Source): boolean; | ||
export declare function getSubscribeArgs(args: PolymorphicSubscribeArguments): SubscriptionArgs; | ||
/** | ||
* Utility function for making a subscribe function that handles polymorphic arguments. | ||
*/ | ||
export declare const makeSubscribe: (subscribeFn: (args: SubscriptionArgs) => PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult>) => (...polyArgs: PolymorphicSubscribeArguments) => PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult>; | ||
export declare function mapAsyncIterator<TInput, TOutput = TInput>(asyncIterable: AsyncIterableIterator<TInput>, map: (input: TInput) => Promise<TOutput> | TOutput): AsyncIterableIterator<TOutput>; | ||
export declare function getExecuteArgs(args: PolymorphicExecuteArguments): ExecutionArgs; | ||
/** | ||
* Utility function for making a execute function that handles polymorphic arguments. | ||
*/ | ||
export declare const makeExecute: (executeFn: (args: ExecutionArgs) => PromiseOrValue<AsyncIterableIteratorOrValue<ExecutionResult>>) => (...polyArgs: PolymorphicExecuteArguments) => PromiseOrValue<AsyncIterableIteratorOrValue<ExecutionResult>>; | ||
export declare function finalAsyncIterator<TInput>(asyncIterable: AsyncIterableIterator<TInput>, onFinal: () => void): AsyncIterableIterator<TInput>; |
Sorry, the diff of this file is not supported yet
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
223160
2039
+ Added@envelop/types@0.5.0-alpha-3faaeae.0(transitive)
- Removed@envelop/types@0.4.2(transitive)