Skip to content

Commit 584ca9f

Browse files
authored
Test scalar converters with Ulocale (ExpediaGroup#1259)
* Test scalar converters with Ulocale * Update wiremock schema * Add fields to types
1 parent 3ea1075 commit 584ca9f

File tree

15 files changed

+197
-34
lines changed

15 files changed

+197
-34
lines changed

examples/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ subprojects {
5151
implementation(kotlin("stdlib", kotlinVersion))
5252
implementation(kotlin("reflect", kotlinVersion))
5353
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlinCoroutinesVersion")
54+
implementation("com.ibm.icu:icu4j:69.1")
5455
testImplementation(kotlin("test-junit5", kotlinVersion))
5556
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
5657
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

examples/client/gradle-client/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ graphql {
3131
// optional
3232
allowDeprecatedFields = true
3333
headers = mapOf("X-Custom-Header" to "My-Custom-Header")
34-
customScalars = listOf(GraphQLScalar("UUID", "java.util.UUID", "com.expediagroup.graphql.examples.client.gradle.UUIDScalarConverter"))
34+
customScalars = listOf(
35+
GraphQLScalar("UUID", "java.util.UUID", "com.expediagroup.graphql.examples.client.gradle.UUIDScalarConverter"),
36+
GraphQLScalar("Locale", "com.ibm.icu.util.ULocale", "com.expediagroup.graphql.examples.client.gradle.ULocaleScalarConverter"),
37+
)
3538
serializer = GraphQLSerializer.KOTLINX
3639
}
3740
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.examples.client.gradle
18+
19+
import com.expediagroup.graphql.client.converter.ScalarConverter
20+
import com.ibm.icu.util.ULocale
21+
22+
/**
23+
* Public client converter for a [ULocale]
24+
*/
25+
class ULocaleScalarConverter : ScalarConverter<ULocale> {
26+
override fun toScalar(rawValue: Any): ULocale = ULocale(rawValue.toString())
27+
override fun toJson(value: ULocale): Any = value.toString()
28+
}

examples/client/gradle-client/src/main/resources/ExampleQuery.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ query ExampleQuery($simpleCriteria: SimpleArgumentInput) {
88
name
99
rating
1010
valid
11+
locale
12+
listLocale
1113
}
1214
listQuery {
1315
id

examples/client/maven-client/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<artifactId>reactor-core</artifactId>
3131
<version>${reactor.version}</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>com.ibm.icu</groupId>
35+
<artifactId>icu4j</artifactId>
36+
<version>69.1</version>
37+
</dependency>
3338
</dependencies>
3439

3540
<build>
@@ -79,6 +84,15 @@
7984
used to convert to/from raw JSON and scalar type -->
8085
<converter>com.expediagroup.graphql.examples.client.maven.UUIDScalarConverter</converter>
8186
</customScalar>
87+
<customScalar>
88+
<!-- custom scalar UUID type -->
89+
<scalar>Locale</scalar>
90+
<!-- fully qualified Java class name of a custom scalar type -->
91+
<type>com.ibm.icu.util.ULocale</type>
92+
<!-- fully qualified Java class name of a custom com.expediagroup.graphql.client.converter.ScalarConverter
93+
used to convert to/from raw JSON and scalar type -->
94+
<converter>com.expediagroup.graphql.examples.client.maven.ULocaleScalarConverter</converter>
95+
</customScalar>
8296
</customScalars>
8397
<serializer>JACKSON</serializer>
8498
<useOptionalInputWrapper>true</useOptionalInputWrapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.examples.client.maven
18+
19+
import com.expediagroup.graphql.client.converter.ScalarConverter
20+
import com.ibm.icu.util.ULocale
21+
22+
/**
23+
* Public client converter for a [ULocale]
24+
*/
25+
class ULocaleScalarConverter : ScalarConverter<ULocale> {
26+
override fun toScalar(rawValue: Any): ULocale = ULocale(rawValue.toString())
27+
override fun toJson(value: ULocale): Any = value.toString()
28+
}

examples/client/maven-client/src/main/resources/ExampleQuery.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ query ExampleQuery($simpleCriteria: SimpleArgumentInput) {
88
name
99
rating
1010
valid
11+
locale
12+
listLocale
1113
}
1214
listQuery {
1315
id

examples/client/server/src/main/kotlin/com/expediagroup/graphql/examples/client/server/Application.kt

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package com.expediagroup.graphql.examples.client.server
1818

19+
import com.expediagroup.graphql.examples.client.server.scalars.graphqlULocaleType
20+
import com.expediagroup.graphql.examples.client.server.scalars.graphqlUUIDType
1921
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
20-
import graphql.language.StringValue
21-
import graphql.schema.Coercing
22-
import graphql.schema.CoercingParseLiteralException
23-
import graphql.schema.CoercingParseValueException
24-
import graphql.schema.GraphQLScalarType
22+
import com.ibm.icu.util.ULocale
2523
import graphql.schema.GraphQLType
2624
import org.springframework.boot.autoconfigure.SpringBootApplication
2725
import org.springframework.boot.runApplication
@@ -32,33 +30,11 @@ import kotlin.reflect.KType
3230
@SpringBootApplication
3331
class Application {
3432

35-
private val graphqlUUIDType = GraphQLScalarType.newScalar()
36-
.name("UUID")
37-
.description("Custom scalar representing UUID")
38-
.coercing(object : Coercing<UUID, String> {
39-
override fun parseValue(input: Any): UUID = try {
40-
UUID.fromString(
41-
serialize(input)
42-
)
43-
} catch (e: Exception) {
44-
throw CoercingParseValueException("Cannot parse value $input to UUID", e)
45-
}
46-
47-
override fun parseLiteral(input: Any): UUID = try {
48-
val uuidString = (input as? StringValue)?.value
49-
UUID.fromString(uuidString)
50-
} catch (e: Exception) {
51-
throw CoercingParseLiteralException("Cannot parse literal $input to UUID", e)
52-
}
53-
54-
override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
55-
})
56-
.build()
57-
5833
@Bean
5934
fun customHooks(): SchemaGeneratorHooks = object : SchemaGeneratorHooks {
6035
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
6136
UUID::class -> graphqlUUIDType
37+
ULocale::class -> graphqlULocaleType
6238
else -> null
6339
}
6440
}

examples/client/server/src/main/kotlin/com/expediagroup/graphql/examples/client/server/SimpleQueries.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.expediagroup.graphql.examples.client.server.repository.BasicObjectRep
3131
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
3232
import com.expediagroup.graphql.generator.scalars.ID
3333
import com.expediagroup.graphql.server.operations.Query
34+
import com.ibm.icu.util.ULocale
3435
import org.springframework.stereotype.Component
3536
import java.util.UUID
3637
import kotlin.random.Random
@@ -54,7 +55,9 @@ class SimpleQueries(private val repository: BasicObjectRepository) : Query {
5455
count = 1,
5556
rating = null,
5657
custom = UUID.randomUUID(),
57-
customList = listOf(UUID.randomUUID(), UUID.randomUUID())
58+
customList = listOf(UUID.randomUUID(), UUID.randomUUID()),
59+
locale = ULocale.US,
60+
listLocale = listOf(ULocale.US, ULocale.FRANCE)
5861
)
5962

6063
@GraphQLDescription("Query returning list of simple objects")

examples/client/server/src/main/kotlin/com/expediagroup/graphql/examples/client/server/model/ScalarWrapper.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.expediagroup.graphql.examples.client.server.model
1818

1919
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
2020
import com.expediagroup.graphql.generator.scalars.ID
21+
import com.ibm.icu.util.ULocale
2122
import java.util.UUID
2223

2324
@GraphQLDescription("Wrapper that holds all supported scalar types")
@@ -32,8 +33,12 @@ data class ScalarWrapper(
3233
val count: Int?,
3334
@GraphQLDescription("A nullable signed double-precision floating-point value")
3435
val rating: Float?,
35-
@GraphQLDescription("Custom scalar")
36+
@GraphQLDescription("Custom scalar of UUID")
3637
val custom: UUID,
37-
@GraphQLDescription("List of custom scalars")
38-
val customList: List<UUID>
38+
@GraphQLDescription("List of custom scalar UUIDs")
39+
val customList: List<UUID>,
40+
@GraphQLDescription("Custom scalar of Locale")
41+
val locale: ULocale,
42+
@GraphQLDescription("List of custom scalar Locales")
43+
val listLocale: List<ULocale>
3944
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.examples.client.server.scalars
18+
19+
import graphql.language.StringValue
20+
import graphql.schema.Coercing
21+
import graphql.schema.CoercingParseLiteralException
22+
import graphql.schema.CoercingParseValueException
23+
import graphql.schema.GraphQLScalarType
24+
25+
internal val graphqlULocaleType = GraphQLScalarType.newScalar()
26+
.name("Locale")
27+
.description("A type representing a Locale such as en_US or fr_FR")
28+
.coercing(ULocaleCoercing)
29+
.build()
30+
31+
// We coerce between <String, String> because jackson will
32+
// take care of ser/deser for us within SchemaGenerator
33+
private object ULocaleCoercing : Coercing<String, String> {
34+
override fun parseValue(input: Any): String = input as? String ?: throw CoercingParseValueException("$input can not be cast to String")
35+
36+
override fun parseLiteral(input: Any): String = (input as? StringValue)?.value ?: throw CoercingParseLiteralException("$input can not be cast to StringValue")
37+
38+
override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.examples.client.server.scalars
18+
19+
import graphql.language.StringValue
20+
import graphql.schema.Coercing
21+
import graphql.schema.CoercingParseLiteralException
22+
import graphql.schema.CoercingParseValueException
23+
import graphql.schema.GraphQLScalarType
24+
import java.util.UUID
25+
26+
internal val graphqlUUIDType = GraphQLScalarType.newScalar()
27+
.name("UUID")
28+
.description("Custom scalar representing UUID")
29+
.coercing(object : Coercing<UUID, String> {
30+
override fun parseValue(input: Any): UUID = try {
31+
UUID.fromString(
32+
serialize(input)
33+
)
34+
} catch (e: Exception) {
35+
throw CoercingParseValueException("Cannot parse value $input to UUID", e)
36+
}
37+
38+
override fun parseLiteral(input: Any): UUID = try {
39+
val uuidString = (input as? StringValue)?.value
40+
UUID.fromString(uuidString)
41+
} catch (e: Exception) {
42+
throw CoercingParseLiteralException("Cannot parse literal $input to UUID", e)
43+
}
44+
45+
override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
46+
})
47+
.build()

examples/client/src/integration/wiremock/__files/schema.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ type ScalarWrapper {
147147
rating: Float
148148
"Either true or false"
149149
valid: Boolean!
150+
"Custom scalar of Locale"
151+
locale: Locale!
152+
"List of custom scalar Locales"
153+
listLocale: [Locale!]!
150154
}
151155

152156
"Example interface implementation where value is a float"
@@ -172,6 +176,9 @@ enum CustomEnum {
172176
"Custom scalar representing UUID"
173177
scalar UUID
174178

179+
"A type representing a Locale such as en_US or fr_FR"
180+
scalar Locale
181+
175182
"Some basic description"
176183
input BasicObjectInput {
177184
id: Int!

plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/exceptions/InvalidSelectionSetException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ package com.expediagroup.graphql.plugin.client.generator.exceptions
2020
* Exception thrown when specified query file contains invalid selection set.
2121
*/
2222
internal class InvalidSelectionSetException(operationName: String, typeDefinitionName: String, typeName: String) :
23-
RuntimeException("Operation $operationName specifies invalid selection set for $typeName - cannot select empty $typeDefinitionName")
23+
RuntimeException("Operation $operationName specifies invalid selection set for $typeName - cannot find field '$typeDefinitionName'")

plugins/client/graphql-kotlin-client-generator/src/test/resources/testSchema.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ type ScalarWrapper {
127127
rating: Float
128128
"Either true or false"
129129
valid: Boolean!
130+
"Custom scalar of Locale"
131+
locale: Locale!
132+
"List of custom scalar Locales"
133+
listLocale: [Locale!]!
130134
}
131135
"Example interface implementation where value is a float"
132136
type SecondInterfaceImplementation implements BasicInterface {
@@ -160,6 +164,10 @@ enum OtherEnum {
160164
}
161165
"Custom scalar representing UUID"
162166
scalar UUID
167+
168+
"A type representing a Locale such as en_US or fr_FR"
169+
scalar Locale
170+
163171
"Test input object"
164172
input SimpleArgumentInput {
165173
"Maximum value for test criteria"

0 commit comments

Comments
 (0)