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.
This commit is contained in:
Jordão Rodrigues Oliveira Rosario 2020-06-04 14:45:19 -07:00 committed by GitHub
parent 5cf21bf41b
commit 40ad17c540
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -2,6 +2,9 @@
## Features
- Normalization for the subscriptions_endpoint_url in the `graphiql_source`.
(See [#628](https://github.com/graphql-rust/juniper/pull/628) for more details)
- Support raw identifiers in field and argument names. (`#[object]` macro)
- Most error types now implement `std::error::Error`:

View file

@ -31,7 +31,23 @@ pub fn graphiql_source(
let fetcher_source = r#"
<script>
if (usingSubscriptions) {
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(GRAPHQL_SUBSCRIPTIONS_URL, { reconnect: true });
var subscriptionEndpoint = normalizeSubscriptionEndpoint(GRAPHQL_URL, GRAPHQL_SUBSCRIPTIONS_URL);
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(subscriptionEndpoint, { reconnect: true });
}
function normalizeSubscriptionEndpoint(endpoint, subscriptionEndpoint) {
if (subscriptionEndpoint) {
if (subscriptionEndpoint.startsWith('/')) {
const secure =
endpoint.includes('https') || location.href.includes('https')
? 's'
: ''
return `ws${secure}://${location.host}${subscriptionEndpoint}`
} else {
return subscriptionEndpoint.replace(/^http/, 'ws')
}
}
return null
}
function graphQLFetcher(params) {