com.apollographql.apollo.gradle.internal.SchemaDownloader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-gradle-plugin Show documentation
Show all versions of apollo-gradle-plugin Show documentation
Gradle plugin for generating java/kotlin classes for graphql files
package com.apollographql.apollo.gradle.internal
import com.squareup.moshi.JsonWriter
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.buffer
import okio.sink
import java.io.ByteArrayOutputStream
import java.io.File
import java.util.concurrent.TimeUnit
object SchemaDownloader {
fun download(
endpoint: String,
schema: File,
headers: Map,
readTimeoutSeconds: Long,
connectTimeoutSeconds: Long
) {
val byteArrayOutputStream = ByteArrayOutputStream()
JsonWriter.of(byteArrayOutputStream.sink().buffer())
.apply {
beginObject()
name("query")
value(introspectionQuery)
endObject()
flush()
}
val body = byteArrayOutputStream.toByteArray().toRequestBody("application/json".toMediaTypeOrNull())
val request = Request.Builder()
.post(body)
.apply {
headers.entries.forEach {
addHeader(it.key, it.value)
}
}
.url(endpoint)
.build()
val response = OkHttpClient.Builder()
.connectTimeout(connectTimeoutSeconds, TimeUnit.SECONDS)
.readTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
.build()
.newCall(request).execute()
if (!response.isSuccessful) {
throw Exception("cannot get schema: ${response.code}:\n${response.body?.string()}")
}
schema.parentFile?.mkdirs()
schema.writeText(response.body!!.string())
}
val introspectionQuery = """
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}""".trimIndent()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy