Skip to content

Commit

Permalink
Fixes graphql#48: add a prettify query button
Browse files Browse the repository at this point in the history
  • Loading branch information
gre committed Nov 28, 2015
1 parent 817cda0 commit c45081c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
12 changes: 12 additions & 0 deletions css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ html, body {
padding-left: 3px;
}

#graphiql-container .tool-button {
cursor: pointer;
text-decoration: none;
font-family: sans-serif;
color: #ccc;
padding: 2px 4px;
margin: 0 4px;
}
#graphiql-container .tool-button:hover {
color: #333;
}

#graphiql-container .execute-button {
background: -webkit-linear-gradient(#fdfdfd, #d2d3d6);
background: linear-gradient(#fdfdfd, #d2d3d6);
Expand Down
10 changes: 10 additions & 0 deletions src/components/GraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -261,6 +264,7 @@ export class GraphiQL extends React.Component {
<div className="topBar">
{logo}
<ExecuteButton onClick={this._runEditorQuery} />
<PrettifyButton onClick={this._prettifyQuery} />
{toolbar}
</div>
{!this.state.docsOpen &&
Expand Down Expand Up @@ -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 });
Expand Down
38 changes: 38 additions & 0 deletions src/components/PrettifyButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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
*/
export class PrettifyButton extends React.Component {
static propTypes = {
onClick: PropTypes.func
}

onClick = e => {
const { onClick } = this.props;
if (onClick) {
e.preventDefault();
onClick();
}
}

render() {
return (
<a
className="tool-button"
onClick={this.onClick}
title="Prettify Query">
{"{ }"}
</a>
);
}
}

0 comments on commit c45081c

Please sign in to comment.