Skip to content

Instantly share code, notes, and snippets.

@yshrsmz
Last active January 16, 2019 04:05
Show Gist options
  • Save yshrsmz/4c8112dcfc00b195bef016ef27a6cd7d to your computer and use it in GitHub Desktop.
Save yshrsmz/4c8112dcfc00b195bef016ef27a6cd7d to your computer and use it in GitHub Desktop.
kgql - GraphQL Wrapper generator for Kotlin MPP, https://github.com/yshrsmz/kgql
query User(
$login: String!,
$name: Int,
$id: String = "",
$company: String = null,
$foo: Float,
$logins: [String]) {
user(login: $login) {
id
login
bio
avatarUrl
company
createdAt
}
}
mutation withArbitraryType($user: UserProfile) {
user(login: $login) {
id
login
bio
avatarUrl
company
createdAt
}
}
package com.sample
import com.codingfeline.kgql.core.KgqlRequestBody
import com.sample.data.UserProfile
import kotlin.Float
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
object QueryWithParamDocumentWrapper {
private val document: String = """
|query User(
| ${'$'}login: String!,
| ${'$'}name: Int,
| ${'$'}id: String = "",
| ${'$'}company: String = null,
| ${'$'}foo: Float,
| ${'$'}logins: [String]) {
| user(login: ${'$'}login) {
| id
| login
| bio
| avatarUrl
| company
| createdAt
| }
|}
|
|mutation withArbitraryType(${'$'}user: UserProfile) {
| user(login: ${'$'}login) {
| id
| login
| bio
| avatarUrl
| company
| createdAt
| }
|}
|""".trimMargin()
object UserQuery {
fun query(variables: Variables): KgqlRequestBody<Variables> =
KgqlRequestBody<Variables>(operationName="User", query=document,
variables=variables)
fun serializer(): KSerializer<KgqlRequestBody<Variables>> =
KgqlRequestBody.serializer(com.sample.QueryWithParamDocumentWrapper.UserQuery.Variables.serializer())
@Serializable
data class Variables(
val login: String,
val name: Int?,
val id: String?,
val company: String?,
val foo: Float?,
val logins: List<String?>?
)
}
object WithArbitraryTypeMutation {
fun mutation(variables: Variables): KgqlRequestBody<Variables> =
KgqlRequestBody<Variables>(operationName="withArbitraryType", query=document,
variables=variables)
fun serializer(): KSerializer<KgqlRequestBody<Variables>> =
KgqlRequestBody.serializer(com.sample.QueryWithParamDocumentWrapper.WithArbitraryTypeMutation.Variables.serializer())
@Serializable
data class Variables(val user: UserProfile?)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment