Skip to content

Commit 947ccee

Browse files
committed
feat(url-loader): ability to provide custom subscriptionsEndpoint
1 parent 854ce65 commit 947ccee

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

.changeset/rude-bottles-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/url-loader': minor
3+
---
4+
5+
feat(url-loader): ability to provide custom subscriptionsEndpoint

packages/loaders/url/src/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export interface LoadFromUrlOptions extends SingleFileOptions, Partial<Introspec
108108
* Handle URL as schema SDL
109109
*/
110110
handleAsSDL?: boolean;
111+
/**
112+
* Subscriptions endpoint; defaults to the endpoint given as pointer
113+
*/
114+
subscriptionsEndpoint?: string;
111115
}
112116

113117
/**
@@ -222,12 +226,14 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
222226
const query = print(document);
223227
switch (method) {
224228
case 'GET':
225-
const urlObj = new URL(HTTP_URL);
229+
const dummyHostname = 'https://dummyhostname.com';
230+
const validUrl = HTTP_URL.startsWith('http') ? HTTP_URL : `${dummyHostname}/${HTTP_URL}`;
231+
const urlObj = new URL(validUrl);
226232
urlObj.searchParams.set('query', query);
227233
if (variables && Object.keys(variables).length > 0) {
228234
urlObj.searchParams.set('variables', JSON.stringify(variables));
229235
}
230-
const finalUrl = urlObj.toString();
236+
const finalUrl = urlObj.toString().replace(dummyHostname, '');
231237
fetchResult = fetch(finalUrl, {
232238
method: 'GET',
233239
headers: extraHeaders,
@@ -269,11 +275,11 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
269275
}
270276

271277
buildWSSubscriber(
272-
pointer: string,
278+
subscriptionsEndpoint: string,
273279
webSocketImpl: typeof WebSocket,
274280
connectionParams: ClientOptions['connectionParams']
275281
): Subscriber {
276-
const WS_URL = switchProtocols(pointer, {
282+
const WS_URL = switchProtocols(subscriptionsEndpoint, {
277283
https: 'wss',
278284
http: 'ws',
279285
});
@@ -302,11 +308,11 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
302308
}
303309

304310
buildWSLegacySubscriber(
305-
pointer: string,
311+
subscriptionsEndpoint: string,
306312
webSocketImpl: typeof WebSocket,
307313
connectionParams: ConnectionParamsOptions
308314
): Subscriber {
309-
const WS_URL = switchProtocols(pointer, {
315+
const WS_URL = switchProtocols(subscriptionsEndpoint, {
310316
https: 'wss',
311317
http: 'ws',
312318
});
@@ -455,15 +461,16 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
455461

456462
let subscriber: Subscriber;
457463

464+
const subscriptionsEndpoint = options.subscriptionsEndpoint || pointer;
458465
if (options.useSSEForSubscription) {
459-
subscriber = this.buildSSESubscriber(pointer, options.eventSourceOptions);
466+
subscriber = this.buildSSESubscriber(subscriptionsEndpoint, options.eventSourceOptions);
460467
} else {
461468
const webSocketImpl = await this.getWebSocketImpl(options, asyncImport);
462469

463470
if (options.useWebSocketLegacyProtocol) {
464-
subscriber = this.buildWSLegacySubscriber(pointer, webSocketImpl, { headers });
471+
subscriber = this.buildWSLegacySubscriber(subscriptionsEndpoint, webSocketImpl, { headers });
465472
} else {
466-
subscriber = this.buildWSSubscriber(pointer, webSocketImpl, { headers });
473+
subscriber = this.buildWSSubscriber(subscriptionsEndpoint, webSocketImpl, { headers });
467474
}
468475
}
469476

@@ -494,15 +501,16 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
494501
useGETForQueries: options.useGETForQueries,
495502
});
496503

504+
const subscriptionsEndpoint = options.subscriptionsEndpoint || pointer;
497505
let subscriber: Subscriber;
498506
if (options.useSSEForSubscription) {
499-
subscriber = this.buildSSESubscriber(pointer, options.eventSourceOptions);
507+
subscriber = this.buildSSESubscriber(subscriptionsEndpoint, options.eventSourceOptions);
500508
} else {
501509
const webSocketImpl = this.getWebSocketImpl(options, syncImport);
502510
if (options.useWebSocketLegacyProtocol) {
503-
subscriber = this.buildWSLegacySubscriber(pointer, webSocketImpl, { headers });
511+
subscriber = this.buildWSLegacySubscriber(subscriptionsEndpoint, webSocketImpl, { headers });
504512
} else {
505-
subscriber = this.buildWSSubscriber(pointer, webSocketImpl, { headers });
513+
subscriber = this.buildWSSubscriber(subscriptionsEndpoint, webSocketImpl, { headers });
506514
}
507515
}
508516

patches/@changesets+apply-release-plan+4.2.0.patch renamed to patches/@changesets+apply-release-plan+5.0.0.patch

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.dev.js b/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.dev.js
2-
index 8b33aee..5cb7113 100644
2+
index 5435d7a..a95a3ad 100644
33
--- a/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.dev.js
44
+++ b/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.dev.js
55
@@ -117,7 +117,8 @@ const DEPENDENCY_TYPES = ["dependencies", "devDependencies", "peerDependencies",
@@ -21,7 +21,7 @@ index 8b33aee..5cb7113 100644
2121
if (usesWorkspaceRange) newNewRange = `workspace:${newNewRange}`;
2222
deps[name] = newNewRange;
2323
}
24-
@@ -271,7 +272,7 @@ async function getCommitsThatAddChangesets(changesetIds, cwd) {
24+
@@ -273,7 +274,7 @@ async function getCommitsThatAddChangesets(changesetIds, cwd) {
2525
return commits;
2626
}
2727

@@ -30,7 +30,7 @@ index 8b33aee..5cb7113 100644
3030
let cwd = packages.root.dir;
3131
let touchedFiles = [];
3232
const packagesByName = new Map(packages.packages.map(x => [x.packageJson.name, x]));
33-
@@ -309,6 +310,7 @@ async function applyReleasePlan(releasePlan, packages, config$1 = config.default
33+
@@ -311,6 +312,7 @@ async function applyReleasePlan(releasePlan, packages, config$1 = config.default
3434
let finalisedRelease = releaseWithChangelogs.map(release => {
3535
return versionPackage(release, versionsToUpdate, {
3636
updateInternalDependencies: config$1.updateInternalDependencies,
@@ -39,7 +39,7 @@ index 8b33aee..5cb7113 100644
3939
bumpVersionsWithWorkspaceProtocolOnly: config$1.bumpVersionsWithWorkspaceProtocolOnly
4040
});
4141
diff --git a/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.prod.js b/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.prod.js
42-
index 9e9d03c..5ea12d6 100644
42+
index f83761f..bc94382 100644
4343
--- a/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.prod.js
4444
+++ b/node_modules/@changesets/apply-release-plan/dist/apply-release-plan.cjs.prod.js
4545
@@ -64,7 +64,7 @@ function shouldUpdateDependencyBasedOnConfig(release, {depVersionRange: depVersi
@@ -60,7 +60,7 @@ index 9e9d03c..5ea12d6 100644
6060
usesWorkspaceRange && (newNewRange = "workspace:" + newNewRange), deps[name] = newNewRange;
6161
}
6262
}
63-
@@ -160,7 +160,7 @@ async function getCommitsThatAddChangesets(changesetIds, cwd) {
63+
@@ -161,7 +161,7 @@ async function getCommitsThatAddChangesets(changesetIds, cwd) {
6464
})), commits;
6565
}
6666

@@ -69,7 +69,7 @@ index 9e9d03c..5ea12d6 100644
6969
let cwd = packages.root.dir, touchedFiles = [];
7070
const packagesByName = new Map(packages.packages.map((x => [ x.packageJson.name, x ])));
7171
let {releases: releases, changesets: changesets} = releasePlan;
72-
@@ -177,6 +177,7 @@ async function applyReleasePlan(releasePlan, packages, config$1 = config.default
72+
@@ -178,6 +178,7 @@ async function applyReleasePlan(releasePlan, packages, config$1 = config.default
7373
type: type
7474
}))), finalisedRelease = releaseWithChangelogs.map((release => versionPackage(release, versionsToUpdate, {
7575
updateInternalDependencies: config$1.updateInternalDependencies,

patches/@changesets+cli+2.15.0.patch renamed to patches/@changesets+cli+2.16.0.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/node_modules/@changesets/cli/dist/cli.cjs.dev.js b/node_modules/@changesets/cli/dist/cli.cjs.dev.js
2-
index 14f0fdf..a5cc31a 100644
2+
index f771824..a2fb0ce 100644
33
--- a/node_modules/@changesets/cli/dist/cli.cjs.dev.js
44
+++ b/node_modules/@changesets/cli/dist/cli.cjs.dev.js
5-
@@ -570,7 +570,7 @@ async function version(cwd, options, config) {
5+
@@ -576,7 +576,7 @@ async function version(cwd, options, config) {
66
// @ts-ignore
77
var isCI = !!(isCI__default['default'] || process.env.GITHUB_ACTIONS);
88

@@ -12,10 +12,10 @@ index 14f0fdf..a5cc31a 100644
1212

1313
function jsonParse(input) {
1414
diff --git a/node_modules/@changesets/cli/dist/cli.esm.js b/node_modules/@changesets/cli/dist/cli.esm.js
15-
index 1c227ab..b84e188 100644
15+
index 40bcc31..4b376a3 100644
1616
--- a/node_modules/@changesets/cli/dist/cli.esm.js
1717
+++ b/node_modules/@changesets/cli/dist/cli.esm.js
18-
@@ -547,7 +547,7 @@ async function version(cwd, options, config) {
18+
@@ -553,7 +553,7 @@ async function version(cwd, options, config) {
1919
// @ts-ignore
2020
var isCI = !!(isCI$1 || process.env.GITHUB_ACTIONS);
2121

yarn.lock

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4316,6 +4316,11 @@ babel-preset-jest@^26.6.2:
43164316
babel-plugin-jest-hoist "^26.6.2"
43174317
babel-preset-current-node-syntax "^1.0.0"
43184318

4319+
backo2@^1.0.2:
4320+
version "1.0.2"
4321+
resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947"
4322+
integrity sha1-MasayLEpNjRj41s+u2n038+6eUc=
4323+
43194324
bail@^1.0.0:
43204325
version "1.0.5"
43214326
resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.5.tgz#b6fa133404a392cbc1f8c4bf63f5953351e7a776"
@@ -6772,6 +6777,11 @@ eval@^0.1.4:
67726777
dependencies:
67736778
require-like ">= 0.1.1"
67746779

6780+
eventemitter3@^3.1.0:
6781+
version "3.1.2"
6782+
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
6783+
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==
6784+
67756785
eventemitter3@^4.0.0:
67766786
version "4.0.7"
67776787
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
@@ -8980,7 +8990,7 @@ istanbul-reports@^3.0.2:
89808990
html-escaper "^2.0.0"
89818991
istanbul-lib-report "^3.0.0"
89828992

8983-
iterall@^1.3.0:
8993+
iterall@^1.2.1, iterall@^1.3.0:
89848994
version "1.3.0"
89858995
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
89868996
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==
@@ -14205,6 +14215,17 @@ stylehacks@^4.0.0:
1420514215
postcss "^7.0.0"
1420614216
postcss-selector-parser "^3.0.0"
1420714217

14218+
subscriptions-transport-ws@^0.9.18:
14219+
version "0.9.18"
14220+
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.18.tgz#bcf02320c911fbadb054f7f928e51c6041a37b97"
14221+
integrity sha512-tztzcBTNoEbuErsVQpTN2xUNN/efAZXyCyL5m3x4t6SKrEiTL2N8SaKWBFWM4u56pL79ULif3zjyeq+oV+nOaA==
14222+
dependencies:
14223+
backo2 "^1.0.2"
14224+
eventemitter3 "^3.1.0"
14225+
iterall "^1.2.1"
14226+
symbol-observable "^1.0.4"
14227+
ws "^5.2.0"
14228+
1420814229
supports-color@^5.3.0, supports-color@^5.4.0, supports-color@^5.5.0:
1420914230
version "5.5.0"
1421014231
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
@@ -14265,6 +14286,11 @@ svgo@^1.0.0, svgo@^1.2.2:
1426514286
unquote "~1.1.1"
1426614287
util.promisify "~1.0.0"
1426714288

14289+
symbol-observable@^1.0.4:
14290+
version "1.2.0"
14291+
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
14292+
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==
14293+
1426814294
symbol-observable@^2.0.0:
1426914295
version "2.0.3"
1427014296
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-2.0.3.tgz#5b521d3d07a43c351055fa43b8355b62d33fd16a"
@@ -15724,6 +15750,13 @@ [email protected]:
1572415750
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.5.tgz#a484dd851e9beb6fdb420027e3885e8ce48986c1"
1572515751
integrity sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g==
1572615752

15753+
ws@^5.2.0:
15754+
version "5.2.2"
15755+
resolved "https://registry.yarnpkg.com/ws/-/ws-5.2.2.tgz#dffef14866b8e8dc9133582514d1befaf96e980f"
15756+
integrity sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==
15757+
dependencies:
15758+
async-limiter "~1.0.0"
15759+
1572715760
ws@^6.2.1:
1572815761
version "6.2.1"
1572915762
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"

0 commit comments

Comments
 (0)