@envelop/execute-subscription-event 3.0.0-alpha-d0d0776.0 → 3.0.0-alpha-ccec0fc.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 CHANGED
@@ -7,35 +7,35 @@ Utilities for hooking into the [ExecuteSubscriptionEvent](<https://spec.graphql.
7
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
8
 
9
9
  ```ts
10
- import { envelop } from '@envelop/core';
11
- import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event';
12
- import { createContext, createDataLoaders } from './context';
10
+ import { envelop } from '@envelop/core'
11
+ import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event'
12
+ import { createContext, createDataLoaders } from './context'
13
13
 
14
14
  const getEnveloped = envelop({
15
15
  plugins: [
16
- useContext(() => createContext())
16
+ useContext(() => createContext()),
17
17
  useContextValuePerExecuteSubscriptionEvent(() => ({
18
18
  // Existing context is merged with this context partial
19
19
  // By recreating the DataLoader we ensure no DataLoader caches from the previous event/initial field subscribe call are are hit
20
20
  contextPartial: {
21
21
  dataLoaders: createDataLoaders()
22
- },
23
- })),
22
+ }
23
+ }))
24
24
  // ... other plugins ...
25
- ],
26
- });
25
+ ]
26
+ })
27
27
  ```
28
28
 
29
29
  Alternatively, you can also provide a callback that is invoked after each [`ExecuteSubscriptionEvent`](<https://spec.graphql.org/draft/#ExecuteSubscriptionEvent()>) phase.
30
30
 
31
31
  ```ts
32
- import { envelop } from '@envelop/core';
33
- import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event';
34
- import { createContext, createDataLoaders } from './context';
32
+ import { envelop } from '@envelop/core'
33
+ import { useContextValuePerExecuteSubscriptionEvent } from '@envelop/execute-subscription-event'
34
+ import { createContext, createDataLoaders } from './context'
35
35
 
36
36
  const getEnveloped = envelop({
37
37
  plugins: [
38
- useContext(() => createContext())
38
+ useContext(() => createContext()),
39
39
  useContextValuePerExecuteSubscriptionEvent(({ args }) => ({
40
40
  onEnd: () => {
41
41
  // Note that onEnd is invoked only after each ExecuteSubscriptionEvent phase
@@ -44,8 +44,8 @@ const getEnveloped = envelop({
44
44
  args.contextValue.dataLoaders.users.clearAll()
45
45
  args.contextValue.dataLoaders.posts.clearAll()
46
46
  }
47
- })),
47
+ }))
48
48
  // ... other plugins ...
49
- ],
50
- });
49
+ ]
50
+ })
51
51
  ```
package/cjs/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useExtendContextValuePerExecuteSubscriptionEvent = void 0;
4
+ const graphql_1 = require("@graphql-tools/graphql");
5
+ const core_1 = require("@envelop/core");
6
+ const subscribe_js_1 = require("./subscribe.js");
7
+ const useExtendContextValuePerExecuteSubscriptionEvent = (createContext) => {
8
+ return {
9
+ onSubscribe({ args, setSubscribeFn }) {
10
+ const executeNew = (0, core_1.makeExecute)(async (executionArgs) => {
11
+ var _a;
12
+ const context = await createContext({ args });
13
+ try {
14
+ return await (0, graphql_1.execute)({
15
+ ...executionArgs,
16
+ // GraphQL.js 16 changed the type of contextValue to unknown
17
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
18
+ // @ts-ignore
19
+ contextValue: { ...executionArgs.contextValue, ...context === null || context === void 0 ? void 0 : context.contextPartial },
20
+ });
21
+ }
22
+ finally {
23
+ (_a = context === null || context === void 0 ? void 0 : context.onEnd) === null || _a === void 0 ? void 0 : _a.call(context);
24
+ }
25
+ });
26
+ setSubscribeFn((0, subscribe_js_1.subscribe)(executeNew));
27
+ },
28
+ };
29
+ };
30
+ exports.useExtendContextValuePerExecuteSubscriptionEvent = useExtendContextValuePerExecuteSubscriptionEvent;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subscribe = void 0;
4
+ const graphql_1 = require("@graphql-tools/graphql");
5
+ const core_1 = require("@envelop/core");
6
+ /**
7
+ * This is a almost identical port from graphql-js subscribe.
8
+ * The only difference is that a custom `execute` function can be injected for customizing the behavior.
9
+ */
10
+ const subscribe = (execute) => (0, core_1.makeSubscribe)(async (args) => {
11
+ const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver, } = args;
12
+ const resultOrStream = await (0, graphql_1.createSourceEventStream)({
13
+ schema,
14
+ document,
15
+ rootValue,
16
+ contextValue,
17
+ variableValues: variableValues !== null && variableValues !== void 0 ? variableValues : undefined,
18
+ operationName,
19
+ subscribeFieldResolver,
20
+ });
21
+ if (!(0, core_1.isAsyncIterable)(resultOrStream)) {
22
+ return resultOrStream;
23
+ }
24
+ // For each payload yielded from a subscription, map it over the normal
25
+ // GraphQL `execute` function, with `payload` as the rootValue.
26
+ // This implements the "MapSourceToResponseEvent" algorithm described in
27
+ // the GraphQL specification. The `execute` function provides the
28
+ // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
29
+ // "ExecuteQuery" algorithm, for which `execute` is also used.
30
+ const mapSourceToResponse = (payload) => execute({
31
+ schema,
32
+ document,
33
+ rootValue: payload,
34
+ contextValue,
35
+ variableValues,
36
+ operationName,
37
+ fieldResolver,
38
+ });
39
+ // Map every source value to a ExecutionResult value as described above.
40
+ return (0, core_1.mapAsyncIterator)(resultOrStream, mapSourceToResponse);
41
+ });
42
+ exports.subscribe = subscribe;
package/esm/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import { execute } from '@graphql-tools/graphql';
2
+ import { makeExecute } from '@envelop/core';
3
+ import { subscribe } from './subscribe.js';
4
+ export const useExtendContextValuePerExecuteSubscriptionEvent = (createContext) => {
5
+ return {
6
+ onSubscribe({ args, setSubscribeFn }) {
7
+ const executeNew = makeExecute(async (executionArgs) => {
8
+ var _a;
9
+ const context = await createContext({ args });
10
+ try {
11
+ return await execute({
12
+ ...executionArgs,
13
+ // GraphQL.js 16 changed the type of contextValue to unknown
14
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
15
+ // @ts-ignore
16
+ contextValue: { ...executionArgs.contextValue, ...context === null || context === void 0 ? void 0 : context.contextPartial },
17
+ });
18
+ }
19
+ finally {
20
+ (_a = context === null || context === void 0 ? void 0 : context.onEnd) === null || _a === void 0 ? void 0 : _a.call(context);
21
+ }
22
+ });
23
+ setSubscribeFn(subscribe(executeNew));
24
+ },
25
+ };
26
+ };
@@ -0,0 +1,38 @@
1
+ import { createSourceEventStream } from '@graphql-tools/graphql';
2
+ import { makeSubscribe, mapAsyncIterator, isAsyncIterable } from '@envelop/core';
3
+ /**
4
+ * This is a almost identical port from graphql-js subscribe.
5
+ * The only difference is that a custom `execute` function can be injected for customizing the behavior.
6
+ */
7
+ export const subscribe = (execute) => makeSubscribe(async (args) => {
8
+ const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver, } = args;
9
+ const resultOrStream = await createSourceEventStream({
10
+ schema,
11
+ document,
12
+ rootValue,
13
+ contextValue,
14
+ variableValues: variableValues !== null && variableValues !== void 0 ? variableValues : undefined,
15
+ operationName,
16
+ subscribeFieldResolver,
17
+ });
18
+ if (!isAsyncIterable(resultOrStream)) {
19
+ return resultOrStream;
20
+ }
21
+ // For each payload yielded from a subscription, map it over the normal
22
+ // GraphQL `execute` function, with `payload` as the rootValue.
23
+ // This implements the "MapSourceToResponseEvent" algorithm described in
24
+ // the GraphQL specification. The `execute` function provides the
25
+ // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
26
+ // "ExecuteQuery" algorithm, for which `execute` is also used.
27
+ const mapSourceToResponse = (payload) => execute({
28
+ schema,
29
+ document,
30
+ rootValue: payload,
31
+ contextValue,
32
+ variableValues,
33
+ operationName,
34
+ fieldResolver,
35
+ });
36
+ // Map every source value to a ExecutionResult value as described above.
37
+ return mapAsyncIterator(resultOrStream, mapSourceToResponse);
38
+ });
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/execute-subscription-event",
3
- "version": "3.0.0-alpha-d0d0776.0",
3
+ "version": "3.0.0-alpha-ccec0fc.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "3.0.0-alpha-d0d0776.0",
7
- "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
6
+ "@envelop/core": "3.0.0-alpha-ccec0fc.0",
7
+ "@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0"
8
8
  },
