diff --git a/css/app.css b/css/app.css index bee47931619..94618a265ad 100644 --- a/css/app.css +++ b/css/app.css @@ -215,6 +215,21 @@ html, body { padding-left: 3px; } +#graphiql-container .tool-button { + cursor: pointer; + text-decoration: none; + font-family: sans-serif; + color: #333; + padding: 2px 4px; + margin: 0 4px; +} +#graphiql-container .tool-button:active { + color: #999; +} +#graphiql-container .tool-button.error { + color: #c00; +} + #graphiql-container .execute-button { background: -webkit-linear-gradient(#fdfdfd, #d2d3d6); background: linear-gradient(#fdfdfd, #d2d3d6); diff --git a/src/components/GraphiQL.js b/src/components/GraphiQL.js index a4e3f956a4b..6ef6f1311c9 100644 --- a/src/components/GraphiQL.js +++ b/src/components/GraphiQL.js @@ -8,10 +8,13 @@ import React, { PropTypes } from 'react'; import ReactDOM from 'react-dom'; +import { print } from 'graphql/language/printer'; +import { parse } from 'graphql/language/parser'; import { GraphQLSchema } from 'graphql/type'; import { buildClientSchema } from 'graphql/utilities'; import find from 'graphql/jsutils/find'; import { ExecuteButton } from './ExecuteButton'; +import { PrettifyButton } from './PrettifyButton'; import { QueryEditor } from './QueryEditor'; import { VariableEditor } from './VariableEditor'; import { ResultViewer } from './ResultViewer'; @@ -261,6 +264,7 @@ export class GraphiQL extends React.Component {
{logo} + {toolbar}
{!this.state.docsOpen && @@ -349,6 +353,12 @@ export class GraphiQL extends React.Component { }); } + _prettifyQuery = () => { + const query = print(parse(this.state.query)); + const editor = this.refs.queryEditor.getCodeMirror(); + editor.setValue(query); + } + _onEditQuery = value => { this._storageSet('query', value); this.setState({ query: value }); diff --git a/src/components/PrettifyButton.js b/src/components/PrettifyButton.js new file mode 100644 index 00000000000..22702a56fa9 --- /dev/null +++ b/src/components/PrettifyButton.js @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the + * LICENSE-examples file in the root directory of this source tree. + */ + +import React, { PropTypes } from 'react'; + + +/** + * PrettifyButton + * + * A {} button that allows to unminify a graphql query + */ +export class PrettifyButton extends React.Component { + static propTypes = { + onClick: PropTypes.func + } + + constructor(props) { + super(props); + this.state = { + error: null + }; + } + + onClick = e => { + e.preventDefault(); + try { + this.props.onClick(); + this.setState({ error: null }); + } catch (error) { + this.setState({ error }); + } + }; + + render() { + const { error } = this.state; + return ( + + {'{ }'} + + ); + } +}