# Installation
Below is an overall list of steps which we'd take to integrate suparnatural-graphql in a Kotlin Multiplatform project.
- Configure kotlinx.serialization
- Apply the
graphql-plugin. - Configure the plugin with options like
GraphQLserverendpointto fetchschema. - Add common and platform specific
graphqldependencies to the project. - Add
rxruntime provider.
# Kotlinx Serialization
To parse JSON, this library depends on kotlinx.serialization. Follow this guide to configure it.
# Apply Gradle Plugin
Configure buildscript section of your build.gradle.kts or build.gradle with following.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.suparnatural.kotlin:graphql-plugin:1.0.12")
}
}
apply(plugin="com.suparnatural.plugins.graphql")
2
3
4
5
6
7
8
9
10
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.suparnatural.kotlin:graphql-plugin:1.0.0"
}
}
apply plugin: "com.suparnatural.kotlin.graphql"
2
3
4
5
6
7
8
9
10
# Configure Gradle Plugin
Add the following to your build.gradle.kts or build.gradle file
suparnaturalGraphQl {
packageName = "com.myapp.graphql.models"
endpointUrl = "https://countries.trevorblades.com"
documentsPath = "operations/*.gql"
outputDirectoryPath = "src/main/kotlin"
headers = listOf("Authorization: abc")
}
2
3
4
5
6
7
suparnaturalGraphQl {
packageName = "com.myapp.graphql.models"
endpointUrl = "https://countries.trevorblades.com"
documentsPath = "operations/*.gql"
outputDirectoryPath = "src/main/kotlin"
headers = ["Authorization: abc"]
}
2
3
4
5
6
7
| Property | Description |
|---|---|
packageName | Generated type files will have this as the package name |
endpointUrl | GraphQL endpoint URL to pull schema from. This takes precedence over localSchemaFilePath |
localSchemaFilePath | Path to local schema file if endpointUrl is not used |
documentsPath | Glob pattern to the directory which contains graphql query files |
outputDirectoryPath | Generated types will be placed under this directory |
headers | Additional headers to be passed while downloading schema |
# Add Library
Add the maven repository to repositories block.
repositories {
mavenCentral()
}
2
3
repositories {
mavenCentral()
}
2
3
Add the following to your commonMain target.
commonMain {
dependencies {
implementation("com.suparnatural.kotlin:graphql:version")
}
}
2
3
4
5
6
commonMain {
dependencies {
implementation "com.suparnatural.kotlin:graphql:version"
}
}
2
3
4
5
With the hierarchical project structure, you generally need to add the dependency to
commonMainonly. Other targets are also available in case you need to override this behavior.
| Platform | Depdendency |
|---|---|
| Common | suparnatural-kotlin-multiplatform:graphql-metadata:version |
| Android-Debug | suparnatural-kotlin-multiplatform:graphql-android-debug:version |
| Android-Release | suparnatural-kotlin-multiplatform:graphql-android:version |
| iOS-Arm64 | suparnatural-kotlin-multiplatform:graphql-iosarm64:version |
| iOS-X64 | suparnatural-kotlin-multiplatform:graphql-iosx64:version |
| JVM | suparnatural-kotlin-multiplatform:graphql-jvm:version |
Finally, run the gradle task graphQlCodeGen under group suparnatural.
# RX Runtime
The response from GraphQL is exposed as an Observable and therefore, the library needs an RX runtime. The RxRuntimeProvider class provides the RX runtime to the rest of the library. A minimal RX runtime is provided by suparnatural:rx-runtime-reaktive package which uses Reaktive internally. Add the following code to build.gradle to include the required repository.
repositories {
maven(url="https://dl.bintray.com/badoo/maven")
}
2
3
4
5
repositories {
maven {
url "https://dl.bintray.com/badoo/maven"
}
}
2
3
4
5
Next, configure each source set with the appropriate dependency
| Platform | Depdendency |
|---|---|
| Common | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-metadata:version |
| Android-Debug | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-android-debug:version |
| Android-Release | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-android:version |
| iOS-Arm64 | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-iosarm64:version |
| iOS-X64 | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-iosx64:version |
| JVM | suparnatural-kotlin-multiplatform:rx-runtime-reaktive-jvm:version |
Next, initialize the runtime in your application
fun main() {
RxRuntimeProvider.observableFactory = ReaktiveObservableFactory()
RxRuntimeProvider.publishSubjectFactory = ReaktivePublishSubjectFactory()
}
2
3
4
# Custom RX Runtime
If you are already using an rx library or you want more than rx-runtime-reaktive, you can also provide your custom runtime. Add the rx definition as a dependency so that you can access the right interfaces. As before, pick the right dependency for the target and add it to the source set.
commonMain {
dependencies {
implementation("com.suparnatural.kotlin:rx:version")
}
}
2
3
4
5
commonMain {
dependencies {
implementation "com.suparnatural.kotlin:rx:version"
}
}
2
3
4
5
| Platform | Depdendency |
|---|---|
| Common | suparnatural-kotlin-multiplatform:rx-metadata:version |
| Android-Debug | suparnatural-kotlin-multiplatform:rx-android-debug:version |
| Android-Release | suparnatural-kotlin-multiplatform:rx-android:version |
| iOS-Arm64 | suparnatural-kotlin-multiplatform:rx-iosarm64:version |
| iOS-X64 | suparnatural-kotlin-multiplatform:rx-iosx64:version |
| JVM | suparnatural-kotlin-multiplatform:rx-jvm:version |
Next, initialize the runtime in your application
fun main() {
RxRuntimeProvider.observableFactory = CustomObservableFactory()
RxRuntimeProvider.publishSubjectFactory = CustomPublishSubjectFactory()
}
2
3
4
← Introduction Overview →