MCPcopy
hub / github.com/graphql/graphiql / usePrettifyEditors

Function usePrettifyEditors

packages/graphiql-react/src/editor/hooks.ts:242–296  ·  view source on GitHub ↗
({
  caller,
  onPrettifyQuery = DEFAULT_PRETTIFY_QUERY,
}: UsePrettifyEditorsArgs = {})

Source from the content-addressed store, hash-verified

240}
241
242export function usePrettifyEditors({
243 caller,
244 onPrettifyQuery = DEFAULT_PRETTIFY_QUERY,
245}: UsePrettifyEditorsArgs = {}) {
246 const { queryEditor, headerEditor, variableEditor } = useEditorContext({
247 nonNull: true,
248 caller: caller || _usePrettifyEditors,
249 });
250 return async () => {
251 if (variableEditor) {
252 const variableEditorContent = variableEditor.getValue();
253 try {
254 const prettifiedVariableEditorContent = JSON.stringify(
255 JSON.parse(variableEditorContent),
256 null,
257 2,
258 );
259 if (prettifiedVariableEditorContent !== variableEditorContent) {
260 variableEditor.setValue(prettifiedVariableEditorContent);
261 }
262 } catch {
263 /* Parsing JSON failed, skip prettification */
264 }
265 }
266
267 if (headerEditor) {
268 const headerEditorContent = headerEditor.getValue();
269
270 try {
271 const prettifiedHeaderEditorContent = JSON.stringify(
272 JSON.parse(headerEditorContent),
273 null,
274 2,
275 );
276 if (prettifiedHeaderEditorContent !== headerEditorContent) {
277 headerEditor.setValue(prettifiedHeaderEditorContent);
278 }
279 } catch {
280 /* Parsing JSON failed, skip prettification */
281 }
282 }
283
284 if (queryEditor) {
285 const editorContent = queryEditor.getValue();
286 try {
287 const prettifiedEditorContent = await onPrettifyQuery(editorContent);
288 if (prettifiedEditorContent !== editorContent) {
289 queryEditor.setValue(prettifiedEditorContent);
290 }
291 } catch {
292 /* Parsing query failed, skip prettification */
293 }
294 }
295 };
296}
297
298export type UseAutoCompleteLeafsArgs = {
299 /**

Callers 4

useHeaderEditorFunction · 0.90
useVariableEditorFunction · 0.90
useQueryEditorFunction · 0.90
GraphiQLToolbarFunction · 0.90

Calls 3

useEditorContextFunction · 0.85
onPrettifyQueryFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected