Skip to content

Commit 200b652

Browse files
committed
fix converting introspection to SDL
1 parent 59de718 commit 200b652

File tree

4 files changed

+775
-3
lines changed

4 files changed

+775
-3
lines changed

apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/parser/introspection/introspection2sdl.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,14 @@ private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
137137
sink.writeUtf8(" $name: ${type.asGraphQLType()}")
138138
if (defaultValue != null) {
139139
sink.writeUtf8(" = ")
140-
sink.writeValue(defaultValue)
140+
if (defaultValue is String) {
141+
// defaultValue is already encoded as GraphQL, we can pass it verbatim
142+
sink.writeUtf8(defaultValue)
143+
} else {
144+
// legacy mode if we bump into an introspection schema that doesn't encode the default value
145+
sink.writeValue(defaultValue)
146+
}
147+
141148
}
142149
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
143150
}
@@ -177,7 +184,13 @@ private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
177184
sink.writeUtf8("$name: ${type.asGraphQLType()}")
178185
if (defaultValue != null) {
179186
sink.writeUtf8(" = ")
180-
sink.writeValue(defaultValue)
187+
if (defaultValue is String) {
188+
// defaultValue is already encoded as GraphQL, we can pass it verbatim
189+
sink.writeUtf8(defaultValue)
190+
} else {
191+
// legacy mode if we bump into an introspection schema that doesn't encode the default value
192+
sink.writeValue(defaultValue)
193+
}
181194
}
182195
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
183196
}

apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/GraphSdlParseTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.apollographql.apollo.compiler.parser.introspection.IntrospectionSchem
66
import com.apollographql.apollo.compiler.parser.introspection.toSDL
77
import com.apollographql.apollo.compiler.parser.sdl.GraphSdlSchema
88
import com.apollographql.apollo.compiler.parser.sdl.toIntrospectionSchema
9-
import com.google.common.truth.Truth
109
import com.google.common.truth.Truth.assertThat
1110
import org.junit.Assert.assertEquals
1211
import org.junit.Assert.fail
@@ -84,6 +83,17 @@ class GraphSdlParseTest() {
8483
assertEquals(initialSchema, finalSchema)
8584
}
8685

86+
@Test
87+
fun `defaultValues are correctly written`() {
88+
val initialSchema = IntrospectionSchema(File("src/test/sdl/default-values.json")).normalize()
89+
val sdlFile = File("build/sdl-test/schema.sdl")
90+
sdlFile.parentFile.deleteRecursively()
91+
sdlFile.parentFile.mkdirs()
92+
initialSchema.toSDL(sdlFile)
93+
94+
assertEquals(File("src/test/sdl/default-values.sdl").readText(), sdlFile.readText())
95+
}
96+
8797
/**
8898
* use to make easier diffs
8999
*/

0 commit comments

Comments
 (0)