@graphql-tools/url-loader 7.9.24 → 7.9.25

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.
Files changed (4) hide show
  1. package/index.d.ts +4 -0
  2. package/index.js +48 -22
  3. package/index.mjs +48 -22
  4. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -91,6 +91,10 @@ export interface LoadFromUrlOptions extends BaseLoaderOptions, Partial<Introspec
91
91
  * Request Credentials
92
92
  */
93
93
  credentials?: RequestCredentials;
94
+ /**
95
+ * Connection Parameters for WebSockets connection
96
+ */
97
+ connectionParams?: any;
94
98
  }
95
99
  /**
96
100
  * This loader loads a schema from a URL. The loaded schema is a fully-executable,
package/index.js CHANGED
@@ -601,28 +601,55 @@ class UrlLoader {
601
601
  https: 'wss',
602
602
  http: 'ws',
603
603
  });
604
+ const observerById = new Map();
605
+ let websocket = null;
606
+ const ensureWebsocket = () => {
607
+ websocket = new WebSocketImpl(WS_URL, 'graphql-ws', {
608
+ followRedirects: true,
609
+ headers: options === null || options === void 0 ? void 0 : options.headers,
610
+ rejectUnauthorized: false,
611
+ skipUTF8Validation: true,
612
+ });
613
+ websocket.onopen = () => {
614
+ let payload = {};
615
+ switch (typeof (options === null || options === void 0 ? void 0 : options.connectionParams)) {
616
+ case 'function':
617
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams();
618
+ break;
619
+ case 'object':
620
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams;
621
+ break;
622
+ }
623
+ websocket.send(JSON.stringify({
624
+ type: LEGACY_WS.CONNECTION_INIT,
625
+ payload,
626
+ }));
627
+ };
628
+ };
629
+ const cleanupWebsocket = () => {
630
+ if (websocket != null && observerById.size === 0) {
631
+ websocket.send(JSON.stringify({
632
+ type: LEGACY_WS.CONNECTION_TERMINATE,
633
+ }));
634
+ websocket.terminate();
635
+ websocket = null;
636
+ }
637
+ };
604
638
  return function legacyExecutor(request) {
605
639
  const id = Date.now().toString();
606
640
  return utils.observableToAsyncIterable({
607
641
  subscribe(observer) {
608
- const websocket = new WebSocketImpl(WS_URL, 'graphql-ws', {
609
- followRedirects: true,
610
- headers: options === null || options === void 0 ? void 0 : options.headers,
611
- rejectUnauthorized: false,
612
- skipUTF8Validation: true,
613
- });
614
- websocket.onopen = () => {
615
- websocket.send(JSON.stringify({
616
- type: LEGACY_WS.CONNECTION_INIT,
617
- payload: {
618
- ...request.extensions,
619
- },
620
- }));
621
- };
642
+ ensureWebsocket();
643
+ if (websocket == null) {
644
+ throw new Error(`WebSocket connection is not found!`);
645
+ }
622
646
  websocket.onmessage = event => {
623
647
  const data = JSON.parse(event.data.toString('utf-8'));
624
648
  switch (data.type) {
625
649
  case LEGACY_WS.CONNECTION_ACK: {
650
+ if (websocket == null) {
651
+ throw new Error(`WebSocket connection is not found!`);
652
+ }
626
653
  websocket.send(JSON.stringify({
627
654
  type: LEGACY_WS.START,
628
655
  id,
@@ -646,25 +673,25 @@ class UrlLoader {
646
673
  break;
647
674
  }
648
675
  case LEGACY_WS.COMPLETE: {
676
+ if (websocket == null) {
677
+ throw new Error(`WebSocket connection is not found!`);
678
+ }
649
679
  websocket.send(JSON.stringify({
650
680
  type: LEGACY_WS.CONNECTION_TERMINATE,
651
681
  }));
652
- websocket.terminate();
653
682
  observer.complete();
683
+ cleanupWebsocket();
654
684
  break;
655
685
  }
656
686
  }
657
687
  };
658
688
  return {
659
689
  unsubscribe: () => {
660
- websocket.send(JSON.stringify({
690
+ websocket === null || websocket === void 0 ? void 0 : websocket.send(JSON.stringify({
661
691
  type: LEGACY_WS.STOP,
662
692
  id,
663
693
  }));
664
- websocket.send(JSON.stringify({
665
- type: LEGACY_WS.CONNECTION_TERMINATE,
666
- }));
667
- websocket.terminate();
694
+ cleanupWebsocket();
668
695
  },
669
696
  };
670
697
  },
@@ -722,13 +749,12 @@ class UrlLoader {
722
749
  }
723
750
  else {
724
751
  const webSocketImpl$ = new valueOrPromise.ValueOrPromise(() => this.getWebSocketImpl(importFn, options));
725
- const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
726
752
  const executor$ = webSocketImpl$.then(webSocketImpl => {
727
753
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.LEGACY_WS) {
728
754
  return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, options);
729
755
  }
730
756
  else {
731
- return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
757
+ return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, options === null || options === void 0 ? void 0 : options.connectionParams);
732
758
  }
733
759
  });
734
760
  return request => executor$.then(executor => executor(request)).resolve();
package/index.mjs CHANGED
@@ -577,28 +577,55 @@ class UrlLoader {
577
577
  https: 'wss',
578
578
  http: 'ws',
579
579
  });
580
+ const observerById = new Map();
581
+ let websocket = null;
582
+ const ensureWebsocket = () => {
583
+ websocket = new WebSocketImpl(WS_URL, 'graphql-ws', {
584
+ followRedirects: true,
585
+ headers: options === null || options === void 0 ? void 0 : options.headers,
586
+ rejectUnauthorized: false,
587
+ skipUTF8Validation: true,
588
+ });
589
+ websocket.onopen = () => {
590
+ let payload = {};
591
+ switch (typeof (options === null || options === void 0 ? void 0 : options.connectionParams)) {
592
+ case 'function':
593
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams();
594
+ break;
595
+ case 'object':
596
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams;
597
+ break;
598
+ }
599
+ websocket.send(JSON.stringify({
600
+ type: LEGACY_WS.CONNECTION_INIT,
601
+ payload,
602
+ }));
603
+ };
604
+ };
605
+ const cleanupWebsocket = () => {
606
+ if (websocket != null && observerById.size === 0) {
607
+ websocket.send(JSON.stringify({
608
+ type: LEGACY_WS.CONNECTION_TERMINATE,
609
+ }));
610
+ websocket.terminate();
611
+ websocket = null;
612
+ }
613
+ };
580
614
  return function legacyExecutor(request) {
581
615
  const id = Date.now().toString();
582
616
  return observableToAsyncIterable({
583
617
  subscribe(observer) {
584
- const websocket = new WebSocketImpl(WS_URL, 'graphql-ws', {
585
- followRedirects: true,
586
- headers: options === null || options === void 0 ? void 0 : options.headers,
587
- rejectUnauthorized: false,
588
- skipUTF8Validation: true,
589
- });
590
- websocket.onopen = () => {
591
- websocket.send(JSON.stringify({
592
- type: LEGACY_WS.CONNECTION_INIT,
593
- payload: {
594
- ...request.extensions,
595
- },
596
- }));
597
- };
618
+ ensureWebsocket();
619
+ if (websocket == null) {
620
+ throw new Error(`WebSocket connection is not found!`);
621
+ }
598
622
  websocket.onmessage = event => {
599
623
  const data = JSON.parse(event.data.toString('utf-8'));
600
624
  switch (data.type) {
601
625
  case LEGACY_WS.CONNECTION_ACK: {
626
+ if (websocket == null) {
627
+ throw new Error(`WebSocket connection is not found!`);
628
+ }
602
629
  websocket.send(JSON.stringify({
603
630
  type: LEGACY_WS.START,
604
631
  id,
@@ -622,25 +649,25 @@ class UrlLoader {
622
649
  break;
623
650
  }
624
651
  case LEGACY_WS.COMPLETE: {
652
+ if (websocket == null) {
653
+ throw new Error(`WebSocket connection is not found!`);
654
+ }
625
655
  websocket.send(JSON.stringify({
626
656
  type: LEGACY_WS.CONNECTION_TERMINATE,
627
657
  }));
628
- websocket.terminate();
629
658
  observer.complete();
659
+ cleanupWebsocket();
630
660
  break;
631
661
  }
632
662
  }
633
663
  };
634
664
  return {
635
665
  unsubscribe: () => {
636
- websocket.send(JSON.stringify({
666
+ websocket === null || websocket === void 0 ? void 0 : websocket.send(JSON.stringify({
637
667
  type: LEGACY_WS.STOP,
638
668
  id,
639
669
  }));
640
- websocket.send(JSON.stringify({
641
- type: LEGACY_WS.CONNECTION_TERMINATE,
642
- }));
643
- websocket.terminate();
670
+ cleanupWebsocket();
644
671
  },
645
672
  };
646
673
  },
@@ -698,13 +725,12 @@ class UrlLoader {
698
725
  }
699
726
  else {
700
727
  const webSocketImpl$ = new ValueOrPromise(() => this.getWebSocketImpl(importFn, options));
701
- const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
702
728
  const executor$ = webSocketImpl$.then(webSocketImpl => {
703
729
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.LEGACY_WS) {
704
730
  return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, options);
705
731
  }
706
732
  else {
707
- return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
733
+ return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, options === null || options === void 0 ? void 0 : options.connectionParams);
708
734
  }
709
735
  });
710
736
  return request => executor$.then(executor => executor(request)).resolve();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/url-loader",
3
- "version": "7.9.24",
3
+ "version": "7.9.25",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {