Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ subprojects {
implementation(kotlin("stdlib", kotlinVersion))
implementation(kotlin("reflect", kotlinVersion))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlinCoroutinesVersion")
implementation("com.ibm.icu:icu4j:69.1")
testImplementation(kotlin("test-junit5", kotlinVersion))
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
Expand Down
5 changes: 4 additions & 1 deletion examples/client/gradle-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ graphql {
// optional
allowDeprecatedFields = true
headers = mapOf("X-Custom-Header" to "My-Custom-Header")
customScalars = listOf(GraphQLScalar("UUID", "java.util.UUID", "com.expediagroup.graphql.examples.client.gradle.UUIDScalarConverter"))
customScalars = listOf(
GraphQLScalar("UUID", "java.util.UUID", "com.expediagroup.graphql.examples.client.gradle.UUIDScalarConverter"),
GraphQLScalar("Locale", "com.ibm.icu.util.ULocale", "com.expediagroup.graphql.examples.client.gradle.ULocaleScalarConverter"),
)
serializer = GraphQLSerializer.KOTLINX
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.client.gradle

import com.expediagroup.graphql.client.converter.ScalarConverter
import com.ibm.icu.util.ULocale

/**
* Public client converter for a [ULocale]
*/
class ULocaleScalarConverter : ScalarConverter<ULocale> {
override fun toScalar(rawValue: Any): ULocale = ULocale(rawValue.toString())
override fun toJson(value: ULocale): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ query ExampleQuery($simpleCriteria: SimpleArgumentInput) {
name
rating
valid
locale
listLocale
}
listQuery {
id
Expand Down
14 changes: 14 additions & 0 deletions examples/client/maven-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<artifactId>reactor-core</artifactId>
<version>${reactor.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>69.1</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -79,6 +84,15 @@
used to convert to/from raw JSON and scalar type -->
<converter>com.expediagroup.graphql.examples.client.maven.UUIDScalarConverter</converter>
</customScalar>
<customScalar>
<!-- custom scalar UUID type -->
<scalar>Locale</scalar>
<!-- fully qualified Java class name of a custom scalar type -->
<type>com.ibm.icu.util.ULocale</type>
<!-- fully qualified Java class name of a custom com.expediagroup.graphql.client.converter.ScalarConverter
used to convert to/from raw JSON and scalar type -->
<converter>com.expediagroup.graphql.examples.client.maven.ULocaleScalarConverter</converter>
</customScalar>
</customScalars>
<serializer>JACKSON</serializer>
<useOptionalInputWrapper>true</useOptionalInputWrapper>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.client.maven

import com.expediagroup.graphql.client.converter.ScalarConverter
import com.ibm.icu.util.ULocale

/**
* Public client converter for a [ULocale]
*/
class ULocaleScalarConverter : ScalarConverter<ULocale> {
override fun toScalar(rawValue: Any): ULocale = ULocale(rawValue.toString())
override fun toJson(value: ULocale): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ query ExampleQuery($simpleCriteria: SimpleArgumentInput) {
name
rating
valid
locale
listLocale
}
listQuery {
id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

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

import com.expediagroup.graphql.examples.client.server.scalars.graphqlULocaleType
import com.expediagroup.graphql.examples.client.server.scalars.graphqlUUIDType
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.GraphQLScalarType
import com.ibm.icu.util.ULocale
import graphql.schema.GraphQLType
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
Expand All @@ -32,33 +30,11 @@ import kotlin.reflect.KType
@SpringBootApplication
class Application {

private val graphqlUUIDType = GraphQLScalarType.newScalar()
.name("UUID")
.description("Custom scalar representing UUID")
.coercing(object : Coercing<UUID, String> {
override fun parseValue(input: Any): UUID = try {
UUID.fromString(
serialize(input)
)
} catch (e: Exception) {
throw CoercingParseValueException("Cannot parse value $input to UUID", e)
}

override fun parseLiteral(input: Any): UUID = try {
val uuidString = (input as? StringValue)?.value
UUID.fromString(uuidString)
} catch (e: Exception) {
throw CoercingParseLiteralException("Cannot parse literal $input to UUID", e)
}

override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
})
.build()

@Bean
fun customHooks(): SchemaGeneratorHooks = object : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
UUID::class -> graphqlUUIDType
ULocale::class -> graphqlULocaleType
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.expediagroup.graphql.examples.client.server.repository.BasicObjectRep
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import com.expediagroup.graphql.generator.scalars.ID
import com.expediagroup.graphql.server.operations.Query
import com.ibm.icu.util.ULocale
import org.springframework.stereotype.Component
import java.util.UUID
import kotlin.random.Random
Expand All @@ -54,7 +55,9 @@ class SimpleQueries(private val repository: BasicObjectRepository) : Query {
count = 1,
rating = null,
custom = UUID.randomUUID(),
customList = listOf(UUID.randomUUID(), UUID.randomUUID())
customList = listOf(UUID.randomUUID(), UUID.randomUUID()),
locale = ULocale.US,
listLocale = listOf(ULocale.US, ULocale.FRANCE)
)

@GraphQLDescription("Query returning list of simple objects")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.expediagroup.graphql.examples.client.server.model

import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import com.expediagroup.graphql.generator.scalars.ID
import com.ibm.icu.util.ULocale
import java.util.UUID

@GraphQLDescription("Wrapper that holds all supported scalar types")
Expand All @@ -32,8 +33,12 @@ data class ScalarWrapper(
val count: Int?,
@GraphQLDescription("A nullable signed double-precision floating-point value")
val rating: Float?,
@GraphQLDescription("Custom scalar")
@GraphQLDescription("Custom scalar of UUID")
val custom: UUID,
@GraphQLDescription("List of custom scalars")
val customList: List<UUID>
@GraphQLDescription("List of custom scalar UUIDs")
val customList: List<UUID>,
@GraphQLDescription("Custom scalar of Locale")
val locale: ULocale,
@GraphQLDescription("List of custom scalar Locales")
val listLocale: List<ULocale>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2021 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.client.server.scalars

import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.GraphQLScalarType

internal val graphqlULocaleType = GraphQLScalarType.newScalar()
.name("Locale")
.description("A type representing a Locale such as en_US or fr_FR")
.coercing(ULocaleCoercing)
.build()

// We coerce between <String, String> because jackson will
// take care of ser/deser for us within SchemaGenerator
private object ULocaleCoercing : Coercing<String, String> {
override fun parseValue(input: Any): String = input as? String ?: throw CoercingParseValueException("$input can not be cast to String")

override fun parseLiteral(input: Any): String = (input as? StringValue)?.value ?: throw CoercingParseLiteralException("$input can not be cast to StringValue")

override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.client.server.scalars

import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.GraphQLScalarType
import java.util.UUID

internal val graphqlUUIDType = GraphQLScalarType.newScalar()
.name("UUID")
.description("Custom scalar representing UUID")
.coercing(object : Coercing<UUID, String> {
override fun parseValue(input: Any): UUID = try {
UUID.fromString(
serialize(input)
)
} catch (e: Exception) {
throw CoercingParseValueException("Cannot parse value $input to UUID", e)
}

override fun parseLiteral(input: Any): UUID = try {
val uuidString = (input as? StringValue)?.value
UUID.fromString(uuidString)
} catch (e: Exception) {
throw CoercingParseLiteralException("Cannot parse literal $input to UUID", e)
}

override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
})
.build()
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ type ScalarWrapper {
rating: Float
"Either true or false"
valid: Boolean!
"Custom scalar of Locale"
locale: Locale!
"List of custom scalar Locales"
listLocale: [Locale!]!
}

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

"A type representing a Locale such as en_US or fr_FR"
scalar Locale

"Some basic description"
input BasicObjectInput {
id: Int!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package com.expediagroup.graphql.plugin.client.generator.exceptions
* Exception thrown when specified query file contains invalid selection set.
*/
internal class InvalidSelectionSetException(operationName: String, typeDefinitionName: String, typeName: String) :
RuntimeException("Operation $operationName specifies invalid selection set for $typeName - cannot select empty $typeDefinitionName")
RuntimeException("Operation $operationName specifies invalid selection set for $typeName - cannot find field '$typeDefinitionName'")
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type ScalarWrapper {
rating: Float
"Either true or false"
valid: Boolean!
"Custom scalar of Locale"
locale: Locale!
"List of custom scalar Locales"
listLocale: [Locale!]!
}
"Example interface implementation where value is a float"
type SecondInterfaceImplementation implements BasicInterface {
Expand Down Expand Up @@ -160,6 +164,10 @@ enum OtherEnum {
}
"Custom scalar representing UUID"
scalar UUID

"A type representing a Locale such as en_US or fr_FR"
scalar Locale

"Test input object"
input SimpleArgumentInput {
"Maximum value for test criteria"
Expand Down