Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-2.x] fix converting introspection to SDL #4316

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ private fun IntrospectionSchema.InputField.toSDL(sink: BufferedSink) {
sink.writeUtf8(" $name: ${type.asGraphQLType()}")
if (defaultValue != null) {
sink.writeUtf8(" = ")
sink.writeValue(defaultValue)
if (defaultValue is String) {
// defaultValue is already encoded as GraphQL, we can pass it verbatim
sink.writeUtf8(defaultValue)
} else {
// legacy mode if we bump into an introspection schema that doesn't encode the default value
sink.writeValue(defaultValue)
}

}
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}
Expand Down Expand Up @@ -177,7 +184,13 @@ private fun IntrospectionSchema.Field.Argument.toSDL(sink: BufferedSink) {
sink.writeUtf8("$name: ${type.asGraphQLType()}")
if (defaultValue != null) {
sink.writeUtf8(" = ")
sink.writeValue(defaultValue)
if (defaultValue is String) {
// defaultValue is already encoded as GraphQL, we can pass it verbatim
sink.writeUtf8(defaultValue)
} else {
// legacy mode if we bump into an introspection schema that doesn't encode the default value
sink.writeValue(defaultValue)
}
}
sink.writeDeprecatedDirective(isDeprecated, deprecationReason)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.apollographql.apollo.compiler.parser.introspection.IntrospectionSchem
import com.apollographql.apollo.compiler.parser.introspection.toSDL
import com.apollographql.apollo.compiler.parser.sdl.GraphSdlSchema
import com.apollographql.apollo.compiler.parser.sdl.toIntrospectionSchema
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
Expand Down Expand Up @@ -84,6 +83,17 @@ class GraphSdlParseTest() {
assertEquals(initialSchema, finalSchema)
}

@Test
fun `defaultValues are correctly written`() {
val initialSchema = IntrospectionSchema(File("src/test/sdl/default-values.json")).normalize()
val sdlFile = File("build/sdl-test/schema.sdl")
sdlFile.parentFile.deleteRecursively()
sdlFile.parentFile.mkdirs()
initialSchema.toSDL(sdlFile)

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

/**
* use to make easier diffs
*/
Expand Down
Loading