diff --git a/packages/insomnia/src/ui/components/editors/body/graph-ql-editor.tsx b/packages/insomnia/src/ui/components/editors/body/graph-ql-editor.tsx index a5c08fdfe..da96da7da 100644 --- a/packages/insomnia/src/ui/components/editors/body/graph-ql-editor.tsx +++ b/packages/insomnia/src/ui/components/editors/body/graph-ql-editor.tsx @@ -11,6 +11,7 @@ import prettier from 'prettier'; import React, { FC, useEffect, useRef, useState } from 'react'; import ReactDOM from 'react-dom'; import { useSelector } from 'react-redux'; +import { useLocalStorage } from 'react-use'; import { jarFromCookies } from '../../../../common/cookies'; import { markdownToHTML } from '../../../../common/markdown-to-html'; @@ -163,7 +164,6 @@ interface State { operations: string[]; hideSchemaFetchErrors: boolean; variablesSyntaxError: string; - automaticFetch: boolean; explorerVisible: boolean; activeReference: null | ActiveReference; documentAST: null | DocumentNode; @@ -193,18 +193,6 @@ export const GraphQLEditor: FC = ({ documentAST = null; } - let automaticFetch = true; - - try { - const automaticFetchStringified = window.localStorage.getItem('graphql.automaticFetch'); - if (automaticFetchStringified) { - automaticFetch = JSON.parse(automaticFetchStringified); - } - } catch (err) { - if (err instanceof Error) { - console.warn('Could not parse value of graphql.automaticFetch from localStorage:', err.message); - } - } const [state, setState] = useState({ body: { query: requestBody.query || '', @@ -216,10 +204,14 @@ export const GraphQLEditor: FC = ({ variablesSyntaxError: '', activeReference: null, explorerVisible: false, - automaticFetch, documentAST, disabledOperationMarkers: [], }); + + const [automaticFetch, setAutoFetch] = useLocalStorage( + 'graphql.automaticFetch', + true + ); const [schema, setSchema] = useState(null); const [schemaFetchError, setSchemaFetchError] = useState<{ message: string; @@ -230,6 +222,9 @@ export const GraphQLEditor: FC = ({ const editorRef = useRef(null); useEffect(() => { + if (!automaticFetch) { + return; + } let isMounted = true; const init = async () => { setSchemaIsFetching(true); @@ -249,7 +244,7 @@ export const GraphQLEditor: FC = ({ return () => { isMounted = false; }; - }, [environmentId, request._id, request.url, workspaceId]); + }, [automaticFetch, environmentId, request._id, request.url, workspaceId]); const { editorIndentWithTabs, editorIndentSize } = useSelector(selectSettings); const beautifyRequestBody = () => { @@ -475,8 +470,7 @@ export const GraphQLEditor: FC = ({ { - setState(state => ({ ...state, automaticFetch: !state.automaticFetch })); - window.localStorage.setItem('graphql.automaticFetch', state.automaticFetch.toString()); + setAutoFetch(!automaticFetch); }} stayOpenAfterClick >