GraphQLClient is a thin wrapper on top of Ktor HTTP Client and supports fully
asynchronous non-blocking communication. It is highly customizable and can be configured with any supported Ktor HTTP
engine and features.
GraphQL Kotlin provides both Gradle and Maven plugins to automatically generate your client code at build time. Once
your data classes are generated, you can then execute their underlying GraphQL operations using
build.gradle.kts Gradle configuration:
pom.xml Maven configuration
See graphql-kotlin-client-example project for complete working examples of Gradle and Maven based projects.
By default, GraphQL Kotlin build plugins will attempt to generate GraphQL clients from all
*.graphql files located under
src/main/resources. Queries are validated against the target GraphQL schema, which can be manually provided, retrieved by
the plugins through introspection (as configured in examples above) or downloaded directly from a custom SDL endpoint.
See our documentation for more details on supported Gradle tasks
and Maven Mojos.
When creating your GraphQL queries make sure to always specify an operation name and name the files accordingly. Each
one of your query files will generate a corresponding Kotlin file with a class matching your operation
name that will act as a wrapper for all corresponding data classes. For example, given
HelloWorldQuery as the operation name, GraphQL Kotlin plugins will generate a corresponding
HelloWorldQuery class under the configured package.
For example, given a simple schema
And a corresponding
Plugins will generate following client code
Generated classes requires an instance of
GraphQLClient and exposes a single
execute suspendable method that executes
the underlying GraphQL operation using the provided client.
Your auto generated classes accept an instance of
GraphQLClient which is a thin wrapper around Ktor HTTP client that
ensures proper serialization and deserialization of your GraphQL objects.
GraphQLClient requires target URL to be
specified and defaults to fully asynchronous non-blocking Coroutine-based IO engine.