@graphql-tools/url-loader 7.9.0 → 7.9.3

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 +9 -8
  2. package/index.js +55 -45
  3. package/index.mjs +56 -46
  4. package/package.json +4 -5
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="ws" />
2
2
  /// <reference lib="dom" />
3
3
  import { IntrospectionOptions } from 'graphql';
4
- import { AsyncExecutor, SyncExecutor, Source, Loader, BaseLoaderOptions } from '@graphql-tools/utils';
4
+ import { AsyncExecutor, Executor, SyncExecutor, Source, Loader, BaseLoaderOptions } from '@graphql-tools/utils';
5
5
  import { ClientOptions } from 'graphql-ws';
6
6
  import { ClientOptions as GraphQLSSEClientOptions } from 'graphql-sse';
7
7
  import WebSocket from 'isomorphic-ws';
@@ -107,8 +107,6 @@ export interface LoadFromUrlOptions extends BaseLoaderOptions, Partial<Introspec
107
107
  * ```
108
108
  */
109
109
  export declare class UrlLoader implements Loader<LoadFromUrlOptions> {
110
- canLoad(pointer: string, options: LoadFromUrlOptions): Promise<boolean>;
111
- canLoadSync(pointer: string, _options: LoadFromUrlOptions): boolean;
112
110
  createFormDataFromVariables<TVariables>({ query, variables, operationName, extensions, }: {
113
111
  query: string;
114
112
  variables: TVariables;
@@ -124,17 +122,20 @@ export declare class UrlLoader implements Loader<LoadFromUrlOptions> {
124
122
  }): string;
125
123
  buildHTTPExecutor(endpoint: string, fetch: SyncFetchFn, options?: LoadFromUrlOptions): SyncExecutor<any, ExecutionExtensions>;
126
124
  buildHTTPExecutor(endpoint: string, fetch: AsyncFetchFn, options?: LoadFromUrlOptions): AsyncExecutor<any, ExecutionExtensions>;
127
- buildWSExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ClientOptions['connectionParams']): AsyncExecutor;
128
- buildWSLegacyExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ConnectionParamsOptions): AsyncExecutor;
125
+ buildWSExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ClientOptions['connectionParams']): Executor;
126
+ buildWSLegacyExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ConnectionParamsOptions): Executor;
129
127
  buildGraphQLSSEExecutor(endpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionEndpoint'>): AsyncExecutor;
130
128
  getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: AsyncImportFn): PromiseLike<AsyncFetchFn> | AsyncFetchFn;
131
129
  getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: SyncImportFn): SyncFetchFn;
132
130
  private getDefaultMethodFromOptions;
133
131
  getWebSocketImpl(importFn: AsyncImportFn, options?: LoadFromUrlOptions): PromiseLike<typeof WebSocket>;
134
132
  getWebSocketImpl(importFn: SyncImportFn, options?: LoadFromUrlOptions): typeof WebSocket;
135
- buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: AsyncFetchFn, options?: LoadFromUrlOptions): Promise<AsyncExecutor>;
136
- getExecutorAsync(endpoint: string, options?: Omit<LoadFromUrlOptions, 'endpoint'>): Promise<AsyncExecutor>;
137
- getExecutorSync(endpoint: string, options: Omit<LoadFromUrlOptions, 'endpoint'>): SyncExecutor;
133
+ buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: SyncFetchFn, syncImport: SyncImportFn, options?: LoadFromUrlOptions): SyncExecutor;
134
+ buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: AsyncFetchFn, asyncImport: AsyncImportFn, options?: LoadFromUrlOptions): Promise<AsyncExecutor> | AsyncExecutor;
135
+ getExecutor(endpoint: string, asyncImport: AsyncImportFn, options?: Omit<LoadFromUrlOptions, 'endpoint'>): AsyncExecutor;
136
+ getExecutor(endpoint: string, syncImport: SyncImportFn, options?: Omit<LoadFromUrlOptions, 'endpoint'>): SyncExecutor;
137
+ getExecutorAsync(endpoint: string, options?: Omit<LoadFromUrlOptions, 'endpoint'>): AsyncExecutor;
138
+ getExecutorSync(endpoint: string, options?: Omit<LoadFromUrlOptions, 'endpoint'>): SyncExecutor;
138
139
  handleSDL(pointer: string, fetch: SyncFetchFn, options: LoadFromUrlOptions): Source;
139
140
  handleSDL(pointer: string, fetch: AsyncFetchFn, options: LoadFromUrlOptions): Promise<Source>;
140
141
  load(pointer: string, options: LoadFromUrlOptions): Promise<Source[]>;
package/index.js CHANGED
@@ -25,7 +25,6 @@ function _interopNamespace(e) {
25
25
 
26
26
  const graphql = require('graphql');
27
27
  const utils = require('@graphql-tools/utils');
28
- const validUrl = require('valid-url');
29
28
  const wrap = require('@graphql-tools/wrap');
30
29
  const graphqlWs = require('graphql-ws');
31
30
  const graphqlSse = require('graphql-sse');
@@ -296,14 +295,16 @@ const syncImport = (moduleName) => require(moduleName);
296
295
  */
297
296
  SubscriptionProtocol["GRAPHQL_SSE"] = "GRAPHQL_SSE";
298
297
  })(exports.SubscriptionProtocol || (exports.SubscriptionProtocol = {}));
299
- const isCompatibleUri = (uri) => {
300
- if (validUrl.isWebUri(uri)) {
298
+ function isCompatibleUri(uri) {
299
+ try {
300
+ // eslint-disable-next-line no-new
301
+ new URL(uri);
301
302
  return true;
302
303
  }
303
- // we just replace the url part, the remaining validation is the same
304
- const wsUri = uri.replace('wss://', 'http://').replace('ws://', 'http://');
305
- return !!validUrl.isWebUri(wsUri);
306
- };
304
+ catch (_a) {
305
+ return false;
306
+ }
307
+ }
307
308
  /**
308
309
  * This loader loads a schema from a URL. The loaded schema is a fully-executable,
309
310
  * remote schema since it's created using [@graphql-tools/wrap](/docs/remote-schemas).
@@ -317,12 +318,6 @@ const isCompatibleUri = (uri) => {
317
318
  * ```
