Skip to content

Commit cc7a7e2

Browse files
fix: Set headers to localstorage (#1578)
1 parent 881a19f commit cc7a7e2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/graphiql/src/components/GraphiQL.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type FetcherParams = {
7777

7878
export type FetcherOpts = {
7979
headers?: { [key: string]: any };
80+
shouldPersistHeaders: boolean;
8081
};
8182

8283
export type FetcherResult =
@@ -109,6 +110,7 @@ export type GraphiQLProps = {
109110
defaultVariableEditorOpen?: boolean;
110111
defaultSecondaryEditorOpen?: boolean;
111112
headerEditorEnabled?: boolean;
113+
shouldPersistHeaders?: boolean;
112114
onCopyQuery?: (query?: string) => void;
113115
onEditQuery?: (query?: string) => void;
114116
onEditVariables?: (value: string) => void;
@@ -137,6 +139,7 @@ export type GraphiQLState = {
137139
variableEditorActive: boolean;
138140
headerEditorActive: boolean;
139141
headerEditorEnabled: boolean;
142+
shouldPersistHeaders: boolean;
140143
historyPaneOpen: boolean;
141144
docExplorerWidth: number;
142145
isWaitingForResponse: boolean;
@@ -253,6 +256,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
253256
}
254257

255258
const headerEditorEnabled = props.headerEditorEnabled ?? false;
259+
const shouldPersistHeaders = props.shouldPersistHeaders ?? false;
256260

257261
// Initialize state
258262
this.state = {
@@ -274,6 +278,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
274278
: secondaryEditorOpen && true,
275279
headerEditorActive: this._storage.get('headerEditorActive') === 'true',
276280
headerEditorEnabled,
281+
shouldPersistHeaders,
277282
historyPaneOpen: this._storage.get('historyPaneOpen') === 'true' || false,
278283
docExplorerWidth:
279284
Number(this._storage.get('docExplorerWidth')) ||
@@ -848,6 +853,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
848853
variables: string,
849854
headers: string,
850855
operationName: string,
856+
shouldPersistHeaders: boolean,
851857
cb: (value: FetcherResult) => any,
852858
) {
853859
const fetcher = this.props.fetcher;
@@ -882,7 +888,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
882888
variables: jsonVariables,
883889
operationName,
884890
},
885-
{ headers: jsonHeaders },
891+
{ headers: jsonHeaders, shouldPersistHeaders },
886892
);
887893

888894
if (isPromise(fetch)) {
@@ -943,6 +949,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
943949
const editedQuery = this.autoCompleteLeafs() || this.state.query;
944950
const variables = this.state.variables;
945951
const headers = this.state.headers;
952+
const shouldPersistHeaders = this.state.shouldPersistHeaders;
946953
let operationName = this.state.operationName;
947954

948955
// If an operation was explicitly provided, different from the current
@@ -975,6 +982,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
975982
variables as string,
976983
headers as string,
977984
operationName as string,
985+
shouldPersistHeaders as boolean,
978986
(result: FetcherResult) => {
979987
if (queryID === this._editorQueryID) {
980988
this.setState({
@@ -1165,6 +1173,8 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
11651173

11661174
handleEditHeaders = (value: string) => {
11671175
this.setState({ headers: value });
1176+
this.props.shouldPersistHeaders &&
1177+
debounce(500, () => this._storage.set('headers', value))();
11681178
if (this.props.onEditHeaders) {
11691179
this.props.onEditHeaders(value);
11701180
}

0 commit comments

Comments
 (0)