Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import './index.css';

interface ToolbarButtonProps extends ComponentPropsWithoutRef<'button'> {
label: string;
onClick?: (args?: any) => any | Promise<any>;
}

export const ToolbarButton = forwardRef<HTMLButtonElement, ToolbarButtonProps>(
({ label, onClick, ...props }, ref) => {
const [error, setError] = useState<Error | null>(null);
const handleClick: MouseEventHandler<HTMLButtonElement> = event => {
const handleClick: MouseEventHandler<HTMLButtonElement> = async event => {
try {
// Optional chaining inside try-catch isn't supported yet by react-compiler
if (onClick) {
onClick(event);
await onClick(event);
}
setError(null);
} catch (err) {
Expand Down
34 changes: 24 additions & 10 deletions packages/graphiql-react/src/stores/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ export const createEditorSlice: CreateEditorSlice = initial => (set, get) => {
const { queryEditor, headerEditor, variableEditor, onPrettifyQuery } =
get();

const errors: any[] = [];

if (variableEditor) {
try {
const content = variableEditor.getValue();
Expand All @@ -491,6 +493,7 @@ export const createEditorSlice: CreateEditorSlice = initial => (set, get) => {
'Parsing variables JSON failed, skip prettification.',
error,
);
errors.push(error);
}
}

Expand All @@ -507,21 +510,32 @@ export const createEditorSlice: CreateEditorSlice = initial => (set, get) => {
'Parsing headers JSON failed, skip prettification.',
error,
);
errors.push(error);
}
}

if (!queryEditor) {
return;
if (queryEditor) {
try {
const content = queryEditor.getValue();
const formatted = await onPrettifyQuery(content);
if (formatted !== content) {
queryEditor.setValue(formatted);
}
} catch (error) {
// eslint-disable-next-line no-console
console.warn('Parsing query failed, skip prettification.', error);
errors.push(error);
}
}
try {
const content = queryEditor.getValue();
const formatted = await onPrettifyQuery(content);
if (formatted !== content) {
queryEditor.setValue(formatted);

if (errors.length) {
if (errors.length > 1) {
const msg = errors
.map(err => err?.message || 'Error prettifying')
.join(', ');
throw new Error(msg);
}
} catch (error) {
// eslint-disable-next-line no-console
console.warn('Parsing query failed, skip prettification.', error);
throw errors[0];
}
},
mergeQuery() {
Expand Down
Loading