Skip to content

Commit

Permalink
reload onegraph schema on header change (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
wawhal authored and rikinsk committed Mar 18, 2019
1 parent 040bef2 commit bcb3fb4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions console/src/components/ApiExplorer/GraphiQLWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class GraphiQLWrapper extends Component {
renderGraphiql={renderGraphiql}
endpoint={graphqlNetworkData.url}
headers={graphqlNetworkData.headers}
headerFocus={headerFocus}
query={queryString}
/>
</div>
Expand Down
39 changes: 38 additions & 1 deletion console/src/components/ApiExplorer/OneGraphExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,39 @@ class OneGraphExplorer extends React.Component {
schema: null,
query: this.props.query,
isResizing: false,
headers: []
};

componentDidMount() {
this.introspect();
}

componentDidUpdate() {
if (this.shouldIntrospect(this.props.headers, this.state.headers)) {
this.introspect()
}
}

shouldIntrospect(newHeadersArray, oldHeadersArray) {
if (this.props.headerFocus) {
return false;
}
const oldHeaders = getHeadersAsJSON(oldHeadersArray);
const headers = getHeadersAsJSON(newHeadersArray);
if (Object.keys(oldHeaders).length !== Object.keys(headers).length) {
return true;
}
for (var i = Object.keys(headers).length - 1; i >= 0; i--) {
const key = Object.keys(headers)[i];
const value = headers[key];
if (oldHeaders[key] !== value) {
return true;
}
}
return false;
}

introspect() {
const { endpoint, headers } = this.props;
fetch(endpoint, {
method: 'POST',
Expand All @@ -34,8 +64,15 @@ class OneGraphExplorer extends React.Component {
.then(result => {
this.setState({
schema: buildClientSchema(result.data),
headers: JSON.parse(JSON.stringify(headers))
});
});
})
.catch(error => {
this.setState({
schema: null,
headers: JSON.parse(JSON.stringify(headers))
});
})
}

onExplorerResize = e => {
Expand Down
2 changes: 1 addition & 1 deletion console/src/components/ApiExplorer/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getHeadersAsJSON = headers => {
const getHeadersAsJSON = (headers = []) => {
const headerJSON = {};
const nonEmptyHeaders = headers.filter(header => {
return header.key && header.isActive;
Expand Down

0 comments on commit bcb3fb4

Please sign in to comment.