graphql-kotlin-spring-client
provides Spring WebClient based reference implementation of GraphQLClient
. GraphQLWebClient
is a thin wrapper on top of Spring WebClient that relies on Reactor Netty for fully asynchronous non-blocking communications.
GraphQLWebClient
uses the Spring WebClient to execute the underlying queries. Clients can be customized to use different GraphQL serializers and by providing customized instance of Spring WebClient.Builder
.
See Spring WebClient documentation for additional details.
Using a JVM dependency manager, link graphql-kotlin-spring-client
to your project.
With Maven:
<dependency> <groupId>com.expediagroup</groupId> <artifactId>graphql-kotlin-spring-client</artifactId> <version>${latestVersion}</version> </dependency>
With Gradle (example using kts):
implementation("com.expediagroup:graphql-kotlin-spring-client:$latestVersion")
Before we can auto-generate the data model for our GraphQL queries we first need to obtain a copy of a corresponding GraphQL schema. Do this by running one of the tasks supplied by the GraphQL Kotlin Gradle plugin through your CLI:
./gradlew graphqlIntrospectSchema --endpoint="http://localhost:8080/graphql"
This runs an introspection query against the target GraphQL server and saves the resulting schema in the schema.graphql
file under the build directory. GraphQL schemas can change over time, so you should configure this task to run as part of your build/release process.
// build.gradle.ktsimportcom.expediagroup.graphql.plugin.gradle.tasks.GraphQLIntrospectSchemaTaskval graphqlIntrospectSchema by tasks.getting(GraphQLIntrospectSchemaTask::class) { endpoint.set("http://localhost:8080/graphql") }
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 result in a generation of a corresponding Kotlin file with a class matching your operation name. Generated top level class will contain all your data classes. For example, given MyAwesomeGraphQLQuery.graphql
with myAwesomeQuery
as the operation name, GraphQL Kotlin plugins will generate a corresponding MyAwesomeGraphQLQuery.kt
file with a MyAwesomeQuery
class under the configured package.
Refer to our documentation for additional details and considerations while writing your GraphQL queries.
GraphQL Kotlin build plugins will auto-generate your data classes based on your queries and the underlying GraphQL schema. In order to generate your client you will need to specify the target package name, schema file, and queries. If the queries parameter is omitted, it will default to using *.graphql
files under your resources directory. In order to generate Spring WebClient based client implementation you also need to specify client type.
graphql-kotlin-spring-client
defaults to use Jackson
which is also the default serializer used by the build plugins. See our documentation for information on how to customize this behavior.
val graphqlGenerateClient by tasks.getting(GraphQLGenerateClientTask::class) { packageName.set("com.expediagroup.graphql.generated") // use schema file generated by the introspection task schemaFile.set(graphqlIntrospectSchema.outputFile) // make sure to run client generation after introspection task dependsOn("graphqlIntrospectSchema") }
Additional information about Gradle and Maven plugins as well as their respective tasks/mojos can be found in our documentation.
Your auto generated classes are simple POJOs that optionally accept variables (only if underlying operation uses variables) as a constructor parameter. GraphQLClient
exposes generic methods that allow you to execute either a single or batch request.
GraphQLWebClient
is a thin wrapper on top of Spring WebClient and can be customized by providing customized instance of Spring WebClient.Builder
.
val client =GraphQLWebClient(url ="http://localhost:8080/graphql") val query =MyAwesomeQuery() val result = client.execute(query)
The result of your query is a type safe object that corresponds to your GraphQL query.
Additional information about Gradle and Maven plugins as well as their respective tasks/mojos can be found in our documentation.
Additional information can be found in our documentation and the Javadocs of all published library versions.
If you have a question about something you can not find in our documentation or javadocs, feel free to start a new discussion.