@graphql-tools/url-loader 7.1.1-alpha-5d885fa3.0 → 7.2.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/index.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import { IntrospectionOptions } from 'graphql';
4
4
  import { AsyncExecutor, SyncExecutor, Source, Loader, BaseLoaderOptions } from '@graphql-tools/utils';
5
5
  import { ClientOptions } from 'graphql-ws';
6
+ import { ClientOptions as GraphQLSSEClientOptions } from 'graphql-sse';
6
7
  import WebSocket from 'isomorphic-ws';
7
8
  import FormData from 'form-data';
8
9
  import { FetchEventSourceInit } from '@ardatan/fetch-event-source';
@@ -29,7 +30,11 @@ export declare enum SubscriptionProtocol {
29
30
  /**
30
31
  * Use SSE for subscription instead of WebSocket
31
32
  */
32
- SSE = "SSE"
33
+ SSE = "SSE",
34
+ /**
35
+ * Use `graphql-sse` for subscriptions
36
+ */
37
+ GRAPHQL_SSE = "GRAPHQL_SSE"
33
38
  }
34
39
  /**
35
40
  * Additional options for loading from a URL
@@ -81,6 +86,10 @@ export interface LoadFromUrlOptions extends BaseLoaderOptions, Partial<Introspec
81
86
  * Use specific protocol for subscriptions
82
87
  */
83
88
  subscriptionsProtocol?: SubscriptionProtocol;
89
+ /**
90
+ * Additional options to pass to the graphql-sse client.
91
+ */
92
+ graphqlSseOptions?: Omit<GraphQLSSEClientOptions, 'url' | 'headers' | 'fetchFn' | 'abortControllerImpl'>;
84
93
  }
85
94
  /**
86
95
  * This loader loads a schema from a URL. The loaded schema is a fully-executable,
@@ -115,12 +124,13 @@ export declare class UrlLoader implements Loader<LoadFromUrlOptions> {
115
124
  buildWSExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ClientOptions['connectionParams']): AsyncExecutor;
116
125
  buildWSLegacyExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ConnectionParamsOptions): AsyncExecutor;
117
126
  buildSSEExecutor(endpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionEndpoint'>): AsyncExecutor<any, ExecutionExtensions>;
127
+ buildGraphQLSSEExecutor(endpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionEndpoint'>): AsyncExecutor;
118
128
  getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: AsyncImportFn): PromiseLike<AsyncFetchFn>;
119
129
  getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: SyncImportFn): SyncFetchFn;
120
130
  private getDefaultMethodFromOptions;
121
131
  getWebSocketImpl(importFn: AsyncImportFn, options?: LoadFromUrlOptions): PromiseLike<typeof WebSocket>;
122
132
  getWebSocketImpl(importFn: SyncImportFn, options?: LoadFromUrlOptions): typeof WebSocket;
123
- buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionsEndpoint'>): Promise<AsyncExecutor>;
133
+ buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: AsyncFetchFn, options?: LoadFromUrlOptions): Promise<AsyncExecutor>;
124
134
  getExecutorAsync(endpoint: string, options?: Omit<LoadFromUrlOptions, 'endpoint'>): Promise<AsyncExecutor>;
125
135
  getExecutorSync(endpoint: string, options: Omit<LoadFromUrlOptions, 'endpoint'>): SyncExecutor;
126
136
  handleSDL(pointer: string, fetch: SyncFetchFn, options: LoadFromUrlOptions): Source;
package/index.js CHANGED
@@ -29,6 +29,7 @@ const validUrl = require('valid-url');
29
29
  const crossFetch = require('cross-fetch');
30
30
  const wrap = require('@graphql-tools/wrap');
31
31
  const graphqlWs = require('graphql-ws');
32
+ const graphqlSse = require('graphql-sse');
32
33
  const WebSocket = _interopDefault(require('isomorphic-ws'));
33
34
  const syncFetchImported = _interopDefault(require('sync-fetch'));
34
35
  const isPromise = _interopDefault(require('is-promise'));
@@ -64,6 +65,10 @@ const syncImport = (moduleName) => require(moduleName);
64
65
  * Use SSE for subscription instead of WebSocket
65
66
  */
66
67
  SubscriptionProtocol["SSE"] = "SSE";
68
+ /**
69
+ * Use `graphql-sse` for subscriptions
70
+ */
71
+ SubscriptionProtocol["GRAPHQL_SSE"] = "GRAPHQL_SSE";
67
72
  })(exports.SubscriptionProtocol || (exports.SubscriptionProtocol = {}));
