diff --git a/README.md b/README.md
index 88e3a16..eff5730 100644
--- a/README.md
+++ b/README.md
@@ -3,3 +3,5 @@
Making the great [GraphiQL tool](https://github.com/graphql/graphiql/) available anywhere as a Chrome extension.
Based on the [GraphiQL example code](https://github.com/graphql/graphiql/tree/master/example).
+
+With option to add headers. Header format is JSON or "HEADER_NAME:HEADER_VALUE;"
diff --git a/css/chromeiql.css b/css/chromeiql.css
index f5cf1a7..b19dffb 100644
--- a/css/chromeiql.css
+++ b/css/chromeiql.css
@@ -18,7 +18,7 @@ body {
display: flex;
}
-#url-bar {
+#input-bar {
width: 600px;
position: absolute;
text-align: center;
@@ -47,3 +47,24 @@ body {
#url-save-button {
padding: 6px 12px;
}
+
+#headers-box, #headers-save-button {
+ height: 30px;
+ box-sizing: border-box;
+ margin: 3px auto;
+}
+
+#headers-box {
+ width: 500px;
+ margin-right: 2px;
+ border-radius: 4px;
+ margin-right: 2px;
+ padding: 0 4px;
+ border: 1px none;
+ border-style: inset;
+ color: #555;
+}
+
+#url-save-button {
+ padding: 6px 12px;
+}
diff --git a/js/chromeiql.jsx b/js/chromeiql.jsx
index a4de511..c9d2789 100644
--- a/js/chromeiql.jsx
+++ b/js/chromeiql.jsx
@@ -7,6 +7,9 @@ import $ from 'jquery';
// Buffer for endpoint entry value
let chromeiqlEndpoint;
+// Buffer for headers value
+let chromeiqlHeaders;
+
// Parse the search string to get url parameters.
const search = window.location.search;
let parameters = {};
@@ -49,12 +52,38 @@ function updateURL() {
history.replaceState(null, null, newSearch);
}
+function createHeaders(headers) {
+ const retHeaders = new Headers({'Content-Type': 'application/json'});
+ if (headers) {
+ let hconv = string2Header(headers);
+ for (let header of hconv) {
+ if (header.length !== 2) continue;
+ retHeaders.append(header[0], header[1]);
+ }
+ }
+ return retHeaders;
+}
+
+const string2Header = (str) => {
+ if (str && typeof str === 'string') {
+ try {
+ return new Headers(JSON.parse(str));
+ } catch (_) {}
+ let headers = str.split(';').filter(h => h !== '').reduce((ph, ch, ci) => {
+ const values = ch.split(':');
+ ph.append(values[0], values[1]);
+ return ph;
+ }, new Headers());
+ return headers;
+ }
+}
+
// Defines a GraphQL fetcher using the fetch API.
-function graphQLFetcher(endpoint) {
+function graphQLFetcher(endpoint, headers = null) {
return function(graphQLParams) {
return fetch(endpoint, {
method: 'post',
- headers: { 'Content-Type': 'application/json' },
+ headers: headers ? createHeaders(headers) : { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(response => response.json());
@@ -67,39 +96,53 @@ class ChromeiQL extends React.Component {
this.state = {
prevEndpoint: null,
currEndpoint: this.props.endpoint,
+ prevHeaders: null,
+ currHeaders: this.props.headers,
};
this.setEndpoint = this.setEndpoint.bind(this)
this.updateEndpoint = this.updateEndpoint.bind(this)
+ this.setHeaders = this.setHeaders.bind(this)
+ this.updateHeaders = this.updateHeaders.bind(this)
}
render() {
const endpoint = this.state.currEndpoint
+ const headers = this.state.currHeaders
let graphqlConsole = null;
if (endpoint) {
+
graphqlConsole =