9
+ "dependencies": {},
9
10
  "repository": {
10
11
  "type": "git",
11
12
  "url": "https://github.com/n1ru4l/envelop.git",
@@ -13,33 +14,42 @@
13
14
  },
14
15
  "author": "Laurin Quast <laurinquast@googlemail.com>",
15
16
  "license": "MIT",
16
- "main": "index.js",
17
- "module": "index.mjs",
18
- "typings": "index.d.ts",
17
+ "main": "cjs/index.js",
18
+ "module": "esm/index.js",
19
+ "typings": "typings/index.d.ts",
19
20
  "typescript": {
20
- "definition": "index.d.ts"
21
+ "definition": "typings/index.d.ts"
21
22
  },
23
+ "type": "module",
22
24
  "exports": {
23
25
  ".": {
24
26
  "require": {
25
- "default": "./index.js",
26
- "types": "./index.d.ts"
27
+ "types": "./typings/index.d.ts",
28
+ "default": "./cjs/index.js"
27
29
  },
28
30
  "import": {
29
- "default": "./index.mjs",
30
- "types": "./index.d.ts"
31
+ "types": "./typings/index.d.ts",
32
+ "default": "./esm/index.js"
33
+ },
34
+ "default": {
35
+ "types": "./typings/index.d.ts",
36
+ "default": "./esm/index.js"
31
37
  }
32
38
  },
33
39
  "./*": {
34
40
  "require": {
35
- "default": "./*.js",
36
- "types": "./*.d.ts"
41
+ "types": "./typings/*.d.ts",
42
+ "default": "./cjs/*.js"
37
43
  },
38
44
  "import": {
39
- "default": "./*.mjs",
40
- "types": "./*.d.ts"
45
+ "types": "./typings/*.d.ts",
46
+ "default": "./esm/*.js"
47
+ },
48
+ "default": {
49
+ "types": "./typings/*.d.ts",
50
+ "default": "./esm/*.js"
41
51
  }
42
52
  },
43
53
  "./package.json": "./package.json"
44
54
  }
