@@ -2,23 +2,26 @@ package com.expedia.graphql.schema.generator
22
33import com.expedia.graphql.TopLevelObjectDef
44import com.expedia.graphql.annotations.GraphQLDescription
5+ import com.expedia.graphql.annotations.GraphQLID
56import com.expedia.graphql.annotations.GraphQLIgnore
67import com.expedia.graphql.schema.exceptions.ConflictingTypesException
78import com.expedia.graphql.schema.exceptions.InvalidSchemaException
89import com.expedia.graphql.schema.extensions.deepName
910import com.expedia.graphql.schema.testSchemaConfig
1011import com.expedia.graphql.toSchema
1112import graphql.GraphQL
13+ import graphql.schema.GraphQLNonNull
1214import graphql.schema.GraphQLObjectType
1315import org.junit.jupiter.api.Assertions.assertNull
1416import org.junit.jupiter.api.Assertions.assertThrows
1517import java.net.CookieManager
18+ import java.util.UUID
1619import kotlin.test.Test
1720import kotlin.test.assertEquals
1821import kotlin.test.assertNotNull
1922import kotlin.test.assertTrue
2023
21- @Suppress(" Detekt.UnusedPrivateMember" , " Detekt.FunctionOnlyReturningConstant" )
24+ @Suppress(" Detekt.UnusedPrivateMember" , " Detekt.FunctionOnlyReturningConstant" , " Detekt.LargeClass " )
2225class SchemaGeneratorTest {
2326 @Test
2427 fun `SchemaGenerator generates a simple GraphQL schema` () {
@@ -228,6 +231,26 @@ class SchemaGeneratorTest {
228231 assertNull(firstChild?.get(" children" ))
229232 }
230233
234+ @Test
235+ fun `SchemaGenerator support GraphQLID scalar` () {
236+ val schema = toSchema(queries = listOf (TopLevelObjectDef (QueryWithId ())), config = testSchemaConfig)
237+
238+ val placeType = schema.getObjectType(" PlaceOfIds" )
239+ assertEquals(graphql.Scalars .GraphQLID , (placeType.getFieldDefinition(" intId" ).type as ? GraphQLNonNull )?.wrappedType)
240+ assertEquals(graphql.Scalars .GraphQLID , (placeType.getFieldDefinition(" longId" ).type as ? GraphQLNonNull )?.wrappedType)
241+ assertEquals(graphql.Scalars .GraphQLID , (placeType.getFieldDefinition(" stringId" ).type as ? GraphQLNonNull )?.wrappedType)
242+ assertEquals(graphql.Scalars .GraphQLID , (placeType.getFieldDefinition(" uuid" ).type as ? GraphQLNonNull )?.wrappedType)
243+ }
244+
245+ @Test
246+ fun `SchemaGenerator throws an exception for invalid GraphQLID` () {
247+ val exception = assertThrows(IllegalArgumentException ::class .java) {
248+ toSchema(queries = listOf (TopLevelObjectDef (QueryWithInvalidId ())), config = testSchemaConfig)
249+ }
250+
251+ assertEquals(" Person is not a valid ID type, only [kotlin.Int, kotlin.String, kotlin.Long, java.util.UUID] are accepted" , exception.message)
252+ }
253+
231254 class QueryObject {
232255 @GraphQLDescription(" A GraphQL query method" )
233256 fun query (@GraphQLDescription(" A GraphQL value" ) value : Int ): Geography = Geography (value, GeoType .CITY , listOf ())
@@ -355,4 +378,21 @@ class SchemaGeneratorTest {
355378 }
356379
357380 data class Person (val name : String , val children : List <Person >? = null )
381+
382+ data class PlaceOfIds (
383+ @property:GraphQLID val intId : Int ,
384+ @property:GraphQLID val longId : Long ,
385+ @property:GraphQLID val stringId : String ,
386+ @property:GraphQLID val uuid : UUID
387+ )
388+
389+ data class InvalidIds (@property:GraphQLID val person : Person )
390+
391+ class QueryWithId {
392+ fun query (): PlaceOfIds = PlaceOfIds (42 , 24 , " 42" , UUID .randomUUID())
393+ }
394+
395+ class QueryWithInvalidId {
396+ fun query (): InvalidIds = InvalidIds (Person (" person id not a valid type id" ))
397+ }
358398}
0 commit comments