Skip to content

Commit 7807ba7

Browse files
committed
graceful subscription url handling
1 parent 8fbc019 commit 7807ba7

File tree

7 files changed

+60
-42
lines changed

7 files changed

+60
-42
lines changed

packages/graphql-playground-middleware-express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-playground-middleware-express",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"homepage": "https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware-express",
55
"description": "GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).",
66
"contributors": [

packages/graphql-playground-middleware-hapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-playground-middleware-hapi",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"homepage": "https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware-hapi",
55
"description": "GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).",
66
"contributors": [

packages/graphql-playground-middleware-koa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-playground-middleware-koa",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"homepage": "https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware-koa",
55
"description": "GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).",
66
"contributors": [

packages/graphql-playground-middleware-lambda/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-playground-middleware-lambda",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"homepage": "https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware-lambada",
55
"description": "GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).",
66
"contributors": [

packages/graphql-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-playground",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"main": "./lib/lib.js",
55
"typings": "./lib/lib.d.ts",
66
"description": "GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).",

packages/graphql-playground/src/components/MiddlewareApp.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,28 @@ class MiddlewareApp extends React.Component<Props, State> {
3939
const endpoint =
4040
props.endpoint || getParameterByName('endpoint') || location.href
4141

42+
const subscriptionEndpoint =
43+
props.subscriptionEndpoint || getParameterByName('subscriptionEndpoint')
44+
4245
this.state = {
4346
endpoint,
4447
platformToken: localStorage.getItem('platform-token') || undefined,
45-
subscriptionEndpoint:
46-
props.subscriptionEndpoint ||
47-
getParameterByName('subscriptionEndpoint') ||
48-
'',
48+
subscriptionEndpoint: subscriptionEndpoint
49+
? this.normalizeSubscriptionUrl(endpoint, subscriptionEndpoint)
50+
: '',
4951
}
5052
}
5153

54+
normalizeSubscriptionUrl(endpoint, subscriptionEndpoint) {
55+
if (subscriptionEndpoint.startsWith('/')) {
56+
const secure =
57+
endpoint.includes('https') || location.href.includes('https') ? 's' : ''
58+
return `ws${secure}://${location.host}${subscriptionEndpoint}`
59+
}
60+
61+
return subscriptionEndpoint
62+
}
63+
5264
componentWillMount() {
5365
const platformToken = getParameterByName('platform-token')
5466
if (platformToken && platformToken.length > 0) {

packages/graphql-playground/src/components/Playground.tsx

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -284,40 +284,39 @@ export class Playground extends React.PureComponent<Props & DocsState, State> {
284284
headers.forEach(header => (additionalHeaders[header.name] = header.value))
285285
}
286286

287-
return this.fetchSchema(
288-
this.getSimpleEndpoint(),
289-
additionalHeaders,
290-
).then(simpleSchemaData => {
291-
if (!simpleSchemaData || simpleSchemaData.error) {
292-
this.setState({
293-
response: {
294-
date: simpleSchemaData.error,
295-
time: new Date(),
296-
},
297-
} as State)
298-
return
299-
}
287+
return this.fetchSchema(this.getSimpleEndpoint(), additionalHeaders).then(
288+
simpleSchemaData => {
289+
if (!simpleSchemaData || simpleSchemaData.error) {
290+
this.setState({
291+
response: {
292+
date: simpleSchemaData.error,
293+
time: new Date(),
294+
},
295+
} as State)
296+
return
297+
}
300298

301-
if (isEqual(this.rawSchemaCache, simpleSchemaData.data)) {
302-
return
303-
}
299+
if (isEqual(this.rawSchemaCache, simpleSchemaData.data)) {
300+
return
301+
}
304302

305-
this.rawSchemaCache = simpleSchemaData.data
303+
this.rawSchemaCache = simpleSchemaData.data
306304

307-
if (!simpleSchemaData.data) {
308-
return
309-
}
305+
if (!simpleSchemaData.data) {
306+
return
307+
}
310308

311-
const simpleSchema = buildClientSchema(simpleSchemaData.data)
312-
const userFields = this.extractUserField(simpleSchema)
309+
const simpleSchema = buildClientSchema(simpleSchemaData.data)
310+
const userFields = this.extractUserField(simpleSchema)
313311

314-
this.renewStack(simpleSchema)
312+
this.renewStack(simpleSchema)
315313

316-
this.setState({
317-
schemaCache: simpleSchema,
318-
userFields,
319-
} as State)
320-
})
314+
this.setState({
315+
schemaCache: simpleSchema,
316+
userFields,
317+
} as State)
318+
},
319+
)
321320
}
322321

323322
componentWillUnmount() {
@@ -351,10 +350,15 @@ export class Playground extends React.PureComponent<Props & DocsState, State> {
351350

352351
const endpoint = this.getWSEndpoint()
353352
if (endpoint) {
354-
this.wsConnections[session.id] = new SubscriptionClient(endpoint, {
355-
timeout: 20000,
356-
connectionParams,
357-
})
353+
try {
354+
this.wsConnections[session.id] = new SubscriptionClient(endpoint, {
355+
timeout: 20000,
356+
connectionParams,
357+
})
358+
} catch (e) {
359+
/* tslint:disable-next-line */
360+
console.error(e)
361+
}
358362
}
359363
}
360364
initWebsockets() {
@@ -554,7 +558,9 @@ export class Playground extends React.PureComponent<Props & DocsState, State> {
554558
.catch(e => {
555559
this.setState({
556560
response: {
557-
date: `Error: Could not fetch schema from ${endpointUrl}. Make sure the url is correct.`,
561+
date: `Error: Could not fetch schema from ${
562+
endpointUrl
563+
}. Make sure the url is correct.`,
558564
time: new Date(),
559565
resultID: cuid(),
560566
},

0 commit comments

Comments
 (0)