Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Add a "Prettify" button to the in-app GraphQL Console #767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion app/components/user/graphql/GraphQLExplorerConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Dropdown from 'app/components/shared/Dropdown';
import FlashesStore from 'app/stores/FlashesStore';
import GraphQLExplorerConsoleEditor from "./GraphQLExplorerConsoleEditor";
import GraphQLExplorerConsoleResultsViewer from "./GraphQLExplorerConsoleResultsViewer";
import { executeQuery } from "./query";
import { executeQuery, prettifyQuery } from "./query";
import consoleState from "./consoleState";
import type { RelayProp } from 'react-relay';
import type { GraphQLExplorerConsoleSnippetQueryResponse } from './__generated__/GraphQLExplorerConsoleSnippetQuery.graphql';
Expand Down Expand Up @@ -158,6 +158,15 @@ class GraphQLExplorerConsole extends React.PureComponent<Props, State> {
</Button>
</div>
</Dropdown>

<Button
theme="default"
outline={true}
className="ml2"
onClick={this.handlePrettifyClick}
>
Prettify
</Button>
</div>
</div>

Expand Down Expand Up @@ -282,6 +291,12 @@ class GraphQLExplorerConsole extends React.PureComponent<Props, State> {
this.executeCurrentQuery();
};

handlePrettifyClick = () => {
prettifyQuery(this.state.query, (query) => {
this.setState({ query: query });
});
}
Copy link
Contributor

@ticky ticky Jan 17, 2019

Choose a reason for hiding this comment

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

I wonder if we’d want to do the lazy-load-on-hover trick here? I did it in ImageUploadField:

// Make sure we're loading the image processing code now the user is dragging something around
this.getLoadImage();

getLoadImage = () => {
// If we don't have our import promise stored, create it
if (!this._loadImage) {
this._loadImage = import('blueimp-load-image');
}
// Finally, return the promise
return this._loadImage;
}

withLoadImage = (callback: Function) => {
// Call back with the default loadImage
// function, once the import promise resolves
this.getLoadImage().then((module) => callback(module.default));
}


handleShareLinkClick = () => {
if (this.shareLinkTextInput) {
this.shareLinkTextInput.select();
Expand Down
6 changes: 6 additions & 0 deletions app/components/user/graphql/GraphQLExplorerConsoleEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ class GraphQLExplorerConsoleEditor extends React.PureComponent<Props & LoadedPro
}
}

componentDidUpdate() {
if (this.codeMirrorInstance && this.props.value != this.codeMirrorInstance.getValue()) {
this.codeMirrorInstance.setValue(this.props.value);
}
}

render() {
return (
<div style={{ height: "100%" }}>
Expand Down
6 changes: 6 additions & 0 deletions app/components/user/graphql/prettier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import prettier from "prettier/standalone";
import graphQLPlugin from "prettier/parser-graphql";

export default function(query) {
return prettier.format(query, { parser: "graphql", plugins: [graphQLPlugin] });
}
6 changes: 6 additions & 0 deletions app/components/user/graphql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export function interpolateQuery(query, interpolations) {
return query;
}

export function prettifyQuery(query, callback) {
import("./prettier").then((modules) => {
callback(modules.default(query));
});
}

export function findQueryOperationNames(query) {
if (!query) {
return undefined;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"moment": "2.22.2",
"moment-duration-format": "https://github.com/ticky/moment-duration-format.git#951b8be1d1b0424824363aa912cbf066546bf384",
"object-assign": "4.1.1",
"prettier": "^1.15.3",
"prop-types": "15.6.2",
"pusher-js": "4.3.1",
"qrcode.react": "0.8.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12070,6 +12070,11 @@ prettier@^1.14.3:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.3.tgz#90238dd4c0684b7edce5f83b0fb7328e48bd0895"
integrity sha512-qZDVnCrnpsRJJq5nSsiHCE3BYMED2OtsI+cmzIzF1QIfqm5ALf8tEJcO27zV1gKNKRPdhjO0dNWnrzssDQ1tFg==

prettier@^1.15.3:
version "1.15.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.15.3.tgz#1feaac5bdd181237b54dbe65d874e02a1472786a"
integrity sha512-gAU9AGAPMaKb3NNSUUuhhFAS7SCO4ALTN4nRIn6PJ075Qd28Yn2Ig2ahEJWdJwJmlEBTUfC7mMUSFy8MwsOCfg==

pretty-bytes@^1.0.2:
version "1.0.4"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-1.0.4.tgz#0a22e8210609ad35542f8c8d5d2159aff0751c84"
Expand Down