import { useMemo } from 'react'; import { Divider } from '../core/Divider'; import type { EditorProps } from '../core/Editor'; import { Editor } from '../core/Editor'; type Props = Pick< EditorProps, 'heightMode' | 'onChange' | 'defaultValue' | 'className' | 'useTemplating' >; interface GraphQLBody { query: string; variables?: Record; operationName?: string; } export function GraphQLEditor({ defaultValue, onChange, ...extraEditorProps }: Props) { const { query, variables } = useMemo(() => { try { const p = JSON.parse(defaultValue ?? '{}'); const query = p.query ?? ''; const variables = p.variables; const operationName = p.operationName; return { query, variables, operationName }; } catch (err) { return { query: 'failed to parse' }; } }, [defaultValue]); const handleChange = (b: GraphQLBody) => { onChange?.(JSON.stringify(b, null, 2)); }; const handleChangeQuery = (query: string) => { handleChange({ query, variables }); }; const handleChangeVariables = (variables: string) => { handleChange({ query, variables: JSON.parse(variables) }); }; return (

Variables

); }