Skip to content

Commit

Permalink
fix: prettify graphql query variables
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed May 19, 2022
1 parent fe20729 commit 79e0032
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Expand Up @@ -4,4 +4,3 @@
{# This is a comment #}
"tag": {% foo 'bar', "baz" %}
}

5 changes: 1 addition & 4 deletions packages/insomnia-prettify/src/json.ts
Expand Up @@ -29,7 +29,7 @@ const NUNJUCKS_CLOSE_STATES: {
/**
* Format a JSON string without parsing it as JavaScript.
*
* Code taken from jsonlint (http://zaa.ch/jsonlint/)
* Code taken from JSON Lint (https://github.com/zaach/jsonlint)
*/
export const prettify = (json?: string, indentChars = '\t', replaceUnicode = true) => {
if (!json) {
Expand Down Expand Up @@ -132,9 +132,6 @@ export const prettify = (json?: string, indentChars = '\t', replaceUnicode = tru
}
continue;
case '}':
indentLevel--;
newJson += '\n' + repeatString(tab, indentLevel) + currentChar;
continue;
case ']':
indentLevel--;
newJson += '\n' + repeatString(tab, indentLevel) + currentChar;
Expand Down
Expand Up @@ -102,6 +102,7 @@ export class GraphQLEditor extends PureComponent<Props, State> {
_documentAST: null | DocumentNode = null;
_isMounted = false;
_queryEditor: null | EditorFromTextArea = null;
_queryVariableEditor: null | EditorFromTextArea = null;
_schemaFetchTimeout: NodeJS.Timeout | null = null;

constructor(props: Props) {
Expand Down Expand Up @@ -281,6 +282,10 @@ export class GraphQLEditor extends PureComponent<Props, State> {
this._handleBodyChange(query, variables, operationName);
}

_handleQueryVariableEditorInit(codeMirror: EditorFromTextArea) {
this._queryVariableEditor = codeMirror;
}

async _fetchAndSetSchema(rawRequest: Request) {
this.setState({
schemaIsFetching: true,
Expand Down Expand Up @@ -468,19 +473,32 @@ export class GraphQLEditor extends PureComponent<Props, State> {
_handlePrettify() {
const { body } = this.state;
const { variables, query } = body;
const { editorIndentWithTabs, editorIndentSize } = this.props.settings;
const prettyQuery = prettier.format(query, {
parser: 'graphql',
useTabs: this.props.settings.editorIndentWithTabs,
tabWidth: this.props.settings.editorIndentSize,
useTabs: editorIndentWithTabs,
tabWidth: editorIndentSize,
});
const prettyVariables = variables && JSON.parse(jsonPrettify(JSON.stringify(variables)));

let prettyVariablesString: string | undefined;
let prettyVariables: Record<string, any> | undefined;

if (variables) {
const indentChars = (editorIndentWithTabs) ? '\t' : ' '.repeat(editorIndentSize);
prettyVariablesString = jsonPrettify(JSON.stringify(variables), indentChars);
prettyVariables = prettyVariablesString && JSON.parse(prettyVariablesString);
}

this._handleBodyChange(prettyQuery, prettyVariables, this.state.body.operationName);

// Update editor contents
if (this._queryEditor) {
this._queryEditor.setValue(prettyQuery);
}

if (this._queryVariableEditor && prettyVariablesString) {
this._queryVariableEditor.setValue(prettyVariablesString);
}
}

_getOperations(): any[] {
Expand Down Expand Up @@ -820,6 +838,7 @@ export class GraphQLEditor extends PureComponent<Props, State> {
}}
noLint={!variableTypes}
onChange={this._handleVariablesChange}
onCodeMirrorInit={this._handleQueryVariableEditorInit}
mode="graphql-variables"
placeholder=""
/>
Expand Down

0 comments on commit 79e0032

Please sign in to comment.