68
73
  const isCompatibleUri = (uri) => {
69
74
  if (validUrl.isWebUri(uri)) {
@@ -356,6 +361,31 @@ class UrlLoader {
356
361
  });
357
362
  };
358
363
  }
364
+ buildGraphQLSSEExecutor(endpoint, fetch, options = {}) {
365
+ const { headers } = options;
366
+ const client = graphqlSse.createClient({
367
+ ...options.graphqlSseOptions,
368
+ url: endpoint,
369
+ fetchFn: fetch,
370
+ abortControllerImpl: AbortController,
371
+ headers,
372
+ });
373
+ return async ({ document, variables, operationName, extensions }) => {
374
+ return utils.observableToAsyncIterable({
375
+ subscribe: observer => {
376
+ const unsubscribe = client.subscribe({
377
+ query: document,
378
+ variables: variables,
379
+ operationName,
380
+ extensions,
381
+ }, observer);
382
+ return {
383
+ unsubscribe,
384
+ };
385
+ },
386
+ });
387
+ };
388
+ }
359
389
  getFetch(customFetch, importFn) {
360
390
  if (customFetch) {
361
391
  if (typeof customFetch === 'string') {
@@ -400,6 +430,14 @@ class UrlLoader {
400
430
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.SSE) {
401
431
  return this.buildSSEExecutor(subscriptionsEndpoint, fetch, options);
402
432
  }
433
+ else if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === exports.SubscriptionProtocol.GRAPHQL_SSE) {
434
+ if (!(options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint)) {
435
+ // when no custom subscriptions endpoint is specified,
436
+ // graphql-sse is recommended to be used on `/graphql/stream`
437
+ subscriptionsEndpoint += '/stream';
438
+ }
439
+ return this.buildGraphQLSSEExecutor(subscriptionsEndpoint, fetch, options);
440
+ }
403
441
  else {
404
442
  const webSocketImpl = await this.getWebSocketImpl(asyncImport, options);
405
443
  const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
package/index.mjs CHANGED
@@ -4,6 +4,7 @@ import { isWebUri } from 'valid-url';
4
4
  import { fetch as fetch$1 } from 'cross-fetch';
5
5
  import { introspectSchema, wrapSchema } from '@graphql-tools/wrap';
6
6
  import { createClient } from 'graphql-ws';
7
+ import { createClient as createClient$1 } from 'graphql-sse';
7
8
  import WebSocket from 'isomorphic-ws';
8
9
  import syncFetchImported from 'sync-fetch';
9
10
  import isPromise from 'is-promise';
@@ -40,6 +41,10 @@ var SubscriptionProtocol;
40
41
  * Use SSE for subscription instead of WebSocket
41
42
  */
42
43
  SubscriptionProtocol["SSE"] = "SSE";
44
+ /**
45
+ * Use `graphql-sse` for subscriptions
46
+ */
47
+ SubscriptionProtocol["GRAPHQL_SSE"] = "GRAPHQL_SSE";
43
48
  })(SubscriptionProtocol || (SubscriptionProtocol = {}));
44
49
  const isCompatibleUri = (uri) => {
45
50
  if (isWebUri(uri)) {
@@ -332,6 +337,31 @@ class UrlLoader {
332
337
  });
333
338
  };
334
339
  }
