{"id":13536676,"url":"https://github.com/yshrsmz/kgql","last_synced_at":"2025-03-22T05:31:07.012Z","repository":{"id":34379834,"uuid":"162102812","full_name":"yshrsmz/kgql","owner":"yshrsmz","description":"GraphQL Document wrapper generator for Kotlin Multiplatform Project and Android","archived":false,"fork":false,"pushed_at":"2022-09-09T09:22:40.000Z","size":820,"stargazers_count":57,"open_issues_count":13,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T13:11:13.851Z","etag":null,"topics":["android","android-library","code-generator","gradle-plugin","graphql","kotlin","kotlin-library","kotlin-multiplatform","kotlin-native","kotlinpoet","multiplatform"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yshrsmz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"yshrsmz","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-12-17T09:03:25.000Z","updated_at":"2024-08-01T20:26:02.000Z","dependencies_parsed_at":"2022-09-14T22:53:28.657Z","dependency_job_id":null,"html_url":"https://github.com/yshrsmz/kgql","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yshrsmz%2Fkgql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yshrsmz%2Fkgql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yshrsmz%2Fkgql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yshrsmz%2Fkgql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yshrsmz","download_url":"https://codeload.github.com/yshrsmz/kgql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244181289,"owners_count":20411605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","code-generator","gradle-plugin","graphql","kotlin","kotlin-library","kotlin-multiplatform","kotlin-native","kotlinpoet","multiplatform"],"created_at":"2024-08-01T09:00:47.135Z","updated_at":"2025-03-22T05:31:03.014Z","avatar_url":"https://github.com/yshrsmz.png","language":"Kotlin","funding_links":["https://github.com/sponsors/yshrsmz"],"categories":["Libraries"],"sub_categories":["Network","🌎 Network"],"readme":"kgql\n===\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.codingfeline.kgql/gradle-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.codingfeline.kgql/gradle-plugin)\n\nGraphQL Document wrapper generator for Kotlin Multiplatform Project.  \nCurrently, available for JVM/Android/iOS\n\n## core\n\nkgql core classes\n\n## kgql-gradle-plugin\n\nkgql Gradle Plugin generates wrapper classes for provided GraphQL document files.\n\n### Setup\n\nkgql requires Gradle __7.0 or later__\n\nSupported GraphQL file extension: `.gql` or `.graphql`\n\n#### For Android Project\n\n```gradle\nbuildscript {\n    repositories {\n        mavenCentral()\n        google()\n        jcenter()\n    }\n    dependencies {\n        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'\n        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.3.2'\n        classpath 'com.codingfeline.kgql:gradle-plugin:0.9.0'\n    }\n}\n\napply plugin: 'com.android.application'\napply plugin: 'kotlin-android'\napply plugin: 'kotlinx-serialization'\napply plugin: 'com.codingfeline.kgql'\n\nrepositories {\n     mavenCentral()\n}\n\nkgql {\n    packageName = \"com.sample\"\n    sourceSet = files(\"src/main/kgql\")\n    typeMapper = [\n        // mapper for non-scalar type\n        \"UserProfile\": \"com.sample.data.UserProfile\"\n    ]\n}\n```\n\n#### For Kotlin Multiplatform Project\n\n```gradle\nbuildscript {\n    repositories {\n        mavenCentral()\n        google()\n        jcenter()\n    }\n    dependencies {\n        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'\n        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.3.2'\n        classpath 'com.codingfeline.kgql:gradle-plugin:0.9.0'\n    }\n}\n\napply plugin: 'kotlin-multiplatform'\napply plugin: 'kotlinx-serialization'\napply plugin: 'com.codingfeline.kgql'\n\nrepositories {\n     mavenCentral()\n}\n\nkotlin {\n    // kotlin configurations...\n}\n\nkgql {\n    packageName = \"com.sample\"\n    sourceSet = files(\"src/main/kgql\")\n    typeMapper = [\n        // mapper for non-scalar type\n        \"UserProfile\": \"com.sample.data.UserProfile\"\n    ]\n}\n```\n\n#### How to generate wrapper classes\n\nWhen you apply kgql plugin, `generateKgqlInterface` task is added to the project. Manually executing it is one way, but\nthe task is integrated into project's build task, so it will be generated upon each build.\n\n## [WIP]kgql-ktor\n\nktor extensions for kgql\n\n## How it works\n\n```\n# viewer.gql\nquery {\n  viewer {\n    login\n  }\n}\n```\n\nBelow code will be generated from above GraphQL document file(viewer.gql).\n\n```kotlin\npackage com.sample\n\nimport com.codingfeline.kgql.core.KgqlRequestBody\nimport kotlin.String\nimport kotlin.Unit\nimport kotlinx.serialization.KSerializer\nimport kotlinx.serialization.Optional\nimport kotlinx.serialization.SerialName\nimport kotlinx.serialization.Serializable\n\nobject ViewerDocument {\n    private val document: String = \"\"\"\n            |query {\n            |  viewer {\n            |    login\n            |  }\n            |}\n            |\"\"\".trimMargin()\n\n    object Query {\n        /**\n         * Create an instance of [Request] which then you can encode to JSON string\n         */\n        fun requestBody(): Request = Request()\n\n        fun serializer(): KSerializer\u003cRequest\u003e = Request.serializer()\n\n        @Serializable\n        data class Request(\n            @SerialName(value = \"variables\") @Optional override val variables: Unit? = null,\n            @Optional @SerialName(value = \"operationName\") override val operationName: String? =\n                null,\n            @SerialName(value = \"query\") override val query: String = document\n        ) : KgqlRequestBody\u003cUnit\u003e\n    }\n}\n```\n\nAs you can see, generated code utilizes data class's default value. So in order to properly serialize, you need to\nset `encodeDefaults` to true in your `kotlinx.serialization.json.Json` instance.\n\nYou can use this code with Ktor or any other HttpClient.\n\nExample usage with Ktor is below\n\n```kotlin\npackage com.sample\n\nimport com.codingfeline.kgql.core.KgqlResponse\nimport com.codingfeline.kgql.core.KgqlError\nimport com.sample.ViewerDocument\nimport io.ktor.client.HttpClient\nimport io.ktor.client.features.json.JsonFeature\nimport io.ktor.client.request.headers\nimport io.ktor.client.request.post\nimport io.ktor.http.ContentType\nimport io.ktor.http.content.TextContent\nimport io.ktor.http.Url\nimport kotlinx.serialization.json.JSON\nimport kotlinx.serialization.Serializable\n\nconst val TOKEN = \"YOUR_GITHUB_TOKEN\"\n\n@Serializable\ndata class ViewerWrapper(\n    val viewer: Viewer\n)\n\n@Serializable\ndata class Viewer(\n    val login: String\n)\n\n@Serializable\ndata class ViewerResponse(\n    override val data: ViewerWrapper?,\n    override val errors: List\u003cKgqlError\u003e?\n) : KgqlResponse\u003cViewerWrapper\u003e\n\n\nclass GitHubApi {\n\n    private val json = Json {\n        // encodeDefaults must be set to true\n        encodeDefaults = true\n    }\n\n    private val client = HttpClient {\n        install(JsonFeature) {\n            this.serializer = KotlinxSerializer(json = json)\n        }\n    }\n\n    suspend fun fetchLogin(): Viewer? {\n\n        val body = json.encodeToString(ViewerDocument.Query.serializer(), ViewerDocument.Query.requestBody())\n        val response = client.post\u003cString\u003e(url = Url(\"https://api.github.com/graphql\")) {\n            body = TextContent(text = body, contentType = ContentType.Application.Json)\n\n            headers {\n                append(\"Authorization\", \"bearer $TOKEN\")\n            }\n        }\n\n        val res = JSON.parse(ViewerResponse.serializer(), response)\n\n        return res.data?.viewer\n    }\n}\n\n```\n\n## Try out the sample\n\nHave a look at `./sample` directory.\n\n```\n# Try out the samples.\n# BuildKonfig will be generated in ./sample/build/kgql\n$ ./gradlew -p sample generateKgqlInterface\n```\n\n### Try sample with snapshot\n\n```\n# Try out the samples.\n# BuildKonfig will be generated in ./sample/build/kgql\n$ ./gradlew clean build installArchives\n$ ./gradlew -p sample generateKgqlInterface\n```\n\n## Credits\n\nThis library is highly inspired by [squareup/sqldelight](https://github.com/squareup/sqldelight) and the gradle plugin\nand basic idea is heavily based on it. Thanks for this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyshrsmz%2Fkgql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyshrsmz%2Fkgql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyshrsmz%2Fkgql/lists"}