Skip to content

Commit bcc3a70

Browse files
authored
Remove the extra whitespace before the query arguments and fragment name (#362)
* Add the compacted option to opt in the compacted formatter * Add test case
1 parent a54bdfe commit bcc3a70

24 files changed

Lines changed: 503 additions & 1 deletion

formatter/formatter.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func WithoutDescription() FormatterOption {
4747
}
4848
}
4949

50+
// WithCompacted enables compacted output, which removes all unnecessary whitespace.
51+
func WithCompacted() FormatterOption {
52+
return func(f *formatter) {
53+
f.compacted = true
54+
}
55+
}
56+
5057
func NewFormatter(w io.Writer, options ...FormatterOption) Formatter {
5158
f := &formatter{
5259
indent: "\t",
@@ -66,6 +73,7 @@ type formatter struct {
6673
emitBuiltin bool
6774
emitComments bool
6875
omitDescription bool
76+
compacted bool
6977

7078
padNext bool
7179
lineHead bool
@@ -553,6 +561,9 @@ func (f *formatter) FormatOperationDefinition(def *ast.OperationDefinition) {
553561
f.WriteWord(string(def.Operation))
554562
if def.Name != "" {
555563
f.WriteWord(def.Name)
564+
if f.compacted {
565+
f.NoPadding()
566+
}
556567
}
557568
f.FormatVariableDefinitionList(def.VariableDefinitions)
558569
f.FormatDirectiveList(def.Directives)
@@ -707,7 +718,11 @@ func (f *formatter) FormatField(field *ast.Field) {
707718
func (f *formatter) FormatFragmentSpread(spread *ast.FragmentSpread) {
708719
f.FormatCommentGroup(spread.Comment)
709720

710-
f.WriteWord("...").WriteWord(spread.Name)
721+
f.WriteWord("...")
722+
if f.compacted {
723+
f.NoPadding()
724+
}
725+
f.WriteWord(spread.Name)
711726

712727
f.FormatDirectiveList(spread.Directives)
713728
}

formatter/formatter_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var optionSets = []struct {
2626
{"spaceIndent", []formatter.FormatterOption{formatter.WithIndent(" ")}},
2727
{"comments", []formatter.FormatterOption{formatter.WithComments()}},
2828
{"no_description", []formatter.FormatterOption{formatter.WithoutDescription()}},
29+
{"compacted", []formatter.FormatterOption{formatter.WithCompacted()}},
2930
}
3031

3132
func TestFormatter_FormatSchema(t *testing.T) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query FooBarQuery($after: String!) {
2+
fizzList(first: 100, after: $after) {
3+
nodes {
4+
id
5+
}
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
bar: foo
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query FooBarQuery($after: String!) {
2+
fizzList(first: 100, after: $after) {
3+
nodes {
4+
id
5+
...FooFragment
6+
... on Foo {
7+
id
8+
}
9+
... {
10+
id
11+
}
12+
name
13+
}
14+
}
15+
}
16+
fragment FooFragment on Foo {
17+
id
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query ($first: Int = 30, $after: String!) {
2+
searchCats(first: $first, after: $after) {
3+
nodes {
4+
id
5+
name
6+
}
7+
}
8+
}

formatter/testdata/baseline/FormatSchema/compacted/comment.graphql

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Cat0 description
3+
"""
4+
scalar Cat0
5+
type Cat1 {
6+
name: String
7+
}
8+
interface Cat2 {
9+
name: String
10+
}
11+
union Cat3 = Cat3_0 | Cat3_1 | Cat3_2
12+
type Cat3_0 {
13+
name: String
14+
}
15+
type Cat3_1 {
16+
name: String
17+
}
18+
type Cat3_2 {
19+
name: String
20+
}
21+
enum Cat4 {
22+
NFC
23+
MAINECOON
24+
}
25+
input Cat5 {
26+
name: String
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Cat is best kawaii animal in the world.
3+
meow!
4+
"""
5+
type Cat {
6+
"""
7+
Shiny brillian name.
8+
"""
9+
name: String
10+
"""
11+
Only "meow" is allowed.
12+
"""
13+
speaks: String
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
directive @bar repeatable on FIELD | OBJECT
2+
directive @foo on FIELD | OBJECT

0 commit comments

Comments
 (0)