45
- }
55
+ }
@@ -1,4 +1,4 @@
1
- import { SubscriptionArgs } from 'graphql';
1
+ import { SubscriptionArgs } from '@graphql-tools/graphql';
2
2
  import { Plugin, PromiseOrValue, DefaultContext } from '@envelop/core';
3
3
  export declare type ContextFactoryOptions = {
4
4
  /** The arguments with which the subscription was set up. */
File without changes
package/index.js DELETED
@@ -1,60 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const graphql = require('graphql');
6
- const core = require('@envelop/core');
7
-
8
- /**
9
- * This is a almost identical port from graphql-js subscribe.
10
- * The only difference is that a custom `execute` function can be injected for customizing the behavior.
11
- */
12
- const subscribe = (execute) => core.makeSubscribe(async (args) => {
13
- const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver } = args;
14
- const resultOrStream = await graphql.createSourceEventStream(schema, document, rootValue, contextValue, variableValues ?? undefined, operationName, subscribeFieldResolver);
15
- if (!core.isAsyncIterable(resultOrStream)) {
16
- return resultOrStream;
17
- }
18
- // For each payload yielded from a subscription, map it over the normal
19
- // GraphQL `execute` function, with `payload` as the rootValue.
20
- // This implements the "MapSourceToResponseEvent" algorithm described in
21
- // the GraphQL specification. The `execute` function provides the
22
- // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
23
- // "ExecuteQuery" algorithm, for which `execute` is also used.
24
- const mapSourceToResponse = (payload) => execute({
25
- schema,
26
- document,
27
- rootValue: payload,
28
- contextValue,
29
- variableValues,
30
- operationName,
31
- fieldResolver,
32
- });
33
- // Map every source value to a ExecutionResult value as described above.
34
- return core.mapAsyncIterator(resultOrStream, mapSourceToResponse);
35
- });
36
-
37
- const useExtendContextValuePerExecuteSubscriptionEvent = (createContext) => {
38
- return {
39
- onSubscribe({ args, setSubscribeFn }) {
40
- const executeNew = core.makeExecute(async (executionArgs) => {
41
- const context = await createContext({ args });
42
- try {
43
- return await graphql.execute({
44
- ...executionArgs,
45
- // GraphQL.js 16 changed the type of contextValue to unknown
46
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
47
- // @ts-ignore
48
- contextValue: { ...executionArgs.contextValue, ...context?.contextPartial },
49
- });
50
- }
51
- finally {
52
- context?.onEnd?.();
53
- }
54
- });
55
- setSubscribeFn(subscribe(executeNew));
56
- },
57
- };
58
- };
59
-
60
- exports.useExtendContextValuePerExecuteSubscriptionEvent = useExtendContextValuePerExecuteSubscriptionEvent;
package/index.mjs DELETED
@@ -1,56 +0,0 @@
1
- import { createSourceEventStream, execute } from 'graphql';
2
- import { makeSubscribe, isAsyncIterable, mapAsyncIterator, makeExecute } from '@envelop/core';
3
-
4
- /**
5
- * This is a almost identical port from graphql-js subscribe.
6
- * The only difference is that a custom `execute` function can be injected for customizing the behavior.
7
- */
8
- const subscribe = (execute) => makeSubscribe(async (args) => {
9
- const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver } = args;
10
- const resultOrStream = await createSourceEventStream(schema, document, rootValue, contextValue, variableValues ?? undefined, operationName, subscribeFieldResolver);
11
- if (!isAsyncIterable(resultOrStream)) {
12
- return resultOrStream;
13
- }
14
- // For each payload yielded from a subscription, map it over the normal
15
- // GraphQL `execute` function, with `payload` as the rootValue.
16
- // This implements the "MapSourceToResponseEvent" algorithm described in
17
- // the GraphQL specification. The `execute` function provides the
18
- // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
19
- // "ExecuteQuery" algorithm, for which `execute` is also used.
20
- const mapSourceToResponse = (payload) => execute({
21
- schema,
22
- document,
23
- rootValue: payload,
24
- contextValue,
25
- variableValues,
26
- operationName,
27
- fieldResolver,
28
- });
29
- // Map every source value to a ExecutionResult value as described above.
30
- return mapAsyncIterator(resultOrStream, mapSourceToResponse);
31
- });
32
-
33
- const useExtendContextValuePerExecuteSubscriptionEvent = (createContext) => {
34
- return {
35
- onSubscribe({ args, setSubscribeFn }) {
36
- const executeNew = makeExecute(async (executionArgs) => {
37
- const context = await createContext({ args });
38
- try {
39
- return await execute({
40
- ...executionArgs,
41
- // GraphQL.js 16 changed the type of contextValue to unknown
42
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
43
- // @ts-ignore
44
- contextValue: { ...executionArgs.contextValue, ...context?.contextPartial },
45
- });
46
- }
47
- finally {
48
- context?.onEnd?.();
49
- }
50
- });
51
- setSubscribeFn(subscribe(executeNew));
52
- },
53
- };
54
- };
55
-
56
- export { useExtendContextValuePerExecuteSubscriptionEvent };