From 877f9ce15a2252c353bf467bcda3b11c66c75b1a Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 9 Aug 2024 14:05:38 -0700 Subject: [PATCH] GraphQL variables now reset if entire thing deleted --- src-web/components/GraphQLEditor.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src-web/components/GraphQLEditor.tsx b/src-web/components/GraphQLEditor.tsx index 3951f46e..977a8787 100644 --- a/src-web/components/GraphQLEditor.tsx +++ b/src-web/components/GraphQLEditor.tsx @@ -32,7 +32,7 @@ export function GraphQLEditor({ defaultValue, onChange, baseRequest, ...extraEdi return { query: '', variables: {} }; } try { - const p = JSON.parse(defaultValue ?? '{}'); + const p = JSON.parse(defaultValue || '{}'); const query = p.query ?? ''; const variables = p.variables; const operationName = p.operationName; @@ -59,7 +59,14 @@ export function GraphQLEditor({ defaultValue, onChange, baseRequest, ...extraEdi ); const handleChangeVariables = useCallback( - (variables: string) => handleChange({ query, variables: JSON.parse(variables) }), + (variables: string) => { + try { + handleChange({ query, variables: JSON.parse(variables || '{}') }); + } catch (err) { + // Don't do anything if invalid JSON. The user probably hasn't finished + // typing yet. + } + }, [handleChange, query], );