Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#2265): Support GraphQL variables formatting #2267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/bruno-app/src/components/CodeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import StyledWrapper from './StyledWrapper';
import jsonlint from 'jsonlint';
import { JSHINT } from 'jshint';
import stripJsonComments from 'strip-json-comments';
import { IconWand } from '@tabler/icons';
import toast from 'react-hot-toast';
import { format } from 'prettier/standalone';

let CodeMirror;
const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
Expand Down Expand Up @@ -280,19 +283,41 @@ export default class CodeEditor extends React.Component {
}
}

prettify = () => {
try {
const prettified = format(this.props.value, this.props.formattingOptions);

this.editor.setValue(prettified);

toast.success('Code prettified');
} catch (e) {
toast.error('Error occurred while prettifying code : ' + e.message);
}
};

render() {
if (this.editor) {
this.editor.refresh();
}
return (
<StyledWrapper
className="h-full w-full"
className="h-full w-full relative"
aria-label="Code Editor"
font={this.props.font}
ref={(node) => {
this._node = node;
}}
/>
>
{this.props.formattingOptions && (
<button
className="btn-add-param text-link px-4 py-4 select-none absolute top-0 right-0 z-10"
onClick={this.prettify}
title={'Prettify'}
>
<IconWand size={20} strokeWidth={1.5} />
</button>
)}
</StyledWrapper>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { updateRequestGraphqlVariables } from 'providers/ReduxStore/slices/colle
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import { useTheme } from 'providers/Theme';
import StyledWrapper from './StyledWrapper';
import prettierParserBabel from 'prettier/parser-babel';

const GraphQLVariables = ({ variables, item, collection }) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -37,6 +38,7 @@ const GraphQLVariables = ({ variables, item, collection }) => {
mode="javascript"
onRun={onRun}
onSave={onSave}
formattingOptions={{ parser: 'json', plugins: [prettierParserBabel] }}
/>
</StyledWrapper>
);
Expand Down