Skip to content

Commit

Permalink
Pass custom header in GraphiQL (#133)
Browse files Browse the repository at this point in the history
* Pass meteor login token in Authorization header

* Use passHeader method

* update changelog

* Fix lint rules
  • Loading branch information
nicolaslopezj authored and helfer committed Sep 12, 2016
1 parent f4546e7 commit be0f5ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### VNEXT

* Allow to pass custom headers in GraphiQL ([@nicolaslopezj](https://github.com/nicolaslopezj) in [#133](https://github.com/apollostack/apollo-server/pull/133)).

### v0.3.0
* Refactor Hapi integration to improve the API and make the plugins more idiomatic. ([@nnance](https://github.com/nnance)) in
[PR #127](https://github.com/apollostack/apollo-server/pull/127)
Expand Down
1 change: 1 addition & 0 deletions src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export function graphiqlExpress(options: GraphiQL.GraphiQLData) {
query: query || options.query,
variables: JSON.parse(variables) || options.variables,
operationName: operationName || options.operationName,
passHeader: options.passHeader,
});
res.setHeader('Content-Type', 'text/html');
res.write(graphiQLString);
Expand Down
9 changes: 7 additions & 2 deletions src/modules/renderGraphiQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* - (optional) variables: a JS object of variables to pre-fill in the GraphiQL UI
* - (optional) operationName: the operationName to pre-fill in the GraphiQL UI
* - (optional) result: the result of the query to pre-fill in the GraphiQL UI
* - (optional) passHeader: a string that will be added to the header object.
* For example "'Authorization': localStorage['Meteor.loginToken']" for meteor
*/

export type GraphiQLData = {
Expand All @@ -23,6 +25,7 @@ export type GraphiQLData = {
variables?: Object,
operationName?: string,
result?: Object,
passHeader?: string
};

// Current latest version of GraphiQL.
Expand All @@ -41,6 +44,7 @@ export function renderGraphiQL(data: GraphiQLData): string {
data.variables ? JSON.stringify(data.variables, null, 2) : null;
const resultString = null;
const operationName = data.operationName;
const passHeader = data.passHeader ? data.passHeader : '';

/* eslint-disable max-len */
return `
Expand Down Expand Up @@ -94,15 +98,16 @@ export function renderGraphiQL(data: GraphiQLData): string {
otherParams[k] = parameters[k];
}
}
// We don't use safe-serialize for location, becuase it's not client input.
// We don't use safe-serialize for location, because it's not client input.
var fetchURL = locationQuery(otherParams, '${endpointURL}');
// Defines a GraphQL fetcher using the fetch API.
function graphQLFetcher(graphQLParams) {
return fetch(fetchURL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
${passHeader}
},
body: JSON.stringify(graphQLParams),
credentials: 'include',
Expand Down

0 comments on commit be0f5ec

Please sign in to comment.