diff --git a/core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt b/core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt index 94ca0c5..fcd699b 100644 --- a/core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt +++ b/core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt @@ -27,7 +27,7 @@ internal abstract class GraphQLNode { @Suppress("UNCHECKED_CAST") private fun convertToDataEntry(value: Any?) = when(value) { - is String -> DataEntry.StringData(value) + is String -> DataEntry.StringData(value.escapeQuotes()) is Int -> DataEntry.NonDecimalNumberData(value.toLong()) is Long -> DataEntry.NonDecimalNumberData(value) is Float -> DataEntry.DecimalNumberData(value.toDouble()) @@ -42,6 +42,10 @@ internal abstract class GraphQLNode { } } +internal fun String.escapeQuotes() = + this.replace("\\s+".toRegex(), " ") + .replace("\"", "\\\\\\\"") + internal fun String.wrappedWithQuotes(shouldBeEscaped: Boolean) = if (shouldBeEscaped) { "\"$this\"" diff --git a/core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt b/core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt index 844512d..a33aebf 100644 --- a/core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt +++ b/core/src/test/kotlin/me/lazmaid/kraph/test/BuilderSpek.kt @@ -322,6 +322,21 @@ class BuilderSpek : Spek({ } } } + + given("sample mutation with unescaped characters") { + val query = Kraph { + mutation { + field("someField", + args = mapOf( + "foo" to "some \"bar\" over" + ) + ) + } + } + it("should escape those characters") { + assertThat(query.toRequestString(), equalTo("{\"query\": \"mutation { someField (foo: \\\"some \\\\\\\"bar\\\\\\\" over\\\") }\", \"variables\": null, \"operationName\": null}")) + } + } } })