Skip to content

Commit 3ac95ec

Browse files
committed
add a flag that keeps the old behaviour in places where we might still need it
1 parent 200b652 commit 3ac95ec

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ import okio.buffer
55
import okio.sink
66
import java.io.File
77

8-
fun IntrospectionSchema.toSDL(file: File) {
8+
/**
9+
* Previous versions of this method used to encode defaultValues as Json elements. For an example:
10+
* "defaultValue": [0, 1]
11+
*
12+
* This is wrong. The spec mandates defaultValue to be GraphQL encoded String:
13+
* "defaultValue": "[0, 1]"
14+
*
15+
* For backward compatibility, use [legacyDefaultValues] = true
16+
*/
17+
fun IntrospectionSchema.toSDL(file: File, legacyDefaultValues: Boolean) {
918
file.sink().buffer().use {
10-
toSDL(it)
19+
toSDL(it, legacyDefaultValues)
1120
}
1221
}
1322

14-
fun IntrospectionSchema.toSDL(sink: BufferedSink) {
23+
fun IntrospectionSchema.toSDL(file: File) = toSDL(file, true)
24+
25+
fun IntrospectionSchema.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
1526
val builtinsTypes = setOf(
1627
"Int", "Float", "String", "Boolean", "ID",
1728
"__Directive", "__DirectiveLocation", "__EnumValue", "__Field", "__InputValue", "__Schema", "__Type", "__TypeKind"
@@ -25,9 +36,9 @@ fun IntrospectionSchema.toSDL(sink: BufferedSink) {
2536
when (it) {
2637
is IntrospectionSchema.Type.Scalar -> it.toSDL(sink)
2738
is IntrospectionSchema.Type.Enum -> it.toSDL(sink)
28-
is IntrospectionSchema.Type.InputObject -> it.toSDL(sink)
29-
is IntrospectionSchema.Type.Interface -> it.toSDL(sink)
30-
is IntrospectionSchema.Type.Object -> it.toSDL(sink, allInterfaces)
39+
is IntrospectionSchema.Type.InputObject -> it.toSDL(sink, legacyDefaultValues)
40+
is IntrospectionSchema.Type.Interface -> it.toSDL(sink, legacyDefaultValues)
41+
is IntrospectionSchema.Type.Object -> it.toSDL(sink, allInterfaces, legacyDefaultValues)
3142
is IntrospectionSchema.Type.Union -> it.toSDL(sink)
3243
}
3344
// Add a newline as a separation
@@ -40,6 +51,8 @@ fun IntrospectionSchema.toSDL(sink: BufferedSink) {
4051
sink.writeUtf8("}\n")
4152
}
4253

54+
fun IntrospectionSchema.toSDL(sink: BufferedSink) = toSDL(sink, true)
55+
4356
private fun BufferedSink.writeDescription(description: String?, indent: String = "") {
4457
if (!description.isNullOrBlank()) {
4558
writeUtf8(
@@ -86,11 +99,11 @@ private fun IntrospectionSchema.Type.Enum.Value.toSDL(sink: BufferedSink) {
8699
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
87100
}
88101

89-
private fun IntrospectionSchema.Type.InputObject.toSDL(sink: BufferedSink) {
102+
private fun IntrospectionSchema.Type.InputObject.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
90103
sink.writeDescription(description)
91104
sink.writeUtf8("input $name {\n")
92105
inputFields.forEach {
93-
it.toSDL(sink)
106+
it.toSDL(sink, legacyDefaultValues)
94107
sink.writeUtf8("\n")
95108
}
96109
sink.writeUtf8("}\n")
@@ -132,12 +145,12 @@ private fun BufferedSink.writeValue(value: Any?) {
132145
}
133146
}
134147

135-
private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
148+
private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
136149
sink.writeDescription(description, " ")
137150
sink.writeUtf8(" $name: ${type.asGraphQLType()}")
138151
if (defaultValue != null) {
139152
sink.writeUtf8(" = ")
140-
if (defaultValue is String) {
153+
if (!legacyDefaultValues && defaultValue is String) {
141154
// defaultValue is already encoded as GraphQL, we can pass it verbatim
142155
sink.writeUtf8(defaultValue)
143156
} else {
@@ -149,23 +162,23 @@ private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
149162
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
150163
}
151164

152-
private fun IntrospectionSchema.Type.Interface.toSDL(sink: BufferedSink) {
165+
private fun IntrospectionSchema.Type.Interface.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
153166
sink.writeDescription(description)
154167
sink.writeUtf8("interface $name {\n")
155168
fields?.forEach {
156-
it.toSDL(sink)
169+
it.toSDL(sink, legacyDefaultValue)
157170
sink.writeUtf8("\n")
158171
}
159172
sink.writeUtf8("}\n")
160173
}
161174

162-
private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink) {
175+
private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
163176
sink.writeDescription(description, " ")
164177
sink.writeUtf8(" $name")
165178
if (args.isNotEmpty()) {
166179
sink.writeUtf8("(")
167180
args.forEachIndexed { index, arg ->
168-
arg.toSDL(sink)
181+
arg.toSDL(sink, legacyDefaultValue)
169182
if (index != args.size - 1) {
170183
sink.writeUtf8(", ")
171184
}
@@ -176,15 +189,15 @@ private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink) {
176189
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
177190
}
178191

179-
private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
192+
private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
180193
if (!description.isNullOrBlank()) {
181194
// Write the description inline
182195
sink.writeUtf8("\"\"\"$description\"\"\" ")
183196
}
184197
sink.writeUtf8("$name: ${type.asGraphQLType()}")
185198
if (defaultValue != null) {
186199
sink.writeUtf8(" = ")
187-
if (defaultValue is String) {
200+
if (!legacyDefaultValue && defaultValue is String) {
188201
// defaultValue is already encoded as GraphQL, we can pass it verbatim
189202
sink.writeUtf8(defaultValue)
190203
} else {
@@ -195,7 +208,7 @@ private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
195208
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
196209
}
197210

198-
private fun IntrospectionSchema.Type.Object.toSDL(sink: BufferedSink, interfaces: List<IntrospectionSchema.Type.Interface>) {
211+
private fun IntrospectionSchema.Type.Object.toSDL(sink: BufferedSink, interfaces: List<IntrospectionSchema.Type.Interface>, legacyDefaultValue: Boolean) {
199212
sink.writeDescription(description, "")
200213
sink.writeUtf8("type $name")
201214
val implements = interfaces.filter {
@@ -216,7 +229,7 @@ private fun IntrospectionSchema.Type.Object.toSDL(sink: BufferedSink, interfaces
216229
sink.writeUtf8(" {\n")
217230

218231
fields?.forEach {
219-
it.toSDL(sink)
232+
it.toSDL(sink, legacyDefaultValue)
220233
sink.writeUtf8("\n")
221234
}
222235

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class GraphSdlParseTest() {
8989
val sdlFile = File("build/sdl-test/schema.sdl")
9090
sdlFile.parentFile.deleteRecursively()
9191
sdlFile.parentFile.mkdirs()
92-
initialSchema.toSDL(sdlFile)
92+
initialSchema.toSDL(sdlFile, legacyDefaultValues = false)
9393

9494
assertEquals(File("src/test/sdl/default-values.sdl").readText(), sdlFile.readText())
9595
}

apollo-gradle-plugin/src/main/kotlin/com/apollographql/apollo/gradle/internal/ApolloDownloadSchemaCliTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ abstract class ApolloDownloadSchemaCliTask : DefaultTask() {
176176
} else {
177177
if (sdlSchema == null) {
178178
val buffer = Buffer()
179-
IntrospectionSchema(introspectionSchema!!.byteInputStream()).toSDL(buffer)
179+
IntrospectionSchema(introspectionSchema!!.byteInputStream()).toSDL(buffer, legacyDefaultValues = false)
180180
sdlSchema = buffer.readString(Charset.defaultCharset())
181181
}
182182
schema.writeText(sdlSchema)

apollo-gradle-plugin/src/main/kotlin/com/apollographql/apollo/gradle/internal/ApolloDownloadSchemaTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ abstract class ApolloDownloadSchemaTask : DefaultTask() {
4343
if (schema.extension == "json") {
4444
schema.writeText(introspection)
4545
} else {
46-
IntrospectionSchema(Buffer().writeUtf8(introspection).inputStream()).toSDL(schema)
46+
IntrospectionSchema(Buffer().writeUtf8(introspection).inputStream()).toSDL(schema, legacyDefaultValues = false)
4747
}
4848
}
4949

0 commit comments

Comments
 (0)