GraphQL query editor transformer works!

This commit is contained in:
Gregory Schier
2023-03-14 19:08:18 -07:00
parent efa5455a7b
commit f4401e77bb
9 changed files with 51 additions and 21 deletions

View File

@@ -0,0 +1,32 @@
import { useMemo } from 'react';
import type { EditorProps } from '../core/Editor';
import { Editor } from '../core/Editor';
type Props = Pick<
EditorProps,
'heightMode' | 'onChange' | 'defaultValue' | 'className' | 'useTemplating'
>;
export function GraphQLEditor({ defaultValue, onChange, ...props }: Props) {
const { query } = useMemo(() => {
try {
const { query } = JSON.parse(defaultValue ?? '{}');
return { query };
} catch (err) {
return { query: 'failed to parse' };
}
}, [defaultValue]);
const handleChange = (query: string) => {
onChange?.(JSON.stringify({ query }, null, 2));
};
return (
<Editor
defaultValue={query ?? ''}
onChange={handleChange}
contentType="application/graphql"
{...props}
/>
);
}