318
319
  */
319
320
  class UrlLoader {
320
- async canLoad(pointer, options) {
321
- return this.canLoadSync(pointer, options);
322
- }
323
- canLoadSync(pointer, _options) {
324
- return isCompatibleUri(pointer);
325
- }
326
321
  createFormDataFromVariables({ query, variables, operationName, extensions, }) {
327
322
  const vars = Object.assign({}, variables);
328
323
  const { clone, files } = extractFiles.extractFiles(vars, 'variables', ((v) => extractFiles.isExtractableFile(v) ||
@@ -423,7 +418,7 @@ class UrlLoader {
423
418
  method = 'GET';
424
419
  }
425
420
  }
426
- const endpoint = ((_a = request.extensions) === null || _a === void 0 ? void 0 : _a.endpoint) || initialEndpoint;
421
+ const endpoint = ((_a = request.extensions) === null || _a === void 0 ? void 0 : _a.endpoint) || HTTP_URL;
427
422
  const headers = Object.assign({}, options === null || options === void 0 ? void 0 : options.headers, ((_b = request.extensions) === null || _b === void 0 ? void 0 : _b.headers) || {});
428
423
  const acceptedProtocols = [`application/json`];
429
424
  if (method === 'GET' && (options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.SSE) {
@@ -468,7 +463,7 @@ class UrlLoader {
468
463
  case 'POST':
469
464
  if (options === null || options === void 0 ? void 0 : options.multipart) {
470
465
  return new valueOrPromise.ValueOrPromise(() => this.createFormDataFromVariables(requestBody))
471
- .then(form => fetch(HTTP_URL, {
466
+ .then(form => fetch(endpoint, {
472
467
  method: 'POST',
473
468
  credentials,
474
469
  body: form,
@@ -481,7 +476,7 @@ class UrlLoader {
481
476
  .resolve();
482
477
  }
483
478
  else {
484
- return fetch(HTTP_URL, {
479
+ return fetch(endpoint, {
485
480
  method: 'POST',
486
481
  credentials,
487
482
  body: JSON.stringify(requestBody),
@@ -569,7 +564,7 @@ class UrlLoader {
569
564
  connectionParams,
570
565
  lazy: true,
571
566
  });
572
- return async ({ document, variables, operationName, extensions }) => {
567
+ return ({ document, variables, operationName, extensions }) => {
573
568
  const query = graphql.print(document);
574
569
  return utils.observableToAsyncIterable({
575
570
  subscribe: observer => {
@@ -595,7 +590,7 @@ class UrlLoader {
595
590
  connectionParams,
596
591
  lazy: true,
597
592
  }, webSocketImpl);
598
- return async ({ document, variables, operationName }) => {
593
+ return ({ document, variables, operationName }) => {
599
594
  return utils.observableToAsyncIterable(subscriptionClient.request({
600
595
  query: document,
601
596
  variables,
@@ -665,7 +660,7 @@ class UrlLoader {
665
660
  return websocketImpl;
666
661
  }
667
662
  }
668
- async buildSubscriptionExecutor(subscriptionsEndpoint, fetch, options) {
663
+ buildSubscriptionExecutor(subscriptionsEndpoint, fetch, importFn, options) {
669
664
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.SSE) {
670
665
  return this.buildHTTPExecutor(subscriptionsEndpoint, fetch, {
671
666
  ...options,
@@ -681,34 +676,47 @@ class UrlLoader {
681
676
  return this.buildGraphQLSSEExecutor(subscriptionsEndpoint, fetch, options);
682
677
  }
683
678
  else {
684
- const webSocketImpl = await this.getWebSocketImpl(asyncImport, options);
679
+ const webSocketImpl$ = new valueOrPromise.ValueOrPromise(() => this.getWebSocketImpl(importFn, options));
685
680
  const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
686
- if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.LEGACY_WS) {
687
- return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
688
- }
689
- else {
690
- return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
691
- }
681
+ const executor$ = webSocketImpl$.then(webSocketImpl => {
682
+ if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.LEGACY_WS) {
683
+ return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
684
+ }
685
+ else {
686
+ return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
687
+ }
688
+ });
689
+ return request => executor$.then(executor => executor(request)).resolve();
692
690
  }
693
691
  }
694
- async getExecutorAsync(endpoint, options) {
695
- const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
696
- const httpExecutor = this.buildHTTPExecutor(endpoint, fetch, options);
697
- const subscriptionsEndpoint = (options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint) || endpoint;
698
- const subscriptionExecutor = await this.buildSubscriptionExecutor(subscriptionsEndpoint, fetch, options);
699
- return request => {
692
+ getExecutor(endpoint, importFn, options) {
693
+ const fetch$ = new valueOrPromise.ValueOrPromise(() => this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport));
694
+ const httpExecutor$ = fetch$.then(fetch => {
695
+ return this.buildHTTPExecutor(endpoint, fetch, options);
696
+ });
697
+ const subscriptionExecutor$ = fetch$.then(fetch => {
698
+ const subscriptionsEndpoint = (options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint) || endpoint;
699
+ return this.buildSubscriptionExecutor(subscriptionsEndpoint, fetch, importFn, options);
700
+ });
701
+ function getExecutorByRequest(request) {
700
702
  const operationAst = utils.getOperationASTFromRequest(request);
701
703
  if (operationAst.operation === 'subscription' ||
702
704
  graphqlLiveQuery.isLiveQueryOperationDefinitionNode(operationAst, request.variables)) {
703
- return subscriptionExecutor(request);
705
+ return subscriptionExecutor$;
704
706
  }
705
- return httpExecutor(request);
706
- };
707
+ else {
708
+ return httpExecutor$;
709
+ }
710
+ }
711
+ return request => getExecutorByRequest(request)
712
+ .then(executor => executor(request))
713
+ .resolve();
714
+ }
715
+ getExecutorAsync(endpoint, options) {
716
+ return this.getExecutor(endpoint, asyncImport, options);
707
717
  }
708
718
  getExecutorSync(endpoint, options) {
709
- const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
710
- const executor = this.buildHTTPExecutor(endpoint, fetch, options);
711
- return executor;
719
+ return this.getExecutor(endpoint, syncImport, options);
712
720
  }
713
721
  handleSDL(pointer, fetch, options) {
714
722
  const defaultMethod = this.getDefaultMethodFromOptions(options === null || options === void 0 ? void 0 : options.method, 'GET');
@@ -721,15 +729,15 @@ class UrlLoader {
721
729
  .resolve();
722
730
  }
723
731
  async load(pointer, options) {
724
- if (!(await this.canLoad(pointer, options))) {
732
+ if (!isCompatibleUri(pointer)) {
725
733
  return [];
726
734
  }
727
735
  let source = {
728
736
  location: pointer,
729
737
  };
730
- const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
731
- let executor = await this.getExecutorAsync(pointer, options);
738
+ let executor;
732
739
  if ((options === null || options === void 0 ? void 0 : options.handleAsSDL) || pointer.endsWith('.graphql') || pointer.endsWith('.graphqls')) {
740
+ const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
733
741
  source = await this.handleSDL(pointer, fetch, options);
734
742
  if (!source.schema && !source.document && !source.rawSDL) {
735
743
  throw new Error(`Invalid SDL response`);
@@ -743,13 +751,14 @@ class UrlLoader {
743
751
  : undefined);
744
752
  }
745
753
  else {
754
+ executor = this.getExecutorAsync(pointer, options);
746
755
  source.schema = await wrap.introspectSchema(executor, {}, options);
747
756
  }
748
757
  if (!source.schema) {
749
758
  throw new Error(`Invalid introspected schema`);
750
759
  }
751
760
  if (options === null || options === void 0 ? void 0 : options.endpoint) {
752
- executor = await this.getExecutorAsync(options.endpoint, options);
761
+ executor = this.getExecutorAsync(options.endpoint, options);
753
762
  }
754
763
  source.schema = wrap.wrapSchema({
755
764
  schema: source.schema,
@@ -758,15 +767,15 @@ class UrlLoader {
758
767
  return [source];
759
768
  }
760
769
  loadSync(pointer, options) {
761
- if (!this.canLoadSync(pointer, options)) {
770
+ if (!isCompatibleUri(pointer)) {
762
771
  return [];
763
772
  }
764
773
  let source = {
765
774
  location: pointer,
766
775
  };
767
- const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
768
- let executor = this.getExecutorSync(pointer, options);
776
+ let executor;
769
777
  if ((options === null || options === void 0 ? void 0 : options.handleAsSDL) || pointer.endsWith('.graphql') || pointer.endsWith('.graphqls')) {
778
+ const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
770
779
  source = this.handleSDL(pointer, fetch, options);
771
780
  if (!source.schema && !source.document && !source.rawSDL) {
772
781
  throw new Error(`Invalid SDL response`);
@@ -780,6 +789,7 @@ class UrlLoader {
780
789
  : undefined);
781
790
  }
782
791
  else {
792
+ executor = this.getExecutorSync(pointer, options);
783
793
  source.schema = wrap.introspectSchema(executor, {}, options);
784
794
  }
785
795
  if (!source.schema) {
package/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import { print, buildASTSchema, buildSchema } from 'graphql';
2
- import { mapAsyncIterator, isAsyncIterable, inspect, withCancel, observableToAsyncIterable, getOperationASTFromRequest, parseGraphQLSDL } from '@graphql-tools/utils';
3
- import { isWebUri } from 'valid-url';
2
+ import { mapAsyncIterator, isAsyncIterable, inspect, withCancel, observableToAsyncIterable, parseGraphQLSDL, getOperationASTFromRequest } from '@graphql-tools/utils';
4
3
  import { introspectSchema, wrapSchema } from '@graphql-tools/wrap';
5
4
  import { createClient } from 'graphql-ws';
6
5
  import { createClient as createClient$1 } from 'graphql-sse';
@@ -272,14 +271,16 @@ var SubscriptionProtocol;
272
271
  */
273
272
  SubscriptionProtocol["GRAPHQL_SSE"] = "GRAPHQL_SSE";
274
273
  })(SubscriptionProtocol || (SubscriptionProtocol = {}));
275
- const isCompatibleUri = (uri) => {
276
- if (isWebUri(uri)) {
274
+ function isCompatibleUri(uri) {
275
+ try {
276
+ // eslint-disable-next-line no-new
277
+ new URL(uri);
277
278
  return true;
278
279
  }
279
- // we just replace the url part, the remaining validation is the same
280
- const wsUri = uri.replace('wss://', 'http://').replace('ws://', 'http://');
281
- return !!isWebUri(wsUri);
282
- };
280
+ catch (_a) {
281
+ return false;
282
+ }
283
+ }
283
284
  /**
284
285
  * This loader loads a schema from a URL. The loaded schema is a fully-executable,
285
286
  * remote schema since it's created using [@graphql-tools/wrap](/docs/remote-schemas).
@@ -293,12 +294,6 @@ const isCompatibleUri = (uri) => {
293
294
  * ```
294
295
  */
295
296
  class UrlLoader {
296
- async canLoad(pointer, options) {
297
- return this.canLoadSync(pointer, options);
298
- }
299
- canLoadSync(pointer, _options) {
300
- return isCompatibleUri(pointer);
301
- }
302
297
  createFormDataFromVariables({ query, variables, operationName, extensions, }) {
303
298
  const vars = Object.assign({}, variables);
304
299
  const { clone, files } = extractFiles(vars, 'variables', ((v) => isExtractableFile(v) ||
@@ -399,7 +394,7 @@ class UrlLoader {
399
394
  method = 'GET';
400
395
  }
401
396
  }
402
- const endpoint = ((_a = request.extensions) === null || _a === void 0 ? void 0 : _a.endpoint) || initialEndpoint;
397
+ const endpoint = ((_a = request.extensions) === null || _a === void 0 ? void 0 : _a.endpoint) || HTTP_URL;
403
398
  const headers = Object.assign({}, options === null || options === void 0 ? void 0 : options.headers, ((_b = request.extensions) === null || _b === void 0 ? void 0 : _b.headers) || {});
404
399
  const acceptedProtocols = [`application/json`];
405
400
  if (method === 'GET' && (options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.SSE) {
@@ -444,7 +439,7 @@ class UrlLoader {
444
439
  case 'POST':
445
440
  if (options === null || options === void 0 ? void 0 : options.multipart) {
446
441
  return new ValueOrPromise(() => this.createFormDataFromVariables(requestBody))
447
- .then(form => fetch(HTTP_URL, {
442
+ .then(form => fetch(endpoint, {
448
443
  method: 'POST',
449
444
  credentials,
450
445
  body: form,
@@ -457,7 +452,7 @@ class UrlLoader {
457
452
  .resolve();
458
453
  }
459
454
  else {
460
- return fetch(HTTP_URL, {
455
+ return fetch(endpoint, {
461
456
  method: 'POST',
462
457
  credentials,
463
458
  body: JSON.stringify(requestBody),
@@ -545,7 +540,7 @@ class UrlLoader {
545
540
  connectionParams,
546
541
  lazy: true,
547
542
  });
548
- return async ({ document, variables, operationName, extensions }) => {
543
+ return ({ document, variables, operationName, extensions }) => {
549
544
  const query = print(document);
550
545
  return observableToAsyncIterable({
551
546
  subscribe: observer => {
@@ -571,7 +566,7 @@ class UrlLoader {
571
566
  connectionParams,
572
567
  lazy: true,
573
568
  }, webSocketImpl);
574
- return async ({ document, variables, operationName }) => {
569
+ return ({ document, variables, operationName }) => {
575
570
  return observableToAsyncIterable(subscriptionClient.request({
576
571
  query: document,
577
572
  variables,
@@ -641,7 +636,7 @@ class UrlLoader {
641
636
  return websocketImpl;
642
637
  }
643
638
  }
644
- async buildSubscriptionExecutor(subscriptionsEndpoint, fetch, options) {
639
+ buildSubscriptionExecutor(subscriptionsEndpoint, fetch, importFn, options) {
645
640
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.SSE) {
646
641
  return this.buildHTTPExecutor(subscriptionsEndpoint, fetch, {
647
642
  ...options,
@@ -657,34 +652,47 @@ class UrlLoader {
657
652
  return this.buildGraphQLSSEExecutor(subscriptionsEndpoint, fetch, options);
658
653
  }
659
654
  else {
660
- const webSocketImpl = await this.getWebSocketImpl(asyncImport, options);
655
+ const webSocketImpl$ = new ValueOrPromise(() => this.getWebSocketImpl(importFn, options));
661
656
  const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
662
- if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.LEGACY_WS) {
663
- return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
664
- }
665
- else {
666
- return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
667
- }
657
+ const executor$ = webSocketImpl$.then(webSocketImpl => {
658
+ if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.LEGACY_WS) {
659
+ return this.buildWSLegacyExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
660
+ }
661
+ else {
662
+ return this.buildWSExecutor(subscriptionsEndpoint, webSocketImpl, connectionParams);
663
+ }
664
+ });
665
+ return request => executor$.then(executor => executor(request)).resolve();
668
666
  }
669
667
  }
670
- async getExecutorAsync(endpoint, options) {
671
- const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
672
- const httpExecutor = this.buildHTTPExecutor(endpoint, fetch, options);
673
- const subscriptionsEndpoint = (options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint) || endpoint;
674
- const subscriptionExecutor = await this.buildSubscriptionExecutor(subscriptionsEndpoint, fetch, options);
675
- return request => {
668
+ getExecutor(endpoint, importFn, options) {
669
+ const fetch$ = new ValueOrPromise(() => this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport));
670
+ const httpExecutor$ = fetch$.then(fetch => {
671
+ return this.buildHTTPExecutor(endpoint, fetch, options);
672
+ });
673
+ const subscriptionExecutor$ = fetch$.then(fetch => {
674
+ const subscriptionsEndpoint = (options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint) || endpoint;
675
+ return this.buildSubscriptionExecutor(subscriptionsEndpoint, fetch, importFn, options);
676
+ });
677
+ function getExecutorByRequest(request) {
676
678
  const operationAst = getOperationASTFromRequest(request);
677
679
  if (operationAst.operation === 'subscription' ||
678
680
  isLiveQueryOperationDefinitionNode(operationAst, request.variables)) {
679
- return subscriptionExecutor(request);
681
+ return subscriptionExecutor$;
680
682
  }
681
- return httpExecutor(request);
682
- };
683
+ else {
684
+ return httpExecutor$;
685
+ }
686
+ }
687
+ return request => getExecutorByRequest(request)
688
+ .then(executor => executor(request))
689
+ .resolve();
690
+ }
691
+ getExecutorAsync(endpoint, options) {
692
+ return this.getExecutor(endpoint, asyncImport, options);
683
693
  }
684
694
  getExecutorSync(endpoint, options) {
685
- const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
686
- const executor = this.buildHTTPExecutor(endpoint, fetch, options);
687
- return executor;
695
+ return this.getExecutor(endpoint, syncImport, options);
688
696
  }
689
697
  handleSDL(pointer, fetch, options) {
690
698
  const defaultMethod = this.getDefaultMethodFromOptions(options === null || options === void 0 ? void 0 : options.method, 'GET');
@@ -697,15 +705,15 @@ class UrlLoader {
697
705
  .resolve();
698
706
  }
699
707
  async load(pointer, options) {
700
- if (!(await this.canLoad(pointer, options))) {
708
+ if (!isCompatibleUri(pointer)) {
701
709
  return [];
702
710
  }
703
711
  let source = {
704
712
  location: pointer,
705
713
  };
706
- const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
707
- let executor = await this.getExecutorAsync(pointer, options);
714
+ let executor;
708
715
  if ((options === null || options === void 0 ? void 0 : options.handleAsSDL) || pointer.endsWith('.graphql') || pointer.endsWith('.graphqls')) {
716
+ const fetch = await this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, asyncImport);
709
717
  source = await this.handleSDL(pointer, fetch, options);
710
718
  if (!source.schema && !source.document && !source.rawSDL) {
711
719
  throw new Error(`Invalid SDL response`);
@@ -719,13 +727,14 @@ class UrlLoader {
719
727
  : undefined);
720
728
  }
721
729
  else {
730
+ executor = this.getExecutorAsync(pointer, options);
722
731
  source.schema = await introspectSchema(executor, {}, options);
723
732
  }
724
733
  if (!source.schema) {
725
734
  throw new Error(`Invalid introspected schema`);
726
735
  }
727
736
  if (options === null || options === void 0 ? void 0 : options.endpoint) {
728
- executor = await this.getExecutorAsync(options.endpoint, options);
737
+ executor = this.getExecutorAsync(options.endpoint, options);
729
738
  }
730
739
  source.schema = wrapSchema({
731
740
  schema: source.schema,
@@ -734,15 +743,15 @@ class UrlLoader {
734
743
  return [source];
735
744
  }
736
745
  loadSync(pointer, options) {
737
- if (!this.canLoadSync(pointer, options)) {
746
+ if (!isCompatibleUri(pointer)) {
738
747
  return [];
739
748
  }
740
749
  let source = {
741
750
  location: pointer,
742
751
  };
743
- const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
744
- let executor = this.getExecutorSync(pointer, options);
752
+ let executor;
745
753
  if ((options === null || options === void 0 ? void 0 : options.handleAsSDL) || pointer.endsWith('.graphql') || pointer.endsWith('.graphqls')) {
754
+ const fetch = this.getFetch(options === null || options === void 0 ? void 0 : options.customFetch, syncImport);
746
755
  source = this.handleSDL(pointer, fetch, options);
747
756
  if (!source.schema && !source.document && !source.rawSDL) {
748
757
  throw new Error(`Invalid SDL response`);
@@ -756,6 +765,7 @@ class UrlLoader {
756
765
  : undefined);
757
766
  }
758
767
  else {
768
+ executor = this.getExecutorSync(pointer, options);
759
769
  source.schema = introspectSchema(executor, {}, options);
760
770
  }
761
771
  if (!source.schema) {
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@graphql-tools/url-loader",
3
- "version": "7.9.0",
3
+ "version": "7.9.3",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-tools/delegate": "^8.5.1",
11
- "@graphql-tools/utils": "^8.6.2",
12
- "@graphql-tools/wrap": "^8.4.2",
10
+ "@graphql-tools/delegate": "^8.5.3",
11
+ "@graphql-tools/utils": "^8.6.3",
12
+ "@graphql-tools/wrap": "^8.4.5",
13
13
  "@n1ru4l/graphql-live-query": "^0.9.0",
14
14
  "@types/websocket": "^1.0.4",
15
15
  "@types/ws": "^8.0.0",
@@ -23,7 +23,6 @@
23
23
  "subscriptions-transport-ws": "^0.11.0",
24
24
  "sync-fetch": "^0.3.1",
25
25
  "tslib": "^2.3.0",
26
- "valid-url": "^1.0.9",
27
26
  "value-or-promise": "^1.0.11",
28
27
  "ws": "^8.3.0"
29
28
  },