Skip to content
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

Add support for subscription connectionParams in GraphiQL #548

Merged
merged 11 commits into from
Oct 9, 2017
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT
* Fixes bug where CORS would not allow `Access-Control-Allow-Origin: *` with credential 'include', changed to 'same-origin' [Issue #514](https://github.com/apollographql/apollo-server/issues/514)
* Update apollo-server-lambda README to reflect new package name.
* Add support for connectionParams in GraphiQL plugin options [#452](https://github.com/apollographql/apollo-server/issues/452) [PR 548](https://github.com/apollographql/apollo-server/pull/548)

### v1.1.2
* Fixed bug with no URL query params with GraphiQL on Lambda [Issue #504](https://github.com/apollographql/apollo-server/issues/504) [PR #512](https://github.com/apollographql/apollo-server/pull/503)
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-server-module-graphiql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"typings": "dist/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
}
},
"dependencies": {}
}
7 changes: 6 additions & 1 deletion packages/apollo-server-module-graphiql/src/renderGraphiQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* - (optional) passHeader: a string that will be added to the header object.
* For example "'Authorization': localStorage['Meteor.loginToken']" for meteor
* - (optional) editorTheme: a CodeMirror theme to be applied to the GraphiQL UI
* - (optional) websocketConnectionParams: an object to pass to the web socket server
*/

export type GraphiQLData = {
Expand All @@ -28,6 +29,7 @@ export type GraphiQLData = {
result?: Object,
passHeader?: string,
editorTheme?: string,
websocketConnectionParams?: Object,
};

// Current latest version of GraphiQL.
Expand Down Expand Up @@ -56,6 +58,7 @@ export function renderGraphiQL(data: GraphiQLData): string {
const passHeader = data.passHeader ? data.passHeader : '';
const editorTheme = data.editorTheme;
const usingEditorTheme = !!editorTheme;
const websocketConnectionParams = data.websocketConnectionParams || null;

/* eslint-disable max-len */
return `
Expand Down Expand Up @@ -124,7 +127,8 @@ export function renderGraphiQL(data: GraphiQLData): string {

${usingWs ? `
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient('${endpointURLWs}', {
reconnect: true
reconnect: true${websocketConnectionParams ? `,
connectionParams: ${JSON.stringify(websocketConnectionParams)}` : '' }
});

var graphQLWSFetcher = subscriptionsClient.request.bind(subscriptionsClient);
Expand Down Expand Up @@ -200,6 +204,7 @@ export function renderGraphiQL(data: GraphiQLData): string {
variables: ${safeSerialize(variablesString)},
operationName: ${safeSerialize(operationName)},
editorTheme: ${safeSerialize(editorTheme)},
websocketConnectionParams: ${safeSerialize(websocketConnectionParams)},
}),
document.body
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function createGraphiQLData(params: GraphiQLParams, options: GraphiQLData): Grap
operationName: params.operationName || options.operationName,
passHeader: options.passHeader,
editorTheme: options.editorTheme,
websocketConnectionParams: options.websocketConnectionParams,
};
}

Expand Down