Skip to content

Commit 220b6b4

Browse files
andimarekbbakerman
authored andcommitted
handle special formatting for arguments descriptions
also fix general formatting: no space before colon
1 parent 0922a87 commit 220b6b4

File tree

2 files changed

+90
-47
lines changed

2 files changed

+90
-47
lines changed

src/main/java/graphql/schema/idl/SchemaPrinter.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private TypePrinter<GraphQLInterfaceType> interfacePrinter() {
170170
out.format("interface %s {\n", type.getName());
171171
type.getFieldDefinitions().forEach(fd -> {
172172
printComments(out, fd, " ");
173-
out.format(" %s%s : %s\n",
173+
out.format(" %s%s: %s\n",
174174
fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()));
175175
});
176176
out.format("}\n\n");
@@ -206,7 +206,7 @@ private TypePrinter<GraphQLObjectType> objectPrinter() {
206206
out.format("type %s {\n", type.getName());
207207
type.getFieldDefinitions().forEach(fd -> {
208208
printComments(out, fd, " ");
209-
out.format(" %s%s : %s\n",
209+
out.format(" %s%s: %s\n",
210210
fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()));
211211
});
212212
out.format("}\n\n");
@@ -223,7 +223,7 @@ private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() {
223223
out.format("input %s {\n", type.getName());
224224
type.getFieldDefinitions().forEach(fd -> {
225225
printComments(out, fd, " ");
226-
out.format(" %s : %s\n",
226+
out.format(" %s: %s\n",
227227
fd.getName(), typeString(fd.getType()));
228228
});
229229
out.format("}\n\n");
@@ -254,13 +254,13 @@ private TypePrinter<GraphQLSchema> schemaPrinter() {
254254
if (needsSchemaPrinted) {
255255
out.format("schema {\n");
256256
if (queryType != null) {
257-
out.format(" query : %s\n", queryType.getName());
257+
out.format(" query: %s\n", queryType.getName());
258258
}
259259
if (mutationType != null) {
260-
out.format(" mutation : %s\n", mutationType.getName());
260+
out.format(" mutation: %s\n", mutationType.getName());
261261
}
262262
if (subscriptionType != null) {
263-
out.format(" subscription : %s\n", subscriptionType.getName());
263+
out.format(" subscription: %s\n", subscriptionType.getName());
264264
}
265265
out.format("}\n\n");
266266
}
@@ -293,6 +293,8 @@ String typeString(GraphQLType rawType) {
293293
}
294294

295295
String argsString(List<GraphQLArgument> arguments) {
296+
boolean hasDescriptions = arguments.stream().filter(arg -> arg.getDescription() != null).count() > 0;
297+
String prefix = hasDescriptions ? " " : "";
296298
int count = 0;
297299
StringBuilder sb = new StringBuilder();
298300
for (GraphQLArgument argument : arguments) {
@@ -301,7 +303,15 @@ String argsString(List<GraphQLArgument> arguments) {
301303
} else {
302304
sb.append(", ");
303305
}
304-
sb.append(argument.getName()).append(" : ").append(typeString(argument.getType()));
306+
if (hasDescriptions) {
307+
sb.append("\n");
308+
}
309+
String description = argument.getDescription();
310+
if (description != null) {
311+
Stream<String> stream = Arrays.stream(description.split("\n"));
312+
stream.map(s -> " #" + s + "\n").forEach(sb::append);
313+
}
314+
sb.append(prefix + argument.getName()).append(": ").append(typeString(argument.getType()));
305315
Object defaultValue = argument.getDefaultValue();
306316
if (defaultValue != null) {
307317
sb.append(" = ");
@@ -314,7 +324,10 @@ String argsString(List<GraphQLArgument> arguments) {
314324
count++;
315325
}
316326
if (count > 0) {
317-
sb.append(")");
327+
if (hasDescriptions) {
328+
sb.append("\n");
329+
}
330+
sb.append(prefix + ")");
318331
}
319332
return sb.toString();
320333
}
@@ -376,6 +389,8 @@ private String getDescription(Object descriptionHolder) {
376389
return ((GraphQLInterfaceType) descriptionHolder).getDescription();
377390
} else if (descriptionHolder instanceof GraphQLScalarType) {
378391
return ((GraphQLScalarType) descriptionHolder).getDescription();
392+
} else if (descriptionHolder instanceof GraphQLArgument) {
393+
return ((GraphQLArgument) descriptionHolder).getDescription();
379394
} else {
380395
return Assert.assertShouldNeverHappen();
381396
}

src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ class SchemaPrinterTest extends Specification {
101101
}
102102

103103
def "argsString"() {
104-
def argument1 = new GraphQLArgument("arg1", "desc-arg1", list(nonNull(Scalars.GraphQLInt)), 10)
105-
def argument2 = new GraphQLArgument("arg2", "desc-arg2", GraphQLString, null)
106-
def argument3 = new GraphQLArgument("arg3", "desc-arg3", GraphQLString, "default")
104+
def argument1 = new GraphQLArgument("arg1", null, list(nonNull(Scalars.GraphQLInt)), 10)
105+
def argument2 = new GraphQLArgument("arg2", null, GraphQLString, null)
106+
def argument3 = new GraphQLArgument("arg3", null, GraphQLString, "default")
107107
def argStr = new SchemaPrinter().argsString([argument1, argument2, argument3])
108108

109109
expect:
110110

111-
argStr == "(arg1 : [Int!] = 10, arg2 : String, arg3 : String = \"default\")"
111+
argStr == "(arg1: [Int!] = 10, arg2: String, arg3: String = \"default\")"
112112
}
113113

114114
def "print type direct"() {
@@ -119,10 +119,10 @@ class SchemaPrinterTest extends Specification {
119119
expect:
120120
result ==
121121
"""interface Character {
122-
id : ID!
123-
name : String!
124-
friends : [Character]
125-
appearsIn : [Episode]!
122+
id: ID!
123+
name: String!
124+
friends: [Character]
125+
appearsIn: [Episode]!
126126
}
127127
128128
"""
@@ -157,15 +157,15 @@ class SchemaPrinterTest extends Specification {
157157
def "default root names are handled"() {
158158
def schema = generate("""
159159
type Query {
160-
field : String
160+
field: String
161161
}
162162
163163
type Mutation {
164-
field : String
164+
field: String
165165
}
166166
167167
type Subscription {
168-
field : String
168+
field: String
169169
}
170170
171171
""")
@@ -175,15 +175,15 @@ class SchemaPrinterTest extends Specification {
175175

176176
expect:
177177
result == """type Mutation {
178-
field : String
178+
field: String
179179
}
180180
181181
type Query {
182-
field : String
182+
field: String
183183
}
184184
185185
type Subscription {
186-
field : String
186+
field: String
187187
}
188188
189189
"""
@@ -192,21 +192,21 @@ type Subscription {
192192
def "schema is printed if default root names are not ALL present"() {
193193
def schema = generate("""
194194
type Query {
195-
field : String
195+
field: String
196196
}
197197
198198
type MutationX {
199-
field : String
199+
field: String
200200
}
201201
202202
type Subscription {
203-
field : String
203+
field: String
204204
}
205205
206206
schema {
207-
query : Query
208-
mutation : MutationX
209-
subscription : Subscription
207+
query: Query
208+
mutation: MutationX
209+
subscription: Subscription
210210
}
211211
212212
""")
@@ -216,21 +216,21 @@ type Subscription {
216216

217217
expect:
218218
result == """schema {
219-
query : Query
220-
mutation : MutationX
221-
subscription : Subscription
219+
query: Query
220+
mutation: MutationX
221+
subscription: Subscription
222222
}
223223
224224
type MutationX {
225-
field : String
225+
field: String
226226
}
227227
228228
type Query {
229-
field : String
229+
field: String
230230
}
231231
232232
type Subscription {
233-
field : String
233+
field: String
234234
}
235235
236236
"""
@@ -249,7 +249,7 @@ type Subscription {
249249
result == """#About Query
250250
#Second Line
251251
type Query {
252-
field : String
252+
field: String
253253
}
254254
255255
"""
@@ -268,7 +268,7 @@ type Query {
268268
result == """type Query {
269269
#About field
270270
#second
271-
field : String
271+
field: String
272272
}
273273
274274
"""
@@ -291,7 +291,7 @@ type Query {
291291

292292
then:
293293
result == """type Query {
294-
field : Enum
294+
field: Enum
295295
}
296296
297297
#About enum
@@ -327,11 +327,11 @@ enum Enum {
327327
union Union = PossibleType
328328
329329
type PossibleType {
330-
field : String
330+
field: String
331331
}
332332
333333
type Query {
334-
field : Union
334+
field: Union
335335
}
336336
337337
"""
@@ -362,15 +362,15 @@ type Query {
362362
result == """union Union = PossibleType1 | PossibleType2
363363
364364
type PossibleType1 {
365-
field : String
365+
field: String
366366
}
367367
368368
type PossibleType2 {
369-
field : String
369+
field: String
370370
}
371371
372372
type Query {
373-
field : Union
373+
field: Union
374374
}
375375
376376
"""
@@ -395,13 +395,13 @@ type Query {
395395

396396
then:
397397
result == """type Query {
398-
field(arg : Input) : String
398+
field(arg: Input): String
399399
}
400400
401401
#About input
402402
input Input {
403403
#about field
404-
field : String
404+
field: String
405405
}
406406
407407
"""
@@ -427,11 +427,11 @@ input Input {
427427
result == """#about interface
428428
interface Interface {
429429
#about field
430-
field : String
430+
field: String
431431
}
432432
433433
type Query {
434-
field : Interface
434+
field: Interface
435435
}
436436
437437
"""
@@ -464,7 +464,7 @@ type Query {
464464

465465
then:
466466
result == """type Query {
467-
field : Scalar
467+
field: Scalar
468468
}
469469
470470
#about scalar
@@ -473,5 +473,33 @@ scalar Scalar
473473
"""
474474
}
475475

476+
def "special formatting for argument descriptions"() {
477+
GraphQLFieldDefinition fieldDefinition2 = newFieldDefinition()
478+
.name("field")
479+
.argument(newArgument().name("arg1").description("about arg1").type(GraphQLString).build())
480+
.argument(newArgument().name("arg2").type(GraphQLString).build())
481+
.argument(newArgument().name("arg3").description("about 3\nsecond line").type(GraphQLString).build())
482+
.type(GraphQLString).build()
483+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition2).build()
484+
def schema = GraphQLSchema.newSchema().query(queryType).build()
485+
when:
486+
def result = new SchemaPrinter().print(schema)
487+
488+
then:
489+
result == """type Query {
490+
field(
491+
#about arg1
492+
arg1: String,
493+
arg2: String,
494+
#about 3
495+
#second line
496+
arg3: String
497+
): String
498+
}
499+
500+
"""
501+
502+
}
503+
476504

477505
}

0 commit comments

Comments
 (0)