Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prettier! #2508

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions .changeset/fair-snails-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphiql': patch
'@graphiql/react': patch
---

Introduce prettier formatting support to graphiql
3 changes: 2 additions & 1 deletion packages/graphiql-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"escape-html": "^1.0.3",
"graphql-language-service": "^5.0.6",
"markdown-it": "^12.2.0",
"set-value": "^4.1.0"
"set-value": "^4.1.0",
"prettier": "^2.7.0"
},
"devDependencies": {
"@types/codemirror": "^5.60.5",
Expand Down
24 changes: 17 additions & 7 deletions packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fillLeafs, GetDefaultFieldNamesFn, mergeAst } from '@graphiql/toolkit';
import { EditorChange } from 'codemirror';
import copyToClipboard from 'copy-to-clipboard';
import { parse, print } from 'graphql';
import { print } from 'graphql';
import { useCallback, useEffect } from 'react';

import { useExplorerContext } from '../explorer';
Expand Down Expand Up @@ -208,12 +208,22 @@ export function usePrettifyEditors({
}

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

if (prettifiedEditorContent !== editorContent) {
queryEditor.setValue(prettifiedEditorContent);
}
import('prettier/standalone').then(({ default: prettier }) => {
import('prettier/parser-graphql').then(({ default: graphqlPlugin }) => {
Comment on lines +211 to +212
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can use Promise.all in this case, and async/await.

Suggested change
import('prettier/standalone').then(({ default: prettier }) => {
import('prettier/parser-graphql').then(({ default: graphqlPlugin }) => {
const [{ default: prettier }, { default: graphqlPlugin }] = await Promise.all([
import('prettier/standalone'),
import('prettier/parser-graphql'),
]);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would need to make the parent function async as well, I can try this

try {
const editorContent = queryEditor.getValue();
const prettifiedEditorContent = prettier.format(editorContent, {
parser: 'graphql',
plugins: [graphqlPlugin],
});
if (prettifiedEditorContent !== editorContent) {
queryEditor.setValue(prettifiedEditorContent);
}
} catch {
// as always on format, no-op on parser failures
}
});
});
}
}, [queryEditor, variableEditor, headerEditor]);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/graphiql/cypress/integration/prettify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const prettifiedQuery = `{
longDescriptionType {
id
}
}`;
}
`;

const prettifiedVariables = `{
"a": 1
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15752,6 +15752,11 @@ prettier@^2.0.4:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.4.tgz#2d1bae173e355996ee355ec9830a7a1ee05457ef"
integrity sha512-SVJIQ51spzFDvh4fIbCLvciiDMCrRhlN3mbZvv/+ycjvmF5E73bKdGfU8QDLNmjYJf+lsGnDBC4UUnvTe5OO0w==

prettier@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.0.tgz#a4fdae07e5596c51c9857ea676cd41a0163879d6"
integrity sha512-nwoX4GMFgxoPC6diHvSwmK/4yU8FFH3V8XWtLQrbj4IBsK2pkYhG4kf/ljF/haaZ/aii+wNJqISrCDPgxGWDVQ==

pretty-bytes@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.5.0.tgz#0cecda50a74a941589498011cf23275aa82b339e"
Expand Down