All Projects → yshrsmz → kgql

yshrsmz / kgql

Licence: Apache-2.0 license
GraphQL Document wrapper generator for Kotlin Multiplatform Project and Android

Programming Languages

kotlin
9241 projects
shell
77523 projects

Projects that are alternatives of or similar to kgql

kmm-production-sample
This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you can download it from the App Store and Google Play. It's been designed to demonstrate how KMM can be used in real production projects.
Stars: ✭ 1,476 (+2633.33%)
Mutual labels:  multiplatform, kotlin-native, kotlin-multiplatform
moko-network
Network components with codegeneration of rest api for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 107 (+98.15%)
Mutual labels:  gradle-plugin, kotlin-native, kotlin-multiplatform
tmdb-api
This Kotlin Multiplatform library is for accessing the TMDB API to get movie and TV show content. Using for Android, iOS, and JS projects.
Stars: ✭ 31 (-42.59%)
Mutual labels:  multiplatform, kotlin-native, kotlin-multiplatform
StarWars
Minimal GraphQL based Jetpack Compose, Wear Compose and SwiftUI Kotlin Multiplatform sample (using StarWars endpoint - https://graphql.org/swapi-graphql)
Stars: ✭ 165 (+205.56%)
Mutual labels:  kotlin-native, kotlin-multiplatform
moko-geo
Geolocation access for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 41 (-24.07%)
Mutual labels:  kotlin-native, kotlin-multiplatform
KParser
Kotlin Multiplatform Arithmatic Parser
Stars: ✭ 32 (-40.74%)
Mutual labels:  kotlin-native, kotlin-multiplatform
Apollo Android
🤖  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
Stars: ✭ 2,949 (+5361.11%)
Mutual labels:  multiplatform, kotlin-multiplatform
MultiplatformPlayground
Kotlin Multiplatform project in Jetpack Compose & SwiftUI with shared ViewModel layer and File upload
Stars: ✭ 72 (+33.33%)
Mutual labels:  kotlin-native, kotlin-multiplatform
KotlinXcodeSync
Sync Kotlin files with an Xcode project
Stars: ✭ 25 (-53.7%)
Mutual labels:  kotlin-native, kotlin-multiplatform
KMQTT
Embeddable and standalone Kotlin Multiplatform MQTT broker
Stars: ✭ 56 (+3.7%)
Mutual labels:  kotlin-native, kotlin-multiplatform
devtools-library
Multiplatform, pluggable, extensible, and dynamic config library which supports YAML and JSON as a source.
Stars: ✭ 15 (-72.22%)
Mutual labels:  kotlin-native, kotlin-multiplatform
kotlinx-resources
Kotlin Multiplatform (KMP) library for reading resources in tests
Stars: ✭ 15 (-72.22%)
Mutual labels:  gradle-plugin, kotlin-multiplatform
poetimizely
Generate Kotlin type safe accessors for Optimizely experiments and features
Stars: ✭ 17 (-68.52%)
Mutual labels:  gradle-plugin, kotlinpoet
KotlinCompilerPluginExample
This is an example project that shows how to create a Kotlin Compiler Plugin. The plugin will print "Hello from" and the name of the file that is being compiled, as a compiler warning to the terminal log.
Stars: ✭ 43 (-20.37%)
Mutual labels:  gradle-plugin, kotlin-multiplatform
kmp-fatframework-cocoa
A Gradle plugin to generate and publish an iOs FatFramework or XCFramework on Kotlin Multiplatform projects.
Stars: ✭ 26 (-51.85%)
Mutual labels:  gradle-plugin, kotlin-multiplatform
libs.kmp.icerock.dev
Kotlin Multiplatform libraries list with info auto-fetch
Stars: ✭ 178 (+229.63%)
Mutual labels:  kotlin-native, kotlin-multiplatform
CompleteKotlin
Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms.
Stars: ✭ 236 (+337.04%)
Mutual labels:  kotlin-native, kotlin-multiplatform
Keemun
No description or website provided.
Stars: ✭ 13 (-75.93%)
Mutual labels:  multiplatform, kotlin-multiplatform
Aboutlibraries
AboutLibraries automatically detects all dependencies of a project and collects their information including the license. Optionally visualising it via the provided ui components.
Stars: ✭ 2,777 (+5042.59%)
Mutual labels:  multiplatform, kotlin-multiplatform
kfsm
Finite State Machine in Kotlin
Stars: ✭ 76 (+40.74%)
Mutual labels:  kotlin-native, kotlin-multiplatform

kgql

Maven Central

GraphQL Document wrapper generator for Kotlin Multiplatform Project.
Currently, available for JVM/Android/iOS

core

kgql core classes

kgql-gradle-plugin

kgql Gradle Plugin generates wrapper classes for provided GraphQL document files.

Setup

kgql requires Gradle 7.0 or later

Supported GraphQL file extension: .gql or .graphql

For Android Project

buildscript {
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.3.2'
        classpath 'com.codingfeline.kgql:gradle-plugin:0.9.0'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlinx-serialization'
apply plugin: 'com.codingfeline.kgql'

repositories {
     mavenCentral()
}

kgql {
    packageName = "com.sample"
    sourceSet = files("src/main/kgql")
    typeMapper = [
        // mapper for non-scalar type
        "UserProfile": "com.sample.data.UserProfile"
    ]
}

For Kotlin Multiplatform Project

buildscript {
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.3.2'
        classpath 'com.codingfeline.kgql:gradle-plugin:0.9.0'
    }
}

apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
apply plugin: 'com.codingfeline.kgql'

repositories {
     mavenCentral()
}

kotlin {
    // kotlin configurations...
}

kgql {
    packageName = "com.sample"
    sourceSet = files("src/main/kgql")
    typeMapper = [
        // mapper for non-scalar type
        "UserProfile": "com.sample.data.UserProfile"
    ]
}

How to generate wrapper classes

When you apply kgql plugin, generateKgqlInterface task is added to the project. Manually executing it is one way, but the task is integrated into project's build task, so it will be generated upon each build.

[WIP]kgql-ktor

ktor extensions for kgql

How it works

# viewer.gql
query {
  viewer {
    login
  }
}

Below code will be generated from above GraphQL document file(viewer.gql).

package com.sample

import com.codingfeline.kgql.core.KgqlRequestBody
import kotlin.String
import kotlin.Unit
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Optional
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

object ViewerDocument {
    private val document: String = """
            |query {
            |  viewer {
            |    login
            |  }
            |}
            |""".trimMargin()

    object Query {
        /**
         * Create an instance of [Request] which then you can encode to JSON string
         */
        fun requestBody(): Request = Request()

        fun serializer(): KSerializer<Request> = Request.serializer()

        @Serializable
        data class Request(
            @SerialName(value = "variables") @Optional override val variables: Unit? = null,
            @Optional @SerialName(value = "operationName") override val operationName: String? =
                null,
            @SerialName(value = "query") override val query: String = document
        ) : KgqlRequestBody<Unit>
    }
}

As you can see, generated code utilizes data class's default value. So in order to properly serialize, you need to set encodeDefaults to true in your kotlinx.serialization.json.Json instance.

You can use this code with Ktor or any other HttpClient.

Example usage with Ktor is below

package com.sample

import com.codingfeline.kgql.core.KgqlResponse
import com.codingfeline.kgql.core.KgqlError
import com.sample.ViewerDocument
import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.http.ContentType
import io.ktor.http.content.TextContent
import io.ktor.http.Url
import kotlinx.serialization.json.JSON
import kotlinx.serialization.Serializable

const val TOKEN = "YOUR_GITHUB_TOKEN"

@Serializable
data class ViewerWrapper(
    val viewer: Viewer
)

@Serializable
data class Viewer(
    val login: String
)

@Serializable
data class ViewerResponse(
    override val data: ViewerWrapper?,
    override val errors: List<KgqlError>?
) : KgqlResponse<ViewerWrapper>


class GitHubApi {

    private val json = Json {
        // encodeDefaults must be set to true
        encodeDefaults = true
    }

    private val client = HttpClient {
        install(JsonFeature) {
            this.serializer = KotlinxSerializer(json = json)
        }
    }

    suspend fun fetchLogin(): Viewer? {

        val body = json.encodeToString(ViewerDocument.Query.serializer(), ViewerDocument.Query.requestBody())
        val response = client.post<String>(url = Url("https://api.github.com/graphql")) {
            body = TextContent(text = body, contentType = ContentType.Application.Json)

            headers {
                append("Authorization", "bearer $TOKEN")
            }
        }

        val res = JSON.parse(ViewerResponse.serializer(), response)

        return res.data?.viewer
    }
}

Try out the sample

Have a look at ./sample directory.

# Try out the samples.
# BuildKonfig will be generated in ./sample/build/kgql
$ ./gradlew -p sample generateKgqlInterface

Try sample with snapshot

# Try out the samples.
# BuildKonfig will be generated in ./sample/build/kgql
$ ./gradlew clean build installArchives
$ ./gradlew -p sample generateKgqlInterface

Credits

This library is highly inspired by squareup/sqldelight and the gradle plugin and basic idea is heavily based on it. Thanks for this.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].