@envelop/execute-subscription-event
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

@envelop/execute-subscription-event

Utilities for hooking into the ExecuteSubscriptionEvent phase.

useContextValuePerExecuteSubscriptionEvent

Create a new context object per ExecuteSubscriptionEvent phase, allowing to bypass common issues with context objects such as DataLoader caching issues.

import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine } from '@envelop/core'
import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event'
import { createContext, createDataLoaders } from './context'

const getEnveloped = envelop({
  plugins: [
    useEngine({ parse, validate, specifiedRules, execute, subscribe }),
    useContext(() => createContext()),
    useContextValuePerExecuteSubscriptionEvent(() => ({
      // Existing context is merged with this context partial
      // By recreating the DataLoader we ensure no DataLoader caches from the previous event/initial field subscribe call are are hit
      contextPartial: {
        dataLoaders: createDataLoaders()
      }
    }))
    // ... other plugins ...
  ]
})

Alternatively, you can also provide a callback that is invoked after each ExecuteSubscriptionEvent phase.

import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine } from '@envelop/core'
import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event'
import { createContext, createDataLoaders } from './context'

const getEnveloped = envelop({
  plugins: [
    useEngine({ parse, validate, specifiedRules, execute, subscribe }),
    useContext(() => createContext()),
    useContextValuePerExecuteSubscriptionEvent(({ args }) => ({
      onEnd: () => {
        // Note that onEnd is invoked only after each ExecuteSubscriptionEvent phase
        // This means the initial event will still use the cache from potential subscribe dataloader calls
        // If you use this to clear DataLoader caches it is recommended to not do any DataLoader calls within your field subscribe function.
        args.contextValue.dataLoaders.users.clearAll()
        args.contextValue.dataLoaders.posts.clearAll()
      }
    }))
    // ... other plugins ...
  ]
})

Readme

Keywords

none

Package Sidebar

Install

npm i @envelop/execute-subscription-event

Weekly Downloads

4,544

Version

5.0.0

License

MIT

Unpacked Size

13.3 kB

Total Files

12

Last publish

Collaborators

  • dotansimha