Skip to content

Commit 407afbc

Browse files
Vlad ZhukovKent C. Dodds
authored andcommitted
feat: support GraphQL files (#128)
1 parent 977977a commit 407afbc

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/__tests__/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,6 @@ const tests = [
162162
},
163163
output: '.stop {\n color: red;\n}',
164164
},
165-
{
166-
title: 'JSON example',
167-
input: {
168-
text: '{ "foo": "bar"}',
169-
filePath: path.resolve('./test.json'),
170-
},
171-
output: '{ "foo": "bar" }',
172-
},
173165
{
174166
title: 'TypeScript example',
175167
input: {
@@ -178,6 +170,22 @@ const tests = [
178170
},
179171
output: 'function Foo(this: void) {\n return this;\n}',
180172
},
173+
{
174+
title: 'GraphQL example',
175+
input: {
176+
text: 'type Query{test: Test}',
177+
filePath: path.resolve('./test.gql'),
178+
},
179+
output: 'type Query {\n test: Test\n}',
180+
},
181+
{
182+
title: 'JSON example',
183+
input: {
184+
text: '{ "foo": "bar"}',
185+
filePath: path.resolve('./test.json'),
186+
},
187+
output: '{ "foo": "bar" }',
188+
},
181189
]
182190

183191
beforeEach(() => {

src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,27 @@ function format(options) {
8686
)
8787

8888
const isCss = /\.(css|less|scss)$/.test(filePath)
89+
const isTypeScript = /\.(ts|tsx)$/.test(filePath)
90+
const isGraphQL = /\.(graphql|gql)$/.test(filePath)
8991
const isJson = /\.json$/.test(filePath)
90-
const isTypeScript = /\.tsx?$/.test(filePath)
9192

9293
if (isCss) {
9394
formattingOptions.prettier.parser = 'postcss'
94-
} else if (isJson) {
95-
formattingOptions.prettier.parser = 'json'
96-
formattingOptions.prettier.trailingComma = 'none'
9795
} else if (isTypeScript) {
9896
formattingOptions.prettier.parser = 'typescript'
9997
// XXX: It seems babylon is getting a TypeScript plugin.
10098
// Should that be used instead?
10199
formattingOptions.eslint.parser = 'typescript-eslint-parser'
100+
} else if (isGraphQL) {
101+
formattingOptions.prettier.parser = 'graphql'
102+
} else if (isJson) {
103+
formattingOptions.prettier.parser = 'json'
104+
formattingOptions.prettier.trailingComma = 'none'
102105
}
103106

104107
const prettify = createPrettify(formattingOptions.prettier, prettierPath)
105108

106-
if (isCss || isJson) {
109+
if (isCss || isGraphQL || isJson) {
107110
return prettify(text, filePath)
108111
}
109112

0 commit comments

Comments
 (0)