@envelop/execute-subscription-event 2.4.0-alpha-26c4ae2.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
## `@envelop/execute-subscription-event`
|
|
2
|
+
|
|
3
|
+
Utilities for hooking into the [ExecuteSubscriptionEvent](<https://spec.graphql.org/draft/#ExecuteSubscriptionEvent()>) phase.
|
|
4
|
+
|
|
5
|
+
### `useContextValuePerExecuteSubscriptionEvent`
|
|
6
|
+
|
|
7
|
+
Create a new context object per `ExecuteSubscriptionEvent` phase, allowing to bypass common issues with context objects such as [`DataLoader`](https://github.com/n1ru4l/envelop/issues/80) [caching](https://github.com/graphql/graphql-js/issues/894) [issues](https://github.com/apollographql/subscriptions-transport-ws/issues/330).
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { envelop } from '@envelop/core';
|
|
11
|
+
import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event';
|
|
12
|
+
import { createContext, createDataLoaders } from './context';
|
|
13
|
+
|
|
14
|
+
const getEnveloped = envelop({
|
|
15
|
+
plugins: [
|
|
16
|
+
useContext(() => createContext())
|
|
17
|
+
useContextValuePerExecuteSubscriptionEvent(() => ({
|
|
18
|
+
// Existing context is merged with this context partial
|
|
19
|
+
// By recreating the DataLoader we ensure no DataLoader caches from the previous event/initial field subscribe call are are hit
|
|
20
|
+
contextPartial: {
|
|
21
|
+
dataLoaders: createDataLoaders()
|
|
22
|
+
},
|
|
23
|
+
})),
|
|
24
|
+
// ... other plugins ...
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Alternatively, you can also provide a callback that is invoked after each [`ExecuteSubscriptionEvent`](<https://spec.graphql.org/draft/#ExecuteSubscriptionEvent()>) phase.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { envelop } from '@envelop/core';
|
|
33
|
+
import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event';
|
|
34
|
+
import { createContext, createDataLoaders } from './context';
|
|
35
|
+
|
|
36
|
+
const getEnveloped = envelop({
|
|
37
|
+
plugins: [
|
|
38
|
+
useContext(() => createContext())
|
|
39
|
+
useContextValuePerExecuteSubscriptionEvent(({ args }) => ({
|
|
40
|
+
onEnd: () => {
|
|
41
|
+
// Note that onEnd is invoked only after each ExecuteSubscriptionEvent phase
|
|
42
|
+
// This means the initial event will still use the cache from potential subscribe dataloader calls
|
|
43
|
+
// If you use this to clear DataLoader caches it is recommended to not do any DataLoader calls within your field subscribe function.
|
|
44
|
+
args.contextValue.dataLoaders.users.clearAll()
|
|
45
|
+
args.contextValue.dataLoaders.posts.clearAll()
|
|
46
|
+
}
|
|
47
|
+
})),
|
|
48
|
+
// ... other plugins ...
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
```
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/execute-subscription-event",
|
|
3
|
-
"version": "2.4.0
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0
|
|
6
|
+
"@envelop/core": "^2.4.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {},
|