5.0.0 • Published 7 months ago

@envelop/execute-subscription-event v5.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@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 ...
  ]
})
4.0.1

9 months ago

4.0.3

8 months ago

4.0.2

8 months ago

5.0.0

7 months ago

4.0.0

12 months ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.4

1 year ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2

2 years ago

2.6.0

2 years ago

2.5.0

2 years ago

2.3.3

2 years ago

2.3.2

2 years ago

2.3.0

2 years ago

2.3.1

2 years ago

2.2.0

2 years ago

1.0.0

2 years ago

2.0.0

2 years ago

2.1.0

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.1.0

3 years ago