Skip to content

Commit

Permalink
add a flag that keeps the old behaviour in places where we might stil…
Browse files Browse the repository at this point in the history
…l need it
  • Loading branch information
martinbonnin committed Aug 4, 2022
1 parent 200b652 commit 3ac95ec
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import okio.buffer
import okio.sink
import java.io.File

fun IntrospectionSchema.toSDL(file: File) {
/**
* Previous versions of this method used to encode defaultValues as Json elements. For an example:
* "defaultValue": [0, 1]
*
* This is wrong. The spec mandates defaultValue to be GraphQL encoded String:
* "defaultValue": "[0, 1]"
*
* For backward compatibility, use [legacyDefaultValues] = true
*/
fun IntrospectionSchema.toSDL(file: File, legacyDefaultValues: Boolean) {
file.sink().buffer().use {
toSDL(it)
toSDL(it, legacyDefaultValues)
}
}

fun IntrospectionSchema.toSDL(sink: BufferedSink) {
fun IntrospectionSchema.toSDL(file: File) = toSDL(file, true)

fun IntrospectionSchema.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
val builtinsTypes = setOf(
"Int", "Float", "String", "Boolean", "ID",
"__Directive", "__DirectiveLocation", "__EnumValue", "__Field", "__InputValue", "__Schema", "__Type", "__TypeKind"
Expand All @@ -25,9 +36,9 @@ fun IntrospectionSchema.toSDL(sink: BufferedSink) {
when (it) {
is IntrospectionSchema.Type.Scalar -> it.toSDL(sink)
is IntrospectionSchema.Type.Enum -> it.toSDL(sink)
is IntrospectionSchema.Type.InputObject -> it.toSDL(sink)
is IntrospectionSchema.Type.Interface -> it.toSDL(sink)
is IntrospectionSchema.Type.Object -> it.toSDL(sink, allInterfaces)
is IntrospectionSchema.Type.InputObject -> it.toSDL(sink, legacyDefaultValues)
is IntrospectionSchema.Type.Interface -> it.toSDL(sink, legacyDefaultValues)
is IntrospectionSchema.Type.Object -> it.toSDL(sink, allInterfaces, legacyDefaultValues)
is IntrospectionSchema.Type.Union -> it.toSDL(sink)
}
// Add a newline as a separation
Expand All @@ -40,6 +51,8 @@ fun IntrospectionSchema.toSDL(sink: BufferedSink) {
sink.writeUtf8("}\n")
}

fun IntrospectionSchema.toSDL(sink: BufferedSink) = toSDL(sink, true)

private fun BufferedSink.writeDescription(description: String?, indent: String = "") {
if (!description.isNullOrBlank()) {
writeUtf8(
Expand Down Expand Up @@ -86,11 +99,11 @@ private fun IntrospectionSchema.Type.Enum.Value.toSDL(sink: BufferedSink) {
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}

private fun IntrospectionSchema.Type.InputObject.toSDL(sink: BufferedSink) {
private fun IntrospectionSchema.Type.InputObject.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
sink.writeDescription(description)
sink.writeUtf8("input $name {\n")
inputFields.forEach {
it.toSDL(sink)
it.toSDL(sink, legacyDefaultValues)
sink.writeUtf8("\n")
}
sink.writeUtf8("}\n")
Expand Down Expand Up @@ -132,12 +145,12 @@ private fun BufferedSink.writeValue(value: Any?) {
}
}

private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink, legacyDefaultValues: Boolean) {
sink.writeDescription(description, " ")
sink.writeUtf8(" $name: ${type.asGraphQLType()}")
if (defaultValue != null) {
sink.writeUtf8(" = ")
if (defaultValue is String) {
if (!legacyDefaultValues && defaultValue is String) {
// defaultValue is already encoded as GraphQL, we can pass it verbatim
sink.writeUtf8(defaultValue)
} else {
Expand All @@ -149,23 +162,23 @@ private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}

private fun IntrospectionSchema.Type.Interface.toSDL(sink: BufferedSink) {
private fun IntrospectionSchema.Type.Interface.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
sink.writeDescription(description)
sink.writeUtf8("interface $name {\n")
fields?.forEach {
it.toSDL(sink)
it.toSDL(sink, legacyDefaultValue)
sink.writeUtf8("\n")
}
sink.writeUtf8("}\n")
}

private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink) {
private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
sink.writeDescription(description, " ")
sink.writeUtf8(" $name")
if (args.isNotEmpty()) {
sink.writeUtf8("(")
args.forEachIndexed { index, arg ->
arg.toSDL(sink)
arg.toSDL(sink, legacyDefaultValue)
if (index != args.size - 1) {
sink.writeUtf8(", ")
}
Expand All @@ -176,15 +189,15 @@ private fun IntrospectionSchema.Field.toSDL(sink: BufferedSink) {
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}

private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink, legacyDefaultValue: Boolean) {
if (!description.isNullOrBlank()) {
// Write the description inline
sink.writeUtf8("\"\"\"$description\"\"\" ")
}
sink.writeUtf8("$name: ${type.asGraphQLType()}")
if (defaultValue != null) {
sink.writeUtf8(" = ")
if (defaultValue is String) {
if (!legacyDefaultValue && defaultValue is String) {
// defaultValue is already encoded as GraphQL, we can pass it verbatim
sink.writeUtf8(defaultValue)
} else {
Expand All @@ -195,7 +208,7 @@ private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}

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

fields?.forEach {
it.toSDL(sink)
it.toSDL(sink, legacyDefaultValue)
sink.writeUtf8("\n")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class GraphSdlParseTest() {
val sdlFile = File("build/sdl-test/schema.sdl")
sdlFile.parentFile.deleteRecursively()
sdlFile.parentFile.mkdirs()
initialSchema.toSDL(sdlFile)
initialSchema.toSDL(sdlFile, legacyDefaultValues = false)

assertEquals(File("src/test/sdl/default-values.sdl").readText(), sdlFile.readText())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ abstract class ApolloDownloadSchemaCliTask : DefaultTask() {
} else {
if (sdlSchema == null) {
val buffer = Buffer()
IntrospectionSchema(introspectionSchema!!.byteInputStream()).toSDL(buffer)
IntrospectionSchema(introspectionSchema!!.byteInputStream()).toSDL(buffer, legacyDefaultValues = false)
sdlSchema = buffer.readString(Charset.defaultCharset())
}
schema.writeText(sdlSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class ApolloDownloadSchemaTask : DefaultTask() {
if (schema.extension == "json") {
schema.writeText(introspection)
} else {
IntrospectionSchema(Buffer().writeUtf8(introspection).inputStream()).toSDL(schema)
IntrospectionSchema(Buffer().writeUtf8(introspection).inputStream()).toSDL(schema, legacyDefaultValues = false)
}
}

Expand Down

0 comments on commit 3ac95ec

Please sign in to comment.