Skip to content

Commit

Permalink
fix cannot import from Insomnia data dumps if the dump contains a Gra…
Browse files Browse the repository at this point in the history
…phQL request with an empty body
  • Loading branch information
sunny-chung committed Dec 4, 2023
1 parent e40eab8 commit a977426
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.sunnychung.application.multiplatform.hellohttp.model.UserKeyValuePair
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestTemplate
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestExample
import com.sunnychung.application.multiplatform.hellohttp.model.insomniav4.InsomniaV4
import com.sunnychung.application.multiplatform.hellohttp.util.emptyToNull
import com.sunnychung.application.multiplatform.hellohttp.util.log
import com.sunnychung.application.multiplatform.hellohttp.util.replaceIf
import com.sunnychung.application.multiplatform.hellohttp.util.uuidString
Expand Down Expand Up @@ -209,16 +210,23 @@ class InsomniaV4Importer {
"application/octet-stream" -> FileBody(
filePath = it.body.fileName
)
"application/graphql" -> jsonParser.readValue<GraphqlRequestBody>(it.body.text ?: "")
.let {
GraphqlBody(
document = it.query,
variables = jsonParser.writerWithDefaultPrettyPrinter()
.writeValueAsString(it.variables)
.let { if (it == "null") "" else it },
operationName = it.operationName,
)
"application/graphql" -> it.body.text.emptyToNull()?.let { body ->
try {
jsonParser.readValue<GraphqlRequestBody>(body)
.let {
GraphqlBody(
document = it.query,
variables = jsonParser.writerWithDefaultPrettyPrinter()
.writeValueAsString(it.variables)
.let { if (it == "null") "" else it },
operationName = it.operationName,
)
}
} catch (e: Throwable) {
log.w(e) { "Insomnia import error while parsing JSON of GraphQL content. Will use default empty value." }
null
}
} ?: GraphqlBody(document = "", variables = "", operationName = null)
else -> StringBody(it.body.text?.convertVariables(postFlightBodyVariables) ?: "")
},
headers = it.parseHeaders(postFlightBodyVariables),
Expand Down

0 comments on commit a977426

Please sign in to comment.