UNPKG

@envelop/execute-subscription-event

Version:

Utilities for hooking into the [ExecuteSubscriptionEvent](<https://spec.graphql.org/draft/#ExecuteSubscriptionEvent()>) phase.

26 lines (25 loc) 1.03 kB
import { execute } from 'graphql'; import { makeExecute } from '@envelop/core'; import { subscribe } from './subscribe.js'; export const useExtendContextValuePerExecuteSubscriptionEvent = (createContext) => { return { onSubscribe({ args, setSubscribeFn }) { const executeNew = makeExecute(async (executionArgs) => { const context = await createContext({ args }); try { return await execute({ ...executionArgs, // GraphQL.js 16 changed the type of contextValue to unknown // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore contextValue: { ...executionArgs.contextValue, ...context?.contextPartial }, }); } finally { context?.onEnd?.(); } }); setSubscribeFn(subscribe(executeNew)); }, }; };