Skip to content

Added a normalization of subscriptions endpoint in graphiql_source #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
18 changes: 17 additions & 1 deletion juniper/src/http/graphiql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down