@envelop/execute-subscription-event 2.4.0-alpha-c0cc559.0 → 2.4.0-alpha-e9434aa.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/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");
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,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subscribe = void 0;
4
+ const graphql_1 = require("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)(schema, document, rootValue, contextValue, variableValues !== null && variableValues !== void 0 ? variableValues : undefined, operationName, subscribeFieldResolver);
13
+ if (!(0, core_1.isAsyncIterable)(resultOrStream)) {
14
+ return resultOrStream;
15
+ }
16
+ // For each payload yielded from a subscription, map it over the normal
17
+ // GraphQL `execute` function, with `payload` as the rootValue.
18
+ // This implements the "MapSourceToResponseEvent" algorithm described in
19
+ // the GraphQL specification. The `execute` function provides the
20
+ // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
21
+ // "ExecuteQuery" algorithm, for which `execute` is also used.
22
+ const mapSourceToResponse = (payload) => execute({
23
+ schema,
24
+ document,
25
+ rootValue: payload,
26
+ contextValue,
27
+ variableValues,
28
+ operationName,
29
+ fieldResolver,
30
+ });
31
+ // Map every source value to a ExecutionResult value as described above.
32
+ return (0, core_1.mapAsyncIterator)(resultOrStream, mapSourceToResponse);
33
+ });
34
+ exports.subscribe = subscribe;
package/esm/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");
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,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subscribe = void 0;
4
+ const graphql_1 = require("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)(schema, document, rootValue, contextValue, variableValues !== null && variableValues !== void 0 ? variableValues : undefined, operationName, subscribeFieldResolver);
13
+ if (!(0, core_1.isAsyncIterable)(resultOrStream)) {
14
+ return resultOrStream;
15
+ }
16
+ // For each payload yielded from a subscription, map it over the normal
17
+ // GraphQL `execute` function, with `payload` as the rootValue.
18
+ // This implements the "MapSourceToResponseEvent" algorithm described in
19
+ // the GraphQL specification. The `execute` function provides the
20
+ // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
21
+ // "ExecuteQuery" algorithm, for which `execute` is also used.
22
+ const mapSourceToResponse = (payload) => execute({
23
+ schema,
24
+ document,
25
+ rootValue: payload,
26
+ contextValue,
27
+ variableValues,
28
+ operationName,
29
+ fieldResolver,
30
+ });
31
+ // Map every source value to a ExecutionResult value as described above.
32
+ return (0, core_1.mapAsyncIterator)(resultOrStream, mapSourceToResponse);
33
+ });
34
+ exports.subscribe = subscribe;
package/package.json CHANGED
@@ -1,33 +1,55 @@
1
1
  {
2
2
  "name": "@envelop/execute-subscription-event",
3
- "version": "2.4.0-alpha-c0cc559.0",
3
+ "version": "2.4.0-alpha-e9434aa.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-c0cc559.0",
6
+ "@envelop/core": "2.4.0-alpha-e9434aa.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
+ "dependencies": {},
9
10
  "repository": {
10
11
  "type": "git",
11
- "url": "https://github.com/dotansimha/envelop.git",
12
+ "url": "https://github.com/n1ru4l/envelop.git",
12
13
  "directory": "packages/plugins/execute-subscription-event"
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
- "require": "./index.js",
25
- "import": "./index.mjs"
26
+ "require": {
27
+ "types": "./typings/index.d.ts",
28
+ "default": "./cjs/index.js"
29
+ },
30
+ "import": {
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"
37
+ }
26
38
  },
27
39
  "./*": {
28
- "require": "./*.js",
29
- "import": "./*.mjs"
40
+ "require": {
41
+ "types": "./typings/*.d.ts",
42
+ "default": "./cjs/*.js"
43
+ },
44
+ "import": {
45
+ "types": "./typings/*.d.ts",
46
+ "default": "./esm/*.js"
47
+ },
48
+ "default": {
49
+ "types": "./typings/*.d.ts",
50
+ "default": "./esm/*.js"
51
+ }
30
52
  },
31
53
  "./package.json": "./package.json"
32
54
  }
33
- }
55
+ }
File without changes
File without changes
package/README.md DELETED
@@ -1,51 +0,0 @@
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/dotansimha/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/index.js DELETED
@@ -1,61 +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 !== null && variableValues !== void 0 ? 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
- var _a;
42
- const context = await createContext({ args });
43
- try {
44
- return await graphql.execute({
45
- ...executionArgs,
46
- // GraphQL.js 16 changed the type of contextValue to unknown
47
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
48
- // @ts-ignore
49
- contextValue: { ...executionArgs.contextValue, ...context === null || context === void 0 ? void 0 : context.contextPartial },
50
- });
51
- }
52
- finally {
53
- (_a = context === null || context === void 0 ? void 0 : context.onEnd) === null || _a === void 0 ? void 0 : _a.call(context);
54
- }
55
- });
56
- setSubscribeFn(subscribe(executeNew));
57
- },
58
- };
59
- };
60
-
61
- exports.useExtendContextValuePerExecuteSubscriptionEvent = useExtendContextValuePerExecuteSubscriptionEvent;
package/index.mjs DELETED
@@ -1,57 +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 !== null && variableValues !== void 0 ? 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
- var _a;
38
- const context = await createContext({ args });
39
- try {
40
- return await execute({
41
- ...executionArgs,
42
- // GraphQL.js 16 changed the type of contextValue to unknown
43
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
44
- // @ts-ignore
45
- contextValue: { ...executionArgs.contextValue, ...context === null || context === void 0 ? void 0 : context.contextPartial },
46
- });
47
- }
48
- finally {
49
- (_a = context === null || context === void 0 ? void 0 : context.onEnd) === null || _a === void 0 ? void 0 : _a.call(context);
50
- }
51
- });
52
- setSubscribeFn(subscribe(executeNew));
53
- },
54
- };
55
- };
56
-
57
- export { useExtendContextValuePerExecuteSubscriptionEvent };