@graphql-tools/executor-graphql-ws 0.0.6 → 0.0.7

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 CHANGED
@@ -2,20 +2,54 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildGraphQLWSExecutor = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const utils_1 = require("@graphql-tools/utils");
5
6
  const repeater_1 = require("@repeaterjs/repeater");
6
7
  const graphql_1 = require("graphql");
7
8
  const graphql_ws_1 = require("graphql-ws");
8
9
  const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
9
- function buildGraphQLWSExecutor(url, webSocketImpl = isomorphic_ws_1.default, connectionParams) {
10
- const graphqlWSClient = (0, graphql_ws_1.createClient)({
11
- url,
12
- webSocketImpl,
13
- connectionParams,
14
- lazy: true,
15
- });
16
- return function GraphQLWSExecutor({ document, variables, operationName, extensions, }) {
10
+ function isClient(client) {
11
+ return 'subscribe' in client;
12
+ }
13
+ function buildGraphQLWSExecutor(clientOptionsOrClient) {
14
+ let graphqlWSClient;
15
+ if (isClient(clientOptionsOrClient)) {
16
+ graphqlWSClient = clientOptionsOrClient;
17
+ }
18
+ else {
19
+ graphqlWSClient = (0, graphql_ws_1.createClient)({
20
+ webSocketImpl: isomorphic_ws_1.default,
21
+ lazy: true,
22
+ ...clientOptionsOrClient,
23
+ });
24
+ if (clientOptionsOrClient.onClient) {
25
+ clientOptionsOrClient.onClient(graphqlWSClient);
26
+ }
27
+ }
28
+ return function GraphQLWSExecutor(executionRequest) {
29
+ const { document, variables, operationName, extensions, operationType = (0, utils_1.getOperationASTFromRequest)(executionRequest).operation, } = executionRequest;
17
30
  const query = (0, graphql_1.print)(document);
18
- return new repeater_1.Repeater(function repeaterExecutor(push, stop) {
31
+ if (operationType === 'subscription') {
32
+ return new repeater_1.Repeater(function repeaterExecutor(push, stop) {
33
+ const unsubscribe = graphqlWSClient.subscribe({
34
+ query,
35
+ variables,
36
+ operationName,
37
+ extensions,
38
+ }, {
39
+ next(data) {
40
+ return push(data);
41
+ },
42
+ error(error) {
43
+ return stop(error);
44
+ },
45
+ complete() {
46
+ return stop();
47
+ },
48
+ });
49
+ return stop.finally(unsubscribe);
50
+ });
51
+ }
52
+ return new Promise((resolve, reject) => {
19
53
  const unsubscribe = graphqlWSClient.subscribe({
20
54
  query,
21
55
  variables,
@@ -23,16 +57,15 @@ function buildGraphQLWSExecutor(url, webSocketImpl = isomorphic_ws_1.default, co
23
57
  extensions,
24
58
  }, {
25
59
  next(data) {
26
- return push(data);
60
+ return resolve(data);
27
61
  },
28
62
  error(error) {
29
- return stop(error);
63
+ return reject(error);
30
64
  },
31
65
  complete() {
32
- return stop();
66
+ unsubscribe();
33
67
  },
34
68
  });
35
- return stop.finally(unsubscribe);
36
69
  });
37
70
  };
38
71
  }
package/esm/index.js CHANGED
@@ -1,17 +1,51 @@
1
+ import { getOperationASTFromRequest } from '@graphql-tools/utils';
1
2
  import { Repeater } from '@repeaterjs/repeater';
2
3
  import { print } from 'graphql';
3
4
  import { createClient } from 'graphql-ws';
4
5
  import WebSocket from 'isomorphic-ws';
5
- export function buildGraphQLWSExecutor(url, webSocketImpl = WebSocket, connectionParams) {
6
- const graphqlWSClient = createClient({
7
- url,
8
- webSocketImpl,
9
- connectionParams,
10
- lazy: true,
11
- });
12
- return function GraphQLWSExecutor({ document, variables, operationName, extensions, }) {
6
+ function isClient(client) {
7
+ return 'subscribe' in client;
8
+ }
9
+ export function buildGraphQLWSExecutor(clientOptionsOrClient) {
10
+ let graphqlWSClient;
11
+ if (isClient(clientOptionsOrClient)) {
12
+ graphqlWSClient = clientOptionsOrClient;
13
+ }
14
+ else {
15
+ graphqlWSClient = createClient({
16
+ webSocketImpl: WebSocket,
17
+ lazy: true,
18
+ ...clientOptionsOrClient,
19
+ });
20
+ if (clientOptionsOrClient.onClient) {
21
+ clientOptionsOrClient.onClient(graphqlWSClient);
22
+ }
23
+ }
24
+ return function GraphQLWSExecutor(executionRequest) {
25
+ const { document, variables, operationName, extensions, operationType = getOperationASTFromRequest(executionRequest).operation, } = executionRequest;
13
26
  const query = print(document);
14
- return new Repeater(function repeaterExecutor(push, stop) {
27
+ if (operationType === 'subscription') {
28
+ return new Repeater(function repeaterExecutor(push, stop) {
29
+ const unsubscribe = graphqlWSClient.subscribe({
30
+ query,
31
+ variables,
32
+ operationName,
33
+ extensions,
34
+ }, {
35
+ next(data) {
36
+ return push(data);
37
+ },
38
+ error(error) {
39
+ return stop(error);
40
+ },
41
+ complete() {
42
+ return stop();
43
+ },
44
+ });
45
+ return stop.finally(unsubscribe);
46
+ });
47
+ }
48
+ return new Promise((resolve, reject) => {
15
49
  const unsubscribe = graphqlWSClient.subscribe({
16
50
  query,
17
51
  variables,
@@ -19,16 +53,15 @@ export function buildGraphQLWSExecutor(url, webSocketImpl = WebSocket, connectio
19
53
  extensions,
20
54
  }, {
21
55
  next(data) {
22
- return push(data);
56
+ return resolve(data);
23
57
  },
24
58
  error(error) {
25
- return stop(error);
59
+ return reject(error);
26
60
  },
27
61
  complete() {
28
- return stop();
62
+ unsubscribe();
29
63
  },
30
64
  });
31
- return stop.finally(unsubscribe);
32
65
  });
33
66
  };
34
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/executor-graphql-ws",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -1,4 +1,7 @@
1
- /// <reference types="ws" />
2
1
  import { Executor } from '@graphql-tools/utils';
3
- import WebSocket from 'isomorphic-ws';
4
- export declare function buildGraphQLWSExecutor(url: string, webSocketImpl?: typeof WebSocket, connectionParams?: Record<string, any>): Executor;
2
+ import { Client, ClientOptions } from 'graphql-ws';
3
+ interface GraphQLWSExecutorOptions extends ClientOptions {
4
+ onClient?: (client: Client) => void;
5
+ }
6
+ export declare function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorOptions | Client): Executor;
7
+ export {};
@@ -1,4 +1,7 @@
1
- /// <reference types="ws" />
2
1
  import { Executor } from '@graphql-tools/utils';
3
- import WebSocket from 'isomorphic-ws';
4
- export declare function buildGraphQLWSExecutor(url: string, webSocketImpl?: typeof WebSocket, connectionParams?: Record<string, any>): Executor;
2
+ import { Client, ClientOptions } from 'graphql-ws';
3
+ interface GraphQLWSExecutorOptions extends ClientOptions {
4
+ onClient?: (client: Client) => void;
5
+ }
6
+ export declare function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorOptions | Client): Executor;
7
+ export {};