Skip to content

Commit

Permalink
Prettier settings in config (#739)
Browse files Browse the repository at this point in the history
* PrintWidth

* Prettier width

* Clean up
  • Loading branch information
huv1k authored and timsuchanek committed Jun 26, 2018
1 parent f41cd38 commit 5b019b8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
Expand Up @@ -26,7 +26,6 @@ import { getSelectedSessionId } from './selectors'
import { getDefaultSession, defaultQuery } from '../../constants'
import * as cuid from 'cuid'
import { formatError } from './fetchingSagas'
import { prettify } from '../../utils'

export interface SessionStateProps {
sessions: OrderedMap<string, Session>
Expand Down Expand Up @@ -234,10 +233,6 @@ const reducer = handleActions(
]
return state.setIn(path, !state.getIn(path))
},
PRETTIFY_QUERY: state => {
const path = ['sessions', getSelectedSessionId(state), 'query']
return state.setIn(path, prettify(state.getIn(path)))
},
OPEN_TRACING: (state, { payload: { responseTracingHeight } }) => {
return state.mergeDeepIn(
['sessions', getSelectedSessionId(state)],
Expand Down
11 changes: 10 additions & 1 deletion packages/graphql-playground-react/src/state/sessions/sagas.ts
Expand Up @@ -12,6 +12,7 @@ import getSelectedOperationName from '../../components/Playground/util/getSelect
import { getQueryFacts } from '../../components/Playground/util/getQueryFacts'
import { fromJS, is } from 'immutable'
import {
editQuery,
setVariableToType,
setOperations,
setOperationName,
Expand All @@ -33,7 +34,7 @@ import { getSessionDocsState } from '../docs/selectors'
import { getQueryTypes } from '../../components/Playground/util/getQueryTypes'
import { parse } from 'graphql'
import { Session } from './reducers'
import { safely } from '../../utils'
import { safely, prettify } from '../../utils'
import * as queryString from 'query-string'

function* setQueryFacts() {
Expand Down Expand Up @@ -187,6 +188,13 @@ function* addToHistory({ payload }) {
}
}

function* prettifyQuery() {
const { query } = yield select(getSelectedSession)
const settings = yield select(getSettings)
const prettyQuery = prettify(query, settings['prettier.printWidth'])
yield put(editQuery(prettyQuery))
}

export const sessionsSagas = [
takeLatest('GET_QUERY_FACTS', safely(setQueryFacts)),
takeLatest('SET_OPERATION_NAME', safely(setQueryFacts)),
Expand All @@ -197,6 +205,7 @@ export const sessionsSagas = [
takeLatest('REFETCH_SCHEMA', safely(refetchSchemaSaga)),
takeLatest('SCHEMA_FETCHING_SUCCESS', safely(renewStacks)),
takeEvery('QUERY_SUCCESS' as any, safely(addToHistory)),
takeLatest('PRETTIFY_QUERY', safely(prettifyQuery)),
]

// needed to fix typescript
Expand Down
Expand Up @@ -47,6 +47,7 @@ export const defaultSettings: ISettings = {
'editor.fontFamily': `'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace`,
'editor.theme': 'dark',
'editor.reuseHeaders': true,
'prettier.printWidth': 80,
'request.credentials': 'omit',
'tracing.hideTracingResponse': true,
}
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-playground-react/src/types.ts
Expand Up @@ -23,6 +23,7 @@ export interface ISettings {
['editor.fontSize']: number
['editor.theme']: Theme
['editor.reuseHeaders']: boolean
['prettier.printWidth']: number
['tracing.hideTracingResponse']: boolean
['request.credentials']: 'omit' | 'include' | 'same-origin'
}
13 changes: 11 additions & 2 deletions packages/graphql-playground-react/src/utils.ts
Expand Up @@ -12,6 +12,15 @@ export function safely(cb: any) {
}
}

export function prettify(query) {
return prettier.format(query, { parser: 'graphql', plugins: [graphql] })
export function prettify(query: string, printWidth: number) {
try {
return prettier.format(query, {
parser: 'graphql',
printWidth,
plugins: [graphql],
})
} catch (e) {
//TODO show erros somewhere
console.log(e)
}
}

0 comments on commit 5b019b8

Please sign in to comment.