Skip to content

Instantly share code, notes, and snippets.

@martinbonnin
Created March 7, 2023 13:39
Show Gist options
  • Select an option

  • Save martinbonnin/e3d11a10a7768f30f53d3ec71f673fba to your computer and use it in GitHub Desktop.

Select an option

Save martinbonnin/e3d11a10a7768f30f53d3ec71f673fba to your computer and use it in GitHub Desktop.
#!/usr/bin/env kotlin
@file:DependsOn("net.mbonnin.bare-graphql:bare-graphql:0.0.2")
@file:DependsOn("com.squareup.okhttp3:logging-interceptor:4.10.0")
import net.mbonnin.bare.graphql.toJsonElement
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.logging.HttpLoggingInterceptor
import okio.Buffer
val operationName = "GetSessions2"
val query = """
query $operationName {
sessions {
nodes {
title
}
}
}
""".trimIndent()
val extensions = mapOf(
"persistedQuery" to mapOf(
"version" to 1,
"sha256Hash" to Buffer().writeUtf8(query).readByteString().sha256().hex()
)
)
val variables = emptyMap<String, Any>()
val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
val url = "https://confetti-app.dev/graphql"
//val url = "http://localhost:8080/graphql"
fun hashedRequest(): Request {
return Request.Builder()
.get()
.url(url.toHttpUrl().newBuilder()
.addQueryParameter("operationName", operationName)
.addQueryParameter("extensions", extensions.toJsonElement().toString())
.build())
.build()
}
fun fullRequest(): Request {
return Request.Builder()
.post(
mapOf(
"query" to query,
"extensions" to extensions,
"variables" to variables,
"operationName" to operationName
).toJsonElement()
.toString()
.toRequestBody("application/json".toMediaType())
)
.url(url)
.build()
}
val request = if (args[0] == "hashed") {
hashedRequest()
} else {
fullRequest()
}
val response = request
.let {
client.newCall(it).execute()
}
response.isSuccessful
response.body?.string()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment