Skip to content

Commit 5b019b8

Browse files
huv1ktimsuchanek
authored andcommitted
Prettier settings in config (#739)
* PrintWidth * Prettier width * Clean up
1 parent f41cd38 commit 5b019b8

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

packages/graphql-playground-react/src/state/sessions/reducers.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { getSelectedSessionId } from './selectors'
2626
import { getDefaultSession, defaultQuery } from '../../constants'
2727
import * as cuid from 'cuid'
2828
import { formatError } from './fetchingSagas'
29-
import { prettify } from '../../utils'
3029

3130
export interface SessionStateProps {
3231
sessions: OrderedMap<string, Session>
@@ -234,10 +233,6 @@ const reducer = handleActions(
234233
]
235234
return state.setIn(path, !state.getIn(path))
236235
},
237-
PRETTIFY_QUERY: state => {
238-
const path = ['sessions', getSelectedSessionId(state), 'query']
239-
return state.setIn(path, prettify(state.getIn(path)))
240-
},
241236
OPEN_TRACING: (state, { payload: { responseTracingHeight } }) => {
242237
return state.mergeDeepIn(
243238
['sessions', getSelectedSessionId(state)],

packages/graphql-playground-react/src/state/sessions/sagas.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import getSelectedOperationName from '../../components/Playground/util/getSelect
1212
import { getQueryFacts } from '../../components/Playground/util/getQueryFacts'
1313
import { fromJS, is } from 'immutable'
1414
import {
15+
editQuery,
1516
setVariableToType,
1617
setOperations,
1718
setOperationName,
@@ -33,7 +34,7 @@ import { getSessionDocsState } from '../docs/selectors'
3334
import { getQueryTypes } from '../../components/Playground/util/getQueryTypes'
3435
import { parse } from 'graphql'
3536
import { Session } from './reducers'
36-
import { safely } from '../../utils'
37+
import { safely, prettify } from '../../utils'
3738
import * as queryString from 'query-string'
3839

3940
function* setQueryFacts() {
@@ -187,6 +188,13 @@ function* addToHistory({ payload }) {
187188
}
188189
}
189190

191+
function* prettifyQuery() {
192+
const { query } = yield select(getSelectedSession)
193+
const settings = yield select(getSettings)
194+
const prettyQuery = prettify(query, settings['prettier.printWidth'])
195+
yield put(editQuery(prettyQuery))
196+
}
197+
190198
export const sessionsSagas = [
191199
takeLatest('GET_QUERY_FACTS', safely(setQueryFacts)),
192200
takeLatest('SET_OPERATION_NAME', safely(setQueryFacts)),
@@ -197,6 +205,7 @@ export const sessionsSagas = [
197205
takeLatest('REFETCH_SCHEMA', safely(refetchSchemaSaga)),
198206
takeLatest('SCHEMA_FETCHING_SUCCESS', safely(renewStacks)),
199207
takeEvery('QUERY_SUCCESS' as any, safely(addToHistory)),
208+
takeLatest('PRETTIFY_QUERY', safely(prettifyQuery)),
200209
]
201210

202211
// needed to fix typescript

packages/graphql-playground-react/src/state/workspace/reducers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const defaultSettings: ISettings = {
4747
'editor.fontFamily': `'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace`,
4848
'editor.theme': 'dark',
4949
'editor.reuseHeaders': true,
50+
'prettier.printWidth': 80,
5051
'request.credentials': 'omit',
5152
'tracing.hideTracingResponse': true,
5253
}

packages/graphql-playground-react/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ISettings {
2323
['editor.fontSize']: number
2424
['editor.theme']: Theme
2525
['editor.reuseHeaders']: boolean
26+
['prettier.printWidth']: number
2627
['tracing.hideTracingResponse']: boolean
2728
['request.credentials']: 'omit' | 'include' | 'same-origin'
2829
}

packages/graphql-playground-react/src/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ export function safely(cb: any) {
1212
}
1313
}
1414

15-
export function prettify(query) {
16-
return prettier.format(query, { parser: 'graphql', plugins: [graphql] })
15+
export function prettify(query: string, printWidth: number) {
16+
try {
17+
return prettier.format(query, {
18+
parser: 'graphql',
19+
printWidth,
20+
plugins: [graphql],
21+
})
22+
} catch (e) {
23+
//TODO show erros somewhere
24+
console.log(e)
25+
}
1726
}

0 commit comments

Comments
 (0)