Skip to content

Commit

Permalink
fix: persist graphql variables directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexxa committed Nov 16, 2023
1 parent 1238ae4 commit 922e846
Showing 1 changed file with 10 additions and 12 deletions.
Expand Up @@ -30,6 +30,10 @@ import { Toolbar } from '../../key-value-editor/key-value-editor';
import { useDocBodyKeyboardShortcuts } from '../../keydown-binder';
import { TimeFromNow } from '../../time-from-now';

export interface WrappedVariables {
content: string;
}

function getGraphQLContent(body: GraphQLBody, query?: string, operationName?: string, variables?: string): string {
// the body object is one dimensional, so we don't need to worry about shallow copying.
const { query: originalQuery, ...optionalProps } = body;
Expand Down Expand Up @@ -188,13 +192,9 @@ export const GraphQLEditor: FC<Props> = ({
} catch (err) {
requestBody = { query: '' };
}
if (typeof requestBody.variables === 'string') {
try {
requestBody.variables = JSON.parse(requestBody.variables);
} catch (err) {
requestBody.variables = '';
}
}

requestBody.variables = requestBody.variables || '';

let documentAST;
try {
documentAST = parse(requestBody.query || '');
Expand Down Expand Up @@ -281,13 +281,11 @@ export const GraphQLEditor: FC<Props> = ({
};
const changeVariables = (variablesInput: string) => {
try {
const variables = JSON.parse(variablesInput || '{}');

const content = getGraphQLContent(state.body, undefined, operationName, variables);
const content = getGraphQLContent(state.body, undefined, operationName, variablesInput);
onChange(content);
setState(state => ({
...state,
body: { ...state.body, variables },
body: state.body,
variablesSyntaxError: '',
}));
} catch (err) {
Expand Down Expand Up @@ -632,7 +630,7 @@ export const GraphQLEditor: FC<Props> = ({
enableNunjucks
uniquenessKey={uniquenessKey ? uniquenessKey + '::variables' : undefined}
showPrettifyButton={false}
defaultValue={jsonPrettify(JSON.stringify(requestBody.variables))}
defaultValue={jsonPrettify(requestBody.variables)}
className={className}
getAutocompleteConstants={() => Object.keys(variableTypes)}
lintOptions={{
Expand Down

0 comments on commit 922e846

Please sign in to comment.