Skip to content

Commit 3263512

Browse files
authored
Merge pull request #12 from mstachniuk/ms
#11 Comments shouldn't start from space (by default)
2 parents 922f266 + 8f3218f commit 3263512

File tree

11 files changed

+3670
-2817
lines changed

11 files changed

+3670
-2817
lines changed

core/src/main/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/Generator.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Generator {
1111
* This function generate GraphQL schema language (also called Graphql DSL or SDL) from
1212
* <a href="https://graphql.org/learn/introspection/">introspection query</a> result.
1313
*
14-
* Argument for this function should be in format returned by introspection query:
14+
* Argument for this function should be in JSON format returned by introspection query:
1515
* ```
1616
* {
1717
* "data": {
@@ -30,6 +30,18 @@ class Generator {
3030
* @return GraphQL Schema definition
3131
*/
3232
fun generate(input: String): String {
33-
return GeneratorImpl().generate(input)
33+
return GeneratorImpl().generate(input, GeneratorSettings())
34+
}
35+
36+
/**
37+
* Another version of [generate] method with additional settings argument.
38+
*
39+
* @param input Introspection query result
40+
* @param settings Settings how to generate Schema
41+
* @return GraphQL Schema definition
42+
* @since 0.2.0
43+
*/
44+
fun generate(input: String, settings: GeneratorSettings): String {
45+
return GeneratorImpl().generate(input, settings)
3446
}
3547
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.mstachniuk.graphqlschemafromintrospectiongenerator
2+
3+
/**
4+
* Class for Generator Settings
5+
*
6+
* @property trimStartComments Comments in GraphQL Schema usually start from leading space (between # and first letter). This lead that graphql-java will generate description field also with leading space what can lead to different generated schema. To avoid that this property is set to false by default.
7+
*/
8+
data class GeneratorSettings(val trimStartComments: Boolean = false)

core/src/main/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/internal/GeneratorImpl.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package io.github.mstachniuk.graphqlschemafromintrospectiongenerator.internal
22

33
import com.beust.klaxon.Klaxon
4+
import io.github.mstachniuk.graphqlschemafromintrospectiongenerator.GeneratorSettings
45

56
class GeneratorImpl {
67

78
val margin = " "
89
val primitiveScalars = listOf("Boolean", "Float", "ID", "Int", "String")
910

10-
fun generate(input: String): String {
11+
fun generate(
12+
input: String,
13+
settings: GeneratorSettings
14+
): String {
1115
val response = Klaxon().parse<IntrospectionResponse>(input) ?: return ""
1216

13-
val output = printSchema(response) + printTypes(response)
17+
val output = printSchema(response) + printTypes(response, settings)
1418

1519
return output.trimIndent().trimIndent()
1620
}
@@ -42,7 +46,7 @@ class GeneratorImpl {
4246
private fun isCustomSubscriptionType(response: IntrospectionResponse) =
4347
response.data.schema.subscriptionType != null && response.data.schema.subscriptionType.name != "Subscription"
4448

45-
private fun printTypes(response: IntrospectionResponse): String {
49+
private fun printTypes(response: IntrospectionResponse, settings: GeneratorSettings): String {
4650
val types = response.data.schema.types!!
4751
.filter { !it.name.startsWith("__") }
4852
.filter { !primitiveScalars.contains(it.name) }
@@ -60,14 +64,14 @@ class GeneratorImpl {
6064
else -> "UNKNOWN_TYPE"
6165
}
6266

63-
output += printDescription(it, false)
67+
output += printDescription(it, settings, false)
6468
output += "$kind ${it.name}${printInterfaces(it.interfaces)}"
65-
output += printBody(it)
69+
output += printBody(it, settings)
6670
}
6771
return output
6872
}
6973

70-
private fun printBody(type: GraphQLType): String {
74+
private fun printBody(type: GraphQLType, settings: GeneratorSettings): String {
7175
if (type.kind == "UNION") {
7276
return " = ${type.possibleTypes
7377
.sortedBy { it.name }
@@ -79,23 +83,23 @@ class GeneratorImpl {
7983
var output = " {\n"
8084
type.fields.sortedBy { it.name }
8185
.forEach {
82-
output += printField(it)
86+
output += printField(it, settings)
8387
}
8488
type.inputFields.sortedBy { it.name }
8589
.forEach {
86-
output += printField(it)
90+
output += printField(it, settings)
8791
}
88-
output += printEnumTypes(type.enumValues)
92+
output += printEnumTypes(type.enumValues, settings)
8993

9094
output += "}\n\n"
9195
return output
9296
}
9397

94-
private fun printEnumTypes(enumValues: List<GraphQLEnumType>): String {
98+
private fun printEnumTypes(enumValues: List<GraphQLEnumType>, settings: GeneratorSettings): String {
9599
if (containsDescription(enumValues)) {
96100
val enums = enumValues
97101
.sortedBy { it.name }
98-
.map { "${printDescription(it)}$margin${it.name}" }
102+
.map { "${printDescription(it, settings)}$margin${it.name}" }
99103
.joinToString("\n")
100104
return "$enums\n"
101105
}
@@ -107,13 +111,17 @@ class GeneratorImpl {
107111
}
108112
}
109113

110-
private fun printDescription(it: Descriptable, addMargin: Boolean = true): String {
114+
private fun printDescription(it: Descriptable, settings: GeneratorSettings, addMargin: Boolean = true): String {
111115
var output = ""
112116
if (it.description.isNotBlank()) {
113117
if (addMargin) {
114118
output += margin
115119
}
116-
output += "# ${it.description.trim().replace("\n", "\n$margin# ")}\n"
120+
val desc = when {
121+
settings.trimStartComments -> it.description.trim() // trim start and end
122+
else -> it.description.trimEnd() // trim only end otherwise
123+
}
124+
output += "#${desc.replace("\n", "\n$margin#")}\n"
117125
}
118126
return output
119127
}
@@ -128,9 +136,9 @@ class GeneratorImpl {
128136
return type.name
129137
}
130138

131-
private fun printField(field: GraphQLField): String {
132-
val arguments = printArguments(field.args.sortedBy { it.name })
133-
return "${printDescription(field)}$margin${field.name}$arguments: ${printType(field.type)}" +
139+
private fun printField(field: GraphQLField, settings: GeneratorSettings): String {
140+
val arguments = printArguments(field.args.sortedBy { it.name }, settings)
141+
return "${printDescription(field, settings)}$margin${field.name}$arguments: ${printType(field.type)}" +
134142
"${printDefaultValue(field)}\n"
135143
}
136144

@@ -152,11 +160,11 @@ class GeneratorImpl {
152160
}
153161
}
154162

155-
private fun printArguments(args: List<GraphQLField>): String {
163+
private fun printArguments(args: List<GraphQLField>, settings: GeneratorSettings): String {
156164
if (containsDescription(args)) {
157165
val arguments = args
158166
.map {
159-
"${printDescription(it)}$margin${it.name}: ${printType(it.type)}${printDefaultValue(it)}"
167+
"${printDescription(it, settings)}$margin${it.name}: ${printType(it.type)}${printDefaultValue(it)}"
160168
}
161169
.joinToString("\n")
162170
if (arguments.isNotBlank()) {

core/src/test/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/GeneratorTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.mstachniuk.graphqlschemafromintrospectiongenerator
22

33
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
45
import org.junit.jupiter.api.TestInstance
56
import org.junit.jupiter.params.ParameterizedTest
67
import org.junit.jupiter.params.provider.MethodSource
@@ -30,4 +31,28 @@ internal class GeneratorTest {
3031
.replace(".json", ".graphqls")) }
3132
return list.stream()
3233
}
34+
35+
@Test
36+
fun `should trim space in comments`() {
37+
val path = System.getProperty("user.dir") + "/src/test/resources/testdata/input-6.json"
38+
val input = File(path).readText().trimIndent()
39+
val gen = Generator()
40+
41+
val result = gen.generate(input, GeneratorSettings(true))
42+
43+
val expectedResult = """#description of customer
44+
type Customer {
45+
#unique id
46+
id: ID!
47+
#lastname comment with 5 leading spaces
48+
lastname: String
49+
#name comment without leading space
50+
name: String
51+
}
52+
53+
type Query {
54+
customer(id: String): Customer
55+
}"""
56+
assertEquals(expectedResult, result)
57+
}
3358
}

core/src/test/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/SchemaToSchemaTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SchemaToSchemaTest {
2323

2424
// when
2525
val jsonString = graphql.runIntrospectionQuery()
26+
// println(jsonString)
2627
val generatedSchema = generator.generate(jsonString)
2728

2829
// then

0 commit comments

Comments
 (0)