@envelop/core
Advanced tools
Comparing version 0.6.0-alpha-728ec6f.0 to 1.0.0
147
index.js
@@ -124,2 +124,20 @@ 'use strict'; | ||
} | ||
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) { | ||
@@ -396,15 +414,3 @@ try { | ||
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 = []; | ||
@@ -458,4 +464,6 @@ let executeFn = graphql.execute; | ||
}); | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
const hookResult = afterCb({ | ||
result, | ||
@@ -466,5 +474,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; | ||
@@ -484,3 +515,3 @@ initDone = true; | ||
return { | ||
get schema() { | ||
getCurrentSchema() { | ||
return schema; | ||
@@ -567,4 +598,10 @@ }, | ||
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; | ||
@@ -619,4 +656,4 @@ } | ||
const getEnveloped = (initialContext = {}) => { | ||
orchestrator.init(initialContext); | ||
const typedOrchestrator = orchestrator; | ||
typedOrchestrator.init(initialContext); | ||
return { | ||
@@ -628,3 +665,3 @@ parse: typedOrchestrator.parse(initialContext), | ||
subscribe: typedOrchestrator.subscribe, | ||
schema: typedOrchestrator.schema, | ||
schema: typedOrchestrator.getCurrentSchema(), | ||
}; | ||
@@ -827,2 +864,9 @@ }; | ||
}; | ||
const useLazyLoadedSchema = (schemaLoader) => { | ||
return { | ||
onEnveloped({ setSchema, context }) { | ||
setSchema(schemaLoader(context)); | ||
}, | ||
}; | ||
}; | ||
const useAsyncSchema = (schemaPromise) => { | ||
@@ -838,10 +882,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; | ||
}, | ||
@@ -858,10 +914,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; | ||
}, | ||
@@ -883,12 +951,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; | ||
}, | ||
@@ -913,2 +993,3 @@ }; | ||
exports.formatError = formatError; | ||
exports.getExecuteArgs = getExecuteArgs; | ||
exports.getSubscribeArgs = getSubscribeArgs; | ||
@@ -919,2 +1000,3 @@ exports.isIntrospectionDocument = isIntrospectionDocument; | ||
exports.isOperationDefinition = isOperationDefinition; | ||
exports.makeExecute = makeExecute; | ||
exports.makeSubscribe = makeSubscribe; | ||
@@ -926,2 +1008,3 @@ exports.mapAsyncIterator = mapAsyncIterator; | ||
exports.useExtendContext = useExtendContext; | ||
exports.useLazyLoadedSchema = useLazyLoadedSchema; | ||
exports.useLogger = useLogger; | ||
@@ -928,0 +1011,0 @@ exports.useMaskedErrors = useMaskedErrors; |
@@ -11,4 +11,4 @@ import { ArbitraryObject, EnvelopContextFnWrapper, GetEnvelopedFn, Plugin } from '@envelop/types'; | ||
contextFactory: EnvelopContextFnWrapper<ReturnType<GetEnvelopedFn<PluginsContext>>['contextFactory'], PluginsContext>; | ||
schema: Maybe<GraphQLSchema>; | ||
getCurrentSchema: () => Maybe<GraphQLSchema>; | ||
}; | ||
export declare function createEnvelopOrchestrator<PluginsContext = any>(plugins: Plugin[]): EnvelopOrchestrator<any, PluginsContext>; |
{ | ||
"name": "@envelop/core", | ||
"version": "0.6.0-alpha-728ec6f.0", | ||
"version": "1.0.0", | ||
"sideEffects": false, | ||
@@ -9,3 +9,3 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@envelop/types": "0.5.0-alpha-728ec6f.0" | ||
"@envelop/types": "1.0.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; |
import { GraphQLSchema } from 'graphql'; | ||
import { Plugin } from '@envelop/types'; | ||
import { DefaultContext, Maybe, Plugin } from '@envelop/types'; | ||
export declare const useSchema: (schema: GraphQLSchema) => Plugin; | ||
export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => GraphQLSchema) => Plugin; | ||
export declare const useAsyncSchema: (schemaPromise: Promise<GraphQLSchema>) => Plugin; |
@@ -25,2 +25,10 @@ ## `@envelop/core` | ||
#### `useAsyncSchema` | ||
Same as `useSchema`, but you can provide a `Promise<GraphQLSchema>` to load the actual schema. | ||
#### `useLazyLoadedSchema` | ||
Same as `useSchema`, but you can provide a `(initialContext) => GraphQLSchema` function to load the actual schema. | ||
#### `useErrorHandler` | ||
@@ -27,0 +35,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { ArbitraryObject } from 'packages/types/src/get-enveloped'; | ||
import { ArbitraryObject } from '@envelop/types'; | ||
import { EnvelopOrchestrator } from './orchestrator'; | ||
@@ -3,0 +3,0 @@ export declare function traceOrchestrator<TInitialContext extends ArbitraryObject, TPluginsContext extends ArbitraryObject>(orchestrator: EnvelopOrchestrator<TInitialContext, TPluginsContext>): EnvelopOrchestrator<TInitialContext & { |
@@ -1,3 +0,3 @@ | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs } from 'graphql'; | ||
import { PolymorphicSubscribeArguments } from '@envelop/types'; | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs, ExecutionArgs } from 'graphql'; | ||
import { AsyncIterableIteratorOrValue, PolymorphicExecuteArguments, PolymorphicSubscribeArguments } from '@envelop/types'; | ||
import { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue'; | ||
@@ -15,2 +15,7 @@ export declare const envelopIsIntrospectionSymbol: unique symbol; | ||
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
224832
2055
1
176
+ Added@envelop/types@1.0.0(transitive)
- Removed@envelop/types@0.5.0-alpha-728ec6f.0(transitive)
Updated@envelop/types@1.0.0