340
+ buildGraphQLSSEExecutor(endpoint, fetch, options = {}) {
341
+ const { headers } = options;
342
+ const client = createClient$1({
343
+ ...options.graphqlSseOptions,
344
+ url: endpoint,
345
+ fetchFn: fetch,
346
+ abortControllerImpl: AbortController,
347
+ headers,
348
+ });
349
+ return async ({ document, variables, operationName, extensions }) => {
350
+ return observableToAsyncIterable({
351
+ subscribe: observer => {
352
+ const unsubscribe = client.subscribe({
353
+ query: document,
354
+ variables: variables,
355
+ operationName,
356
+ extensions,
357
+ }, observer);
358
+ return {
359
+ unsubscribe,
360
+ };
361
+ },
362
+ });
363
+ };
364
+ }
335
365
  getFetch(customFetch, importFn) {
336
366
  if (customFetch) {
337
367
  if (typeof customFetch === 'string') {
@@ -376,6 +406,14 @@ class UrlLoader {
376
406
  if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.SSE) {
377
407
  return this.buildSSEExecutor(subscriptionsEndpoint, fetch, options);
378
408
  }
409
+ else if ((options === null || options === void 0 ? void 0 : options.subscriptionsProtocol) === SubscriptionProtocol.GRAPHQL_SSE) {
410
+ if (!(options === null || options === void 0 ? void 0 : options.subscriptionsEndpoint)) {
411
+ // when no custom subscriptions endpoint is specified,
412
+ // graphql-sse is recommended to be used on `/graphql/stream`
413
+ subscriptionsEndpoint += '/stream';
414
+ }
415
+ return this.buildGraphQLSSEExecutor(subscriptionsEndpoint, fetch, options);
416
+ }
379
417
  else {
380
418
  const webSocketImpl = await this.getWebSocketImpl(asyncImport, options);
381
419
  const connectionParams = () => ({ headers: options === null || options === void 0 ? void 0 : options.headers });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/url-loader",
3
- "version": "7.1.1-alpha-5d885fa3.0",
3
+ "version": "7.2.0",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -8,16 +8,17 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@ardatan/fetch-event-source": "2.0.2",
11
- "@graphql-tools/delegate": "8.2.2-alpha-5d885fa3.0",
11
+ "@graphql-tools/delegate": "^8.2.0",
12
12
  "@graphql-tools/utils": "^8.2.0",
13
- "@graphql-tools/wrap": "8.1.1-alpha-5d885fa3.0",
14
- "@n1ru4l/graphql-live-query": "0.7.1",
13
+ "@graphql-tools/wrap": "^8.1.0",
14
+ "@n1ru4l/graphql-live-query": "0.8.1",
15
15
  "@types/websocket": "1.0.4",
16
16
  "@types/ws": "^7.4.7",
17
17
  "abort-controller": "3.0.0",
18
18
  "cross-fetch": "3.1.4",
19
19
  "extract-files": "11.0.0",
20
20
  "form-data": "4.0.0",
21
+ "graphql-sse": "^1.0.1",
21
22
  "graphql-ws": "^5.4.1",
22
23
  "is-promise": "4.0.0",
23
24
  "isomorphic-ws": "4.0.1",
@@ -28,7 +29,7 @@
28
29
  "tslib": "~2.3.0",
29
30
  "valid-url": "1.0.9",
30
31
  "value-or-promise": "1.0.10",
31
- "ws": "8.2.1"
32
+ "ws": "8.2.2"
32
33
  },
33
34
  "repository": {
34
35
  "type": "git",
package/es5/index.d.ts DELETED
@@ -1,131 +0,0 @@
1
- /// <reference types="ws" />
2
- /// <reference lib="dom" />
3
- import { IntrospectionOptions } from 'graphql';
4
- import { AsyncExecutor, SyncExecutor, Source, Loader, BaseLoaderOptions } from '@graphql-tools/utils';
5
- import { ClientOptions } from 'graphql-ws';
6
- import WebSocket from 'isomorphic-ws';
7
- import FormData from 'form-data';
8
- import { FetchEventSourceInit } from '@ardatan/fetch-event-source';
9
- import { ConnectionParamsOptions } from 'subscriptions-transport-ws';
10
- export declare type AsyncFetchFn = typeof import('cross-fetch').fetch;
11
- export declare type SyncFetchFn = (input: RequestInfo, init?: RequestInit) => SyncResponse;
12
- export declare type SyncResponse = Omit<Response, 'json' | 'text'> & {
13
- json: () => any;
14
- text: () => string;
15
- };
16
- export declare type FetchFn = AsyncFetchFn | SyncFetchFn;
17
- export declare type AsyncImportFn = (moduleName: string) => PromiseLike<any>;
18
- export declare type SyncImportFn = (moduleName: string) => any;
19
- declare type HeadersConfig = Record<string, string>;
20
- interface ExecutionExtensions {
21
- headers?: HeadersConfig;
22
- }
23
- export declare enum SubscriptionProtocol {
24
- WS = "WS",
25
- /**
26
- * Use legacy web socket protocol `graphql-ws` instead of the more current standard `graphql-transport-ws`
27
- */
28
- LEGACY_WS = "LEGACY_WS",
29
- /**
30
- * Use SSE for subscription instead of WebSocket
31
- */
32
- SSE = "SSE"
33
- }
34
- /**
35
- * Additional options for loading from a URL
36
- */
37
- export interface LoadFromUrlOptions extends BaseLoaderOptions, Partial<IntrospectionOptions> {
38
- /**
39
- * Additional headers to include when querying the original schema
40
- */
41
- headers?: HeadersConfig;
42
- /**
43
- * A custom `fetch` implementation to use when querying the original schema.
44
- * Defaults to `cross-fetch`
45
- */
46
- customFetch?: FetchFn | string;
47
- /**
48
- * HTTP method to use when querying the original schema.
49
- */
50
- method?: 'GET' | 'POST';
51
- /**
52
- * Custom WebSocket implementation used by the loaded schema if subscriptions
53
- * are enabled
54
- */
55
- webSocketImpl?: typeof WebSocket | string;
56
- /**
57
- * Whether to use the GET HTTP method for queries when querying the original schema
58
- */
59
- useGETForQueries?: boolean;
60
- /**
61
- * Use multipart for POST requests
62
- */
63
- multipart?: boolean;
64
- /**
65
- * Additional options to pass to the constructor of the underlying EventSource instance.
66
- */
67
- eventSourceOptions?: FetchEventSourceInit;
68
- /**
69
- * Handle URL as schema SDL
70
- */
71
- handleAsSDL?: boolean;
72
- /**
73
- * Regular HTTP endpoint; defaults to the pointer
74
- */
75
- endpoint?: string;
76
- /**
77
- * Subscriptions endpoint; defaults to the endpoint given as HTTP endpoint
78
- */
79
- subscriptionsEndpoint?: string;
80
- /**
81
- * Use specific protocol for subscriptions
82
- */
83
- subscriptionsProtocol?: SubscriptionProtocol;
84
- }
85
- /**
86
- * This loader loads a schema from a URL. The loaded schema is a fully-executable,
87
- * remote schema since it's created using [@graphql-tools/wrap](/docs/remote-schemas).
88
- *
89
- * ```
90
- * const schema = await loadSchema('http://localhost:3000/graphql', {
91
- * loaders: [
92
- * new UrlLoader(),
93
- * ]
94
- * });
95
- * ```
96
- */
97
- export declare class UrlLoader implements Loader<LoadFromUrlOptions> {
98
- canLoad(pointer: string, options: LoadFromUrlOptions): Promise<boolean>;
99
- canLoadSync(pointer: string, _options: LoadFromUrlOptions): boolean;
100
- createFormDataFromVariables<TVariables>({ query, variables, operationName, extensions, }: {
101
- query: string;
102
- variables: TVariables;
103
- operationName?: string;
104
- extensions?: any;
105
- }): FormData | Promise<FormData>;
106
- prepareGETUrl({ baseUrl, query, variables, operationName, extensions, }: {
107
- baseUrl: string;
108
- query: string;
109
- variables: any;
110
- operationName?: string;
111
- extensions?: any;
112
- }): string;
113
- buildHTTPExecutor(endpoint: string, fetch: SyncFetchFn, options?: LoadFromUrlOptions): SyncExecutor<any, ExecutionExtensions>;
114
- buildHTTPExecutor(endpoint: string, fetch: AsyncFetchFn, options?: LoadFromUrlOptions): AsyncExecutor<any, ExecutionExtensions>;
115
- buildWSExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ClientOptions['connectionParams']): AsyncExecutor;
116
- buildWSLegacyExecutor(subscriptionsEndpoint: string, webSocketImpl: typeof WebSocket, connectionParams?: ConnectionParamsOptions): AsyncExecutor;
117
- buildSSEExecutor(endpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionEndpoint'>): AsyncExecutor<any, ExecutionExtensions>;
118
- getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: AsyncImportFn): PromiseLike<AsyncFetchFn>;
119
- getFetch(customFetch: LoadFromUrlOptions['customFetch'], importFn: SyncImportFn): SyncFetchFn;
120
- private getDefaultMethodFromOptions;
121
- getWebSocketImpl(importFn: AsyncImportFn, options?: LoadFromUrlOptions): PromiseLike<typeof WebSocket>;
122
- getWebSocketImpl(importFn: SyncImportFn, options?: LoadFromUrlOptions): typeof WebSocket;
123
- buildSubscriptionExecutor(subscriptionsEndpoint: string, fetch: AsyncFetchFn, options?: Omit<LoadFromUrlOptions, 'subscriptionsEndpoint'>): Promise<AsyncExecutor>;
124
- getExecutorAsync(endpoint: string, options?: Omit<LoadFromUrlOptions, 'endpoint'>): Promise<AsyncExecutor>;
125
- getExecutorSync(endpoint: string, options: Omit<LoadFromUrlOptions, 'endpoint'>): SyncExecutor;
126
- handleSDL(pointer: string, fetch: SyncFetchFn, options: LoadFromUrlOptions): Source;
127
- handleSDL(pointer: string, fetch: AsyncFetchFn, options: LoadFromUrlOptions): Promise<Source>;
128
- load(pointer: string, options: LoadFromUrlOptions): Promise<Source[]>;
129
- loadSync(pointer: string, options: LoadFromUrlOptions): Source[];
130
- }
131
- export {};