/**
 * This file contains code that is derived from code copyright (c) Facebook,
 * Inc. and its affiliates; released under the MIT license.
 *
 * The original code can be seen at the following URL, which includes a
 * reference to the original license:
 *
 *   https://github.com/graphql/graphql-js/blob/f56905bd6b030d5912092a1239ed21f73fbdd408/src/subscription/subscribe.js
 */ import { DocumentNode , GraphQLSchema , GraphQLFieldResolver , createSourceEventStream , execute , GraphQLError , ExecutionResult , } from 'graphql' ; import type { subscribe } from 'graphql' ; import mapAsyncIterator from './mapAsyncIterator' ; import { isAsyncIterable } from 'iterall' ; import { CreateRequestHandlerOptions } from '../../interfaces' ; import { PluginHookFn } from '../pluginHook' ; type mixed = any ; internal export function makeLiveSubscribe ( params ) { const { pluginHook } = params ; return function liveSubscribe ( argsOrSchema , document ? , rootValue ? , contextValue ? , variableValues ? , operationName ? , fieldResolver ? , subscribeFieldResolver ? ) { return arguments . length === 1 ? liveSubscribeImpl ( argsOrSchema . schema , argsOrSchema . document , argsOrSchema . rootValue , argsOrSchema . contextValue , argsOrSchema . variableValues , argsOrSchema . operationName , argsOrSchema . fieldResolver , argsOrSchema . subscribeFieldResolver ) : liveSubscribeImpl ( argsOrSchema , document as DocumentNode , rootValue , contextValue , variableValues , operationName , fieldResolver , subscribeFieldResolver ) ; } ; function liveSubscribeImpl ( schema , document , rootValue ? , contextValue ? , variableValues ? , operationName ? , fieldResolver ? , subscribeFieldResolver ? ) { const sourcePromise = createSourceEventStream ( schema , document , rootValue , contextValue , variableValues , operationName , subscribeFieldResolver ) ; const mapSourceToResponse = async ( payload ) => { try { const executionResult = execute ( schema , document , payload , contextValue , variableValues , operationName , fieldResolver ) ; const hookedExecutionResult = pluginHook ( 'postgraphile:liveSubscribe:executionResult' , executionResult , { schema , document , rootValue , contextValue , variableValues , operationName , fieldResolver , subscribeFieldResolver , } ) ; return await hookedExecutionResult ; } finally { if ( payload && typeof payload . release === 'function' ) { payload . release ( ) ; } } } ; return sourcePromise . then ( resultOrStream => isAsyncIterable ( resultOrStream ) ? mapAsyncIterator ( ( resultOrStream as any ) as AsyncIterable < mixed > , mapSourceToResponse , reportGraphQLError ) : ( ( resultOrStream as any ) as ExecutionResult ) , reportGraphQLError ) ; } } /**
 * This function checks if the error is a GraphQLError. If it is, report it as
 * an ExecutionResult, containing only errors and no data. Otherwise treat the
 * error as a system-class error and re-throw it.
 */ function reportGraphQLError ( error ) { if ( error instanceof GraphQLError ) { return { errors : [ error ] } ; } throw error ; }