Skip to content

Commit 40ad17c

Browse files
authored
Normalize subscriptions endpoint in graphiql_source (#628)
The objective here is to make the Playground and GraphiQL have the same behavior with the endpoint arguments.
1 parent 5cf21bf commit 40ad17c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

juniper/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Features
44

5+
- Normalization for the subscriptions_endpoint_url in the `graphiql_source`.
6+
(See [#628](https://github.com/graphql-rust/juniper/pull/628) for more details)
7+
58
- Support raw identifiers in field and argument names. (`#[object]` macro)
69

710
- Most error types now implement `std::error::Error`:

juniper/src/http/graphiql.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ pub fn graphiql_source(
3131
let fetcher_source = r#"
3232
<script>
3333
if (usingSubscriptions) {
34-
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(GRAPHQL_SUBSCRIPTIONS_URL, { reconnect: true });
34+
var subscriptionEndpoint = normalizeSubscriptionEndpoint(GRAPHQL_URL, GRAPHQL_SUBSCRIPTIONS_URL);
35+
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(subscriptionEndpoint, { reconnect: true });
36+
}
37+
38+
function normalizeSubscriptionEndpoint(endpoint, subscriptionEndpoint) {
39+
if (subscriptionEndpoint) {
40+
if (subscriptionEndpoint.startsWith('/')) {
41+
const secure =
42+
endpoint.includes('https') || location.href.includes('https')
43+
? 's'
44+
: ''
45+
return `ws${secure}://${location.host}${subscriptionEndpoint}`
46+
} else {
47+
return subscriptionEndpoint.replace(/^http/, 'ws')
48+
}
49+
}
50+
return null
3551
}
3652
3753
function graphQLFetcher(params) {

0 commit comments

Comments
 (0)