Skip to content

Commit d718efa

Browse files
committed
fix(url-loader): handle operationName correctly
1 parent ae55af7 commit d718efa

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

packages/loaders/url/src/index.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { fetch as crossFetch } from 'cross-fetch';
2121
import { introspectSchema, wrapSchema } from '@graphql-tools/wrap';
2222
import { ClientOptions, createClient } from 'graphql-ws';
2323
import WebSocket from 'isomorphic-ws';
24-
import syncFetch from 'sync-fetch';
24+
import syncFetchImported from 'sync-fetch';
2525
import isPromise from 'is-promise';
2626
import { extractFiles, isExtractableFile } from 'extract-files';
2727
import FormData from 'form-data';
@@ -33,6 +33,15 @@ import _ from 'lodash';
3333
import { ValueOrPromise } from 'value-or-promise';
3434
import { isLiveQueryOperationDefinitionNode } from '@n1ru4l/graphql-live-query';
3535

36+
const syncFetch: SyncFetchFn = (input: RequestInfo, init?: RequestInit): SyncResponse => {
37+
if (typeof input === 'string') {
38+
delete init?.signal;
39+
} else {
40+
delete (input as any).signal;
41+
}
42+
return syncFetchImported(input, init);
43+
};
44+
3645
export type AsyncFetchFn = typeof import('cross-fetch').fetch;
3746
export type SyncFetchFn = (input: RequestInfo, init?: RequestInit) => SyncResponse;
3847
export type SyncResponse = Omit<Response, 'json' | 'text'> & {
@@ -41,10 +50,8 @@ export type SyncResponse = Omit<Response, 'json' | 'text'> & {
4150
};
4251
export type FetchFn = AsyncFetchFn | SyncFetchFn;
4352

44-
// TODO: Should the types here be changed to T extends Record<string, any> ?
45-
export type AsyncImportFn<T = unknown> = (moduleName: string) => PromiseLike<T>;
46-
// TODO: Should the types here be changed to T extends Record<string, any> ?
47-
export type SyncImportFn<T = unknown> = (moduleName: string) => T;
53+
export type AsyncImportFn = (moduleName: string) => PromiseLike<any>;
54+
export type SyncImportFn = (moduleName: string) => any;
4855

4956
const asyncImport: AsyncImportFn = (moduleName: string) => import(moduleName);
5057
const syncImport: SyncImportFn = (moduleName: string) => require(moduleName);
@@ -472,10 +479,10 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
472479
fetch: AsyncFetchFn,
473480
options?: Omit<LoadFromUrlOptions, 'subscriptionEndpoint'>
474481
): AsyncExecutor<any, ExecutionExtensions> {
475-
return async ({ document, variables, extensions }) => {
482+
return async ({ document, variables, extensions, operationName }) => {
476483
const controller = new AbortController();
477484
const query = print(document);
478-
const finalUrl = this.prepareGETUrl({ baseUrl: endpoint, query, variables });
485+
const finalUrl = this.prepareGETUrl({ baseUrl: endpoint, query, variables, operationName, extensions });
479486
return observableToAsyncIterable({
480487
subscribe: observer => {
481488
const headers = Object.assign({}, options?.headers || {}, extensions?.headers || {});
@@ -530,17 +537,21 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
530537
if (customFetch) {
531538
if (typeof customFetch === 'string') {
532539
const [moduleName, fetchFnName] = customFetch.split('#');
533-
const moduleResult = importFn(moduleName);
534-
if (isPromise(moduleResult)) {
535-
return moduleResult.then(module => (fetchFnName ? (module as Record<string, any>)[fetchFnName] : module));
536-
} else {
537-
return fetchFnName ? (module as Record<string, any>)[fetchFnName] : moduleResult;
538-
}
540+
return new ValueOrPromise(() => importFn(moduleName))
541+
.then(module => (fetchFnName ? (module as Record<string, any>)[fetchFnName] : module))
542+
.resolve();
539543
} else {
540544
return customFetch as any;
541545
}
542546
}
543-
return importFn === asyncImport ? (typeof fetch === 'undefined' ? crossFetch : fetch) : syncFetch;
547+
if (importFn === asyncImport) {
548+
if (typeof fetch === 'undefined') {
549+
return crossFetch as any;
550+
}
551+
return fetch as any;
552+
} else {
553+
return syncFetch;
554+
}
544555
}
545556

546557
private getDefaultMethodFromOptions(method: LoadFromUrlOptions['method'], defaultMethod: 'GET' | 'POST') {
@@ -560,12 +571,9 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
560571
): typeof WebSocket | PromiseLike<typeof WebSocket> {
561572
if (typeof options?.webSocketImpl === 'string') {
562573
const [moduleName, webSocketImplName] = options.webSocketImpl.split('#');
563-
const importedModule = importFn(moduleName);
564-
if (isPromise(importedModule)) {
565-
return importedModule.then(webSocketImplName ? importedModule[webSocketImplName] : importedModule);
566-
} else {
567-
return webSocketImplName ? (importedModule as Record<string, any>)[webSocketImplName] : importedModule;
568-
}
574+
return new ValueOrPromise(() => importFn(moduleName))
575+
.then(importedModule => (webSocketImplName ? importedModule[webSocketImplName] : importedModule))
576+
.resolve();
569577
} else {
570578
const websocketImpl = options?.webSocketImpl || WebSocket;
571579
return websocketImpl;

0 commit comments

Comments
 (0)