Skip to content

Commit a0c80f8

Browse files
authored
example client project (#716)
1 parent 631c74d commit a0c80f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1513
-2
lines changed

docs/client/client-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Equivalent `pom.xml` Maven configuration
9292
</project>
9393
```
9494

95-
See [graphql-kotlin-client-example](https://github.com/dariuszkuc/graphql-kotlin-client-example) project for complete
95+
See [graphql-kotlin-client-example](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/client) project for complete
9696
working examples of Gradle and Maven based projects.
9797

9898
## Generating GraphQL Client

examples/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
Example apps that use `graphql-kotlin` libraries to test and demonstrate usages.
44

5-
* [spring](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/spring) - This is a sample Spring Boot app that uses `graphql-kotlin-spring-server` to create a reactive GraphQL web application. Please refer to the README file for details on how to run the application.
5+
* [client](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/client) - Example GraphQL Kotlin Client projects using Gradle and Maven together with a simple server to run against.
66
* [federation](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/federation) - Example Spring Boot apps generating federated GraphQL schemas and an Apollo Gateway in NodeJS that exposes the merged federated schema. Please refer to the README files for details on how to run each federated service.
7+
* [spark](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/spark) - Example GraphQL server using Spark framework
8+
* [spring](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/spring) - This is a sample Spring Boot app that uses `graphql-kotlin-spring-server` to create a reactive GraphQL web application. Please refer to the README file for details on how to run the application.

examples/client/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# GraphQL Kotlin Client Example
2+
3+
Example GraphQL Kotlin client projects using Gradle and Maven together with a simple server to run against.
4+
5+
See documentation in individual modules for details how to run them
6+
7+
* [gradle-client](gradle-client)
8+
* [maven-client](maven-client)
9+
* [server](server)
10+
11+
> NOTE: Client examples are not part of the `graphql-kotlin-examples` project as they rely on the server to start up
12+
before clients can be build.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Example usage GraphQL Kotlin Client using Gradle
2+
3+
This project is a simple application that use [GraphQL Kotlin Gradle plugin](https://expediagroup.github.io/graphql-kotlin/docs/plugins/gradle-plugin)
4+
to auto-generate GraphQL client and then use it to communicate with the target GraphQL server. See [documentation](https://expediagroup.github.io/graphql-kotlin/)
5+
for details.
6+
7+
## Building locally
8+
9+
This project uses Gradle and you can build it locally using
10+
11+
```shell script
12+
gradle clean build
13+
```
14+
15+
## Running locally
16+
17+
* [only works after project is build] Run `Application.kt` directly from your IDE
18+
* Alternatively you can also use the Gradle application plugin by running `gradle run` from the command line
19+
20+
Application will then attempt to execute few queries and mutations against a target GraphQL server and print out the results.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import com.expediagroup.graphql.plugin.generator.ScalarConverterMapping
2+
import com.expediagroup.graphql.plugin.gradle.graphql
3+
4+
plugins {
5+
application
6+
id("org.jetbrains.kotlin.jvm") version "1.3.72"
7+
// id("com.expediagroup.graphql") version "3.0.0-SNAPSHOT"
8+
}
9+
10+
repositories {
11+
mavenLocal()
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation("com.expediagroup:graphql-kotlin-client:3.0.0-SNAPSHOT")
17+
implementation("io.ktor:ktor-client-okhttp:1.3.1")
18+
implementation("io.ktor:ktor-client-logging-jvm:1.3.1")
19+
}
20+
21+
// TODO remove this section after final release
22+
buildscript {
23+
repositories {
24+
mavenLocal()
25+
}
26+
27+
dependencies{
28+
classpath("com.expediagroup:graphql-kotlin-gradle-plugin:3.0.0-SNAPSHOT")
29+
}
30+
}
31+
32+
apply(plugin = "com.expediagroup.graphql")
33+
34+
application {
35+
mainClassName = "com.expediagroup.graphql.examples.client.ApplicationKt"
36+
}
37+
38+
graphql {
39+
packageName = "com.expediagroup.graphql.generated"
40+
// you can also use direct sdlEndpoint instead
41+
endpoint = "http://localhost:8080/graphql"
42+
43+
// optional
44+
allowDeprecatedFields = true
45+
converters.put("UUID", ScalarConverterMapping("java.util.UUID", "com.expediagroup.graphql.examples.client.UUIDScalarConverter"))
46+
}
47+
48+
tasks {
49+
compileKotlin {
50+
kotlinOptions {
51+
jvmTarget = "1.8"
52+
freeCompilerArgs = listOf("-Xjsr305=strict")
53+
54+
}
55+
}
56+
}
57+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.expediagroup.graphql.examples.client
2+
3+
import com.expediagroup.graphql.client.GraphQLClient
4+
import com.expediagroup.graphql.generated.AddObjectMutation
5+
import com.expediagroup.graphql.generated.HelloWorldQuery
6+
import com.expediagroup.graphql.generated.RetrieveObjectQuery
7+
import com.expediagroup.graphql.generated.UpdateObjectMutation
8+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
9+
import io.ktor.client.engine.okhttp.OkHttp
10+
import io.ktor.client.features.logging.DEFAULT
11+
import io.ktor.client.features.logging.LogLevel
12+
import io.ktor.client.features.logging.Logger
13+
import io.ktor.client.features.logging.Logging
14+
import kotlinx.coroutines.runBlocking
15+
import java.net.URL
16+
import java.util.concurrent.TimeUnit
17+
18+
fun main() {
19+
val jackson = jacksonObjectMapper()
20+
val client = GraphQLClient(
21+
url = URL("http://localhost:8080/graphql"),
22+
engineFactory = OkHttp,
23+
mapper = jackson
24+
) {
25+
engine {
26+
config {
27+
connectTimeout(10, TimeUnit.SECONDS)
28+
readTimeout(60, TimeUnit.SECONDS)
29+
writeTimeout(60, TimeUnit.SECONDS)
30+
}
31+
}
32+
install(Logging) {
33+
logger = Logger.DEFAULT
34+
level = LogLevel.HEADERS
35+
}
36+
}
37+
val helloWorldQuery = HelloWorldQuery(client)
38+
println("HelloWorld examples")
39+
runBlocking {
40+
val helloWorldResultNoParam = helloWorldQuery.execute(variables = HelloWorldQuery.Variables(name = null))
41+
println("\tquery without parameters result: ${helloWorldResultNoParam.data?.helloWorld}")
42+
43+
val helloWorldResult = helloWorldQuery.execute(variables = HelloWorldQuery.Variables(name = "Dariusz"))
44+
println("\tquery with parameters result: ${helloWorldResult.data?.helloWorld}")
45+
}
46+
47+
// mutation examples
48+
println("simple mutation examples")
49+
val retrieveObjectQuery = RetrieveObjectQuery(client)
50+
val addObjectMutation = AddObjectMutation(client)
51+
val updateObjectMutation = UpdateObjectMutation(client)
52+
runBlocking {
53+
val retrieveNonExistentObject = retrieveObjectQuery.execute(variables = RetrieveObjectQuery.Variables(id = 1))
54+
println("\tretrieve non existent object: ${retrieveNonExistentObject.data?.retrieveBasicObject}")
55+
56+
val addResult = addObjectMutation.execute(variables = AddObjectMutation.Variables(newObject = AddObjectMutation.BasicObjectInput(1, "first")))
57+
println("\tadd new object: ${addResult.data?.addBasicObject}")
58+
59+
val updateResult = updateObjectMutation.execute(variables = UpdateObjectMutation.Variables(updatedObject = UpdateObjectMutation.BasicObjectInput(1, "updated")))
60+
println("\tupdate new object: ${updateResult.data?.updateBasicObject}")
61+
}
62+
client.close()
63+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.expediagroup.graphql.examples.client
2+
3+
import com.expediagroup.graphql.client.converter.ScalarConverter
4+
import java.util.UUID
5+
6+
class UUIDScalarConverter : ScalarConverter<UUID> {
7+
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
8+
override fun toJson(value: UUID): String = value.toString()
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mutation AddObjectMutation($newObject: BasicObjectInput!) {
2+
addBasicObject(newObject: $newObject) {
3+
id
4+
name
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mutation DeleteObjectMutation($id: Int!) {
2+
deleteBasicObject(id: $id) {
3+
id
4+
name
5+
}
6+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
query ExampleQuery($simpleCriteria: SimpleArgumentInput) {
2+
enumQuery
3+
scalarQuery {
4+
count
5+
custom
6+
id
7+
name
8+
rating
9+
valid
10+
}
11+
listQuery {
12+
__typename
13+
id
14+
name
15+
}
16+
complexObjectQuery {
17+
__typename
18+
id
19+
name
20+
optional
21+
details {
22+
id
23+
flag
24+
value
25+
}
26+
}
27+
interfaceQuery {
28+
__typename
29+
id
30+
name
31+
... on FirstInterfaceImplementation {
32+
intValue
33+
}
34+
... on SecondInterfaceImplementation {
35+
floatValue
36+
}
37+
}
38+
unionQuery {
39+
__typename
40+
... on BasicObject {
41+
id
42+
name
43+
}
44+
... on ComplexObject {
45+
id
46+
name
47+
optional
48+
details {
49+
id
50+
flag
51+
value
52+
}
53+
}
54+
}
55+
inputObjectQuery(criteria: $simpleCriteria)
56+
}

0 commit comments

Comments
 (0)