@envelop/core
Advanced tools
Comparing version 0.5.1 to 0.6.0-alpha-728ec6f.0
135
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,60 @@ | ||
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); | ||
} | ||
} | ||
async function* finalAsyncIterator(asyncIterable, onFinal) { | ||
try { | ||
yield* asyncIterable; | ||
} | ||
finally { | ||
onFinal(); | ||
} | ||
} | ||
function createEnvelopOrchestrator(plugins) { | ||
@@ -268,15 +329,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 +362,6 @@ let subscribeFn = graphql.subscribe; | ||
}); | ||
const onNextHandler = []; | ||
const onEndHandler = []; | ||
for (const afterCb of afterCalls) { | ||
afterCb({ | ||
const hookResult = afterCb({ | ||
result, | ||
@@ -321,5 +372,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; | ||
}; | ||
}); | ||
const customExecute = beforeCallbacks.execute.length | ||
@@ -559,29 +633,2 @@ ? async (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => { | ||
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) => { | ||
@@ -858,3 +905,5 @@ return { | ||
exports.envelopIsIntrospectionSymbol = envelopIsIntrospectionSymbol; | ||
exports.finalAsyncIterator = finalAsyncIterator; | ||
exports.formatError = formatError; | ||
exports.getSubscribeArgs = getSubscribeArgs; | ||
exports.isIntrospectionDocument = isIntrospectionDocument; | ||
@@ -864,2 +913,4 @@ exports.isIntrospectionOperation = isIntrospectionOperation; | ||
exports.isOperationDefinition = isOperationDefinition; | ||
exports.makeSubscribe = makeSubscribe; | ||
exports.mapAsyncIterator = mapAsyncIterator; | ||
exports.useAsyncSchema = useAsyncSchema; | ||
@@ -866,0 +917,0 @@ exports.useEnvelop = useEnvelop; |
{ | ||
"name": "@envelop/core", | ||
"version": "0.5.1", | ||
"version": "0.6.0-alpha-728ec6f.0", | ||
"sideEffects": false, | ||
@@ -9,3 +9,3 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@envelop/types": "0.4.1" | ||
"@envelop/types": "0.5.0-alpha-728ec6f.0" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -1,2 +0,4 @@ | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source } from 'graphql'; | ||
import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs } from 'graphql'; | ||
import { PolymorphicSubscribeArguments } from '@envelop/types'; | ||
import { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue'; | ||
export declare const envelopIsIntrospectionSymbol: unique symbol; | ||
@@ -7,1 +9,8 @@ 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 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
205699
1884
+ Added@envelop/types@0.5.0-alpha-728ec6f.0(transitive)
- Removed@envelop/types@0.4.1(transitive)