Skip to content

Commit

Permalink
move prettify query functionality to editor context
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock committed May 25, 2022
1 parent d677b60 commit 0089a9f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-queens-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphiql': patch
'@graphiql/react': patch
---

Move preffity query functionality to editor context in `@graphiql/react`
50 changes: 49 additions & 1 deletion packages/graphiql-react/src/editor/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fillLeafs, GetDefaultFieldNamesFn, mergeAst } from '@graphiql/toolkit';
import copyToClipboard from 'copy-to-clipboard';
import { DocumentNode, OperationDefinitionNode, print } from 'graphql';
import { DocumentNode, OperationDefinitionNode, parse, print } from 'graphql';
import { VariableToType } from 'graphql-language-service';
import {
createContext,
Expand All @@ -24,6 +24,7 @@ export type EditorContextType = {
autoCompleteLeafs(): string | undefined;
copy(): void;
merge(): void;
prettify(): void;
headerEditor: CodeMirrorEditor | null;
queryEditor: CodeMirrorEditorWithOperationFacts | null;
responseEditor: CodeMirrorEditor | null;
Expand All @@ -40,6 +41,7 @@ export const EditorContext = createContext<EditorContextType>({
},
copy() {},
merge() {},
prettify() {},
headerEditor: null,
queryEditor: null,
responseEditor: null,
Expand Down Expand Up @@ -138,11 +140,56 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
queryEditor.setValue(print(mergeAst(documentAST, schema)));
}, [queryEditor, schema]);

const prettify = useCallback(() => {
if (queryEditor) {
const editorContent = queryEditor.getValue();
const prettifiedEditorContent = print(parse(editorContent));

if (prettifiedEditorContent !== editorContent) {
queryEditor.setValue(prettifiedEditorContent);
}
}

if (variableEditor) {
const variableEditorContent = variableEditor.getValue();
try {
const prettifiedVariableEditorContent = JSON.stringify(
JSON.parse(variableEditorContent),
null,
2,
);
if (prettifiedVariableEditorContent !== variableEditorContent) {
variableEditor.setValue(prettifiedVariableEditorContent);
}
} catch {
/* Parsing JSON failed, skip prettification */
}
}

if (headerEditor) {
const headerEditorContent = headerEditor.getValue();

try {
const prettifiedHeaderEditorContent = JSON.stringify(
JSON.parse(headerEditorContent),
null,
2,
);
if (prettifiedHeaderEditorContent !== headerEditorContent) {
headerEditor.setValue(prettifiedHeaderEditorContent);
}
} catch {
/* Parsing JSON failed, skip prettification */
}
}
}, [headerEditor, queryEditor, variableEditor]);

const value = useMemo<EditorContextType>(
() => ({
autoCompleteLeafs,
copy,
merge,
prettify,
headerEditor,
queryEditor,
responseEditor,
Expand All @@ -156,6 +203,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
autoCompleteLeafs,
copy,
merge,
prettify,
headerEditor,
queryEditor,
responseEditor,
Expand Down
4 changes: 1 addition & 3 deletions packages/graphiql-react/src/editor/header-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
export type UseHeaderEditorArgs = {
editorTheme?: string;
onEdit?: EditCallback;
onPrettifyQuery?: EmptyCallback;
onRunQuery?: EmptyCallback;
readOnly?: boolean;
shouldPersistHeaders?: boolean;
Expand All @@ -26,7 +25,6 @@ export type UseHeaderEditorArgs = {
export function useHeaderEditor({
editorTheme = 'graphiql',
onEdit,
onPrettifyQuery,
onRunQuery,
readOnly = false,
shouldPersistHeaders = false,
Expand Down Expand Up @@ -125,7 +123,7 @@ export function useHeaderEditor({
useCompletion(headerEditor);

useKeyMap(headerEditor, ['Cmd-Enter', 'Ctrl-Enter'], onRunQuery);
useKeyMap(headerEditor, ['Shift-Ctrl-P'], onPrettifyQuery);
useKeyMap(headerEditor, ['Shift-Ctrl-P'], context.prettify);
useKeyMap(headerEditor, ['Shift-Ctrl-M'], context.merge);

useResizeEditor(headerEditor, ref);
Expand Down
4 changes: 1 addition & 3 deletions packages/graphiql-react/src/editor/query-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export type UseQueryEditorArgs = {
externalFragments?: string | FragmentDefinitionNode[];
onEdit?: EditCallback;
onEditOperationName?: EditCallback;
onPrettifyQuery?: EmptyCallback;
onRunQuery?: EmptyCallback;
readOnly?: boolean;
validationRules?: ValidationRule[];
Expand All @@ -47,7 +46,6 @@ export function useQueryEditor({
externalFragments,
onEdit,
onEditOperationName,
onPrettifyQuery,
onRunQuery,
readOnly = false,
validationRules,
Expand Down Expand Up @@ -325,7 +323,7 @@ export function useQueryEditor({
// Shift-Ctrl-P is hard coded in Firefox for private browsing so adding an alternative to Pretiffy
'Shift-Ctrl-F',
],
onPrettifyQuery,
editorContext.prettify,
);
useKeyMap(queryEditor, ['Shift-Ctrl-M'], editorContext.merge);

Expand Down
4 changes: 1 addition & 3 deletions packages/graphiql-react/src/editor/variable-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { CodeMirrorType } from './types';
export type UseVariableEditorArgs = {
editorTheme?: string;
onEdit?: EditCallback;
onPrettifyQuery?: EmptyCallback;
onRunQuery?: EmptyCallback;
readOnly?: boolean;
value?: string;
Expand All @@ -26,7 +25,6 @@ export type UseVariableEditorArgs = {
export function useVariableEditor({
editorTheme = 'graphiql',
onEdit,
onPrettifyQuery,
onRunQuery,
readOnly = false,
value,
Expand Down Expand Up @@ -135,7 +133,7 @@ export function useVariableEditor({
useCompletion(variableEditor);

useKeyMap(variableEditor, ['Cmd-Enter', 'Ctrl-Enter'], onRunQuery);
useKeyMap(variableEditor, ['Shift-Ctrl-P'], onPrettifyQuery);
useKeyMap(variableEditor, ['Shift-Ctrl-P'], context.prettify);
useKeyMap(variableEditor, ['Shift-Ctrl-M'], context.merge);

useResizeEditor(variableEditor, ref);
Expand Down
49 changes: 3 additions & 46 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,9 @@ class GraphiQLWithContext extends React.Component<
) || (
<GraphiQL.Toolbar>
<ToolbarButton
onClick={this.handlePrettifyQuery}
onClick={() => {
this.props.editorContext.prettify();
}}
title="Prettify Query (Shift-Ctrl-P)"
label="Prettify"
/>
Expand Down Expand Up @@ -848,7 +850,6 @@ class GraphiQLWithContext extends React.Component<
externalFragments={this.props.externalFragments}
onEdit={this.handleEditQuery}
onEditOperationName={this.props.onEditOperationName}
onPrettifyQuery={this.handlePrettifyQuery}
onRunQuery={this.handleEditorRunQuery}
readOnly={this.props.readOnly}
validationRules={this.props.validationRules}
Expand Down Expand Up @@ -894,7 +895,6 @@ class GraphiQLWithContext extends React.Component<
<VariableEditor
value={this.props.variables}
onEdit={this.handleEditVariables}
onPrettifyQuery={this.handlePrettifyQuery}
onRunQuery={this.handleEditorRunQuery}
editorTheme={this.props.editorTheme}
readOnly={this.props.readOnly}
Expand All @@ -905,7 +905,6 @@ class GraphiQLWithContext extends React.Component<
active={this.state.headerEditorActive}
editorTheme={this.props.editorTheme}
onEdit={this.handleEditHeaders}
onPrettifyQuery={this.handlePrettifyQuery}
onRunQuery={this.handleEditorRunQuery}
readOnly={this.props.readOnly}
shouldPersistHeaders={this.props.shouldPersistHeaders}
Expand Down Expand Up @@ -1308,48 +1307,6 @@ class GraphiQLWithContext extends React.Component<
this.handleRunQuery(operationName);
}

handlePrettifyQuery = () => {
const editor = this.getQueryEditor();
const editorContent = editor?.getValue() ?? '';
const prettifiedEditorContent = print(parse(editorContent));

if (prettifiedEditorContent !== editorContent) {
editor?.setValue(prettifiedEditorContent);
}

const variableEditor = this.getVariableEditor();
const variableEditorContent = variableEditor?.getValue() ?? '';

try {
const prettifiedVariableEditorContent = JSON.stringify(
JSON.parse(variableEditorContent),
null,
2,
);
if (prettifiedVariableEditorContent !== variableEditorContent) {
variableEditor?.setValue(prettifiedVariableEditorContent);
}
} catch {
/* Parsing JSON failed, skip prettification */
}

const headerEditor = this.getHeaderEditor();
const headerEditorContent = headerEditor?.getValue() ?? '';

try {
const prettifiedHeaderEditorContent = JSON.stringify(
JSON.parse(headerEditorContent),
null,
2,
);
if (prettifiedHeaderEditorContent !== headerEditorContent) {
headerEditor?.setValue(prettifiedHeaderEditorContent);
}
} catch {
/* Parsing JSON failed, skip prettification */
}
};

handleEditQuery = (value: string) => {
this.setState(
state => ({
Expand Down

0 comments on commit 0089a9f

Please sign in to comment.