@@ -7,24 +7,24 @@ import graphql.language.StringValue
77import graphql.schema.Coercing
88import graphql.schema.DataFetchingEnvironment
99import graphql.schema.GraphQLScalarType
10- import kotlinx.coroutines.*
10+ import kotlinx.coroutines.CompletableDeferred
1111import kotlinx.coroutines.channels.Channel
1212import kotlinx.coroutines.channels.ReceiveChannel
13+ import kotlinx.coroutines.coroutineScope
1314import org.reactivestreams.Publisher
14- import java.util.Optional
15- import java.util.UUID
15+ import java.util.*
1616import java.util.concurrent.CompletableFuture
1717
1818fun createSchema () = SchemaParser .newParser()
19- .schemaString(schemaDefinition)
20- .resolvers(Query (), Mutation (), Subscription (), ItemResolver (), UnusedRootResolver (), UnusedResolver ())
21- .scalars(customScalarUUID, customScalarMap, customScalarId)
22- .dictionary(" OtherItem" , OtherItemWithWrongName ::class )
23- .dictionary(" ThirdItem" , ThirdItem ::class )
24- .dictionary(" ComplexMapItem" , ComplexMapItem ::class )
25- .dictionary(" NestedComplexMapItem" , NestedComplexMapItem ::class )
26- .build()
27- .makeExecutableSchema()
19+ .schemaString(schemaDefinition)
20+ .resolvers(Query (), Mutation (), Subscription (), ItemResolver (), UnusedRootResolver (), UnusedResolver ())
21+ .scalars(customScalarUUID, customScalarMap, customScalarId)
22+ .dictionary(" OtherItem" , OtherItemWithWrongName ::class )
23+ .dictionary(" ThirdItem" , ThirdItem ::class )
24+ .dictionary(" ComplexMapItem" , ComplexMapItem ::class )
25+ .dictionary(" NestedComplexMapItem" , NestedComplexMapItem ::class )
26+ .build()
27+ .makeExecutableSchema()
2828
2929val schemaDefinition = """
3030
@@ -210,13 +210,13 @@ type ItemWithGenericProperties {
210210
211211
212212val items = mutableListOf (
213- Item (0 , " item1" , Type .TYPE_1 , UUID .fromString(" 38f685f1-b460-4a54-a17f-7fd69e8cf3f8" ), listOf (Tag (1 , " item1-tag1" ), Tag (2 , " item1-tag2" ))),
214- Item (1 , " item2" , Type .TYPE_2 , UUID .fromString(" 38f685f1-b460-4a54-b17f-7fd69e8cf3f8" ), listOf (Tag (3 , " item2-tag1" ), Tag (4 , " item2-tag2" )))
213+ Item (0 , " item1" , Type .TYPE_1 , UUID .fromString(" 38f685f1-b460-4a54-a17f-7fd69e8cf3f8" ), listOf (Tag (1 , " item1-tag1" ), Tag (2 , " item1-tag2" ))),
214+ Item (1 , " item2" , Type .TYPE_2 , UUID .fromString(" 38f685f1-b460-4a54-b17f-7fd69e8cf3f8" ), listOf (Tag (3 , " item2-tag1" ), Tag (4 , " item2-tag2" )))
215215)
216216
217217val otherItems = mutableListOf (
218- OtherItemWithWrongName (0 , " otherItem1" , Type .TYPE_1 , UUID .fromString(" 38f685f1-b460-4a54-c17f-7fd69e8cf3f8" )),
219- OtherItemWithWrongName (1 , " otherItem2" , Type .TYPE_2 , UUID .fromString(" 38f685f1-b460-4a54-d17f-7fd69e8cf3f8" ))
218+ OtherItemWithWrongName (0 , " otherItem1" , Type .TYPE_1 , UUID .fromString(" 38f685f1-b460-4a54-c17f-7fd69e8cf3f8" )),
219+ OtherItemWithWrongName (1 , " otherItem2" , Type .TYPE_2 , UUID .fromString(" 38f685f1-b460-4a54-d17f-7fd69e8cf3f8" ))
220220)
221221
222222val thirdItems = mutableListOf (
@@ -244,7 +244,7 @@ val propertyMapWithNestedComplexItems = mutableListOf(
244244 hashMapOf(" nested" to NestedComplexMapItem (UndiscoveredItem (63 )), " age" to 72 )
245245)
246246
247- class Query : GraphQLQueryResolver , ListListResolver <String >() {
247+ class Query : GraphQLQueryResolver , ListListResolver <String >() {
248248 fun isEmpty () = items.isEmpty()
249249 fun allBaseItems () = items
250250 fun items (input : ItemSearchInput ): List <Item > = items.filter { it.name == input.name }
@@ -257,8 +257,9 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
257257 fun itemByBuiltInId (id : UUID ): Item ? {
258258 return items.find { it.uuid == id }
259259 }
260- fun itemsWithOptionalInput (input : ItemSearchInput ? ) = if (input == null ) items else items(input)
261- fun itemsWithOptionalInputExplicit (input : Optional <ItemSearchInput >) = if (input.isPresent) items(input.get()) else items
260+
261+ fun itemsWithOptionalInput (input : ItemSearchInput ? ) = if (input == null ) items else items(input)
262+ fun itemsWithOptionalInputExplicit (input : Optional <ItemSearchInput >) = if (input.isPresent) items(input.get()) else items
262263 fun enumInputType (type : Type ) = type
263264 fun customScalarMapInputType (customScalarMap : Map <String , Any >) = customScalarMap
264265 fun itemWithGenericProperties () = ItemWithGenericProperties (listOf (" A" , " B" ))
@@ -269,7 +270,9 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
269270 fun futureItems () = CompletableFuture .completedFuture(items)
270271 fun complexNullableType (): ComplexNullable ? = null
271272
272- fun complexInputType (input : List <List <ComplexInputType >? >? ) = input?.firstOrNull()?.firstOrNull()?.let { it.first == " foo" && it.second?.firstOrNull()?.firstOrNull()?.first == " bar" } ? : false
273+ fun complexInputType (input : List <List <ComplexInputType >? >? ) = input?.firstOrNull()?.firstOrNull()?.let { it.first == " foo" && it.second?.firstOrNull()?.firstOrNull()?.first == " bar" }
274+ ? : false
275+
273276 fun extendedType () = ExtendedType ()
274277
275278 fun getItemsWithGetResolver () = items
@@ -286,16 +289,16 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
286289 private val propertyField = " test"
287290
288291 fun dataFetcherResult (): DataFetcherResult <Item > {
289- return DataFetcherResult ( items.first(), listOf () )
292+ return DataFetcherResult .newResult< Item >().data( items.first()).build( )
290293 }
291294
292295 suspend fun coroutineItems (): List <Item > = CompletableDeferred (items).await()
293296
294297 fun arrayItems () = items.toTypedArray()
295298}
296299
297- class UnusedRootResolver : GraphQLQueryResolver
298- class UnusedResolver : GraphQLResolver <String >
300+ class UnusedRootResolver : GraphQLQueryResolver
301+ class UnusedResolver : GraphQLResolver <String >
299302
300303class ExtendedType {
301304 fun first () = " test"
@@ -306,7 +309,7 @@ abstract class ListListResolver<out E> {
306309 fun listList (): List <List <E >> = listOf (listOf ())
307310}
308311
309- class Mutation : GraphQLMutationResolver {
312+ class Mutation : GraphQLMutationResolver {
310313 fun addItem (input : NewItemInput ): Item {
311314 return Item (items.size, input.name, input.type, UUID .randomUUID(), listOf ()) // Don't actually add the item to the list, since we want the test to be deterministic
312315 }
@@ -316,10 +319,10 @@ class OnItemCreatedContext(val newItem: Item)
316319
317320class Subscription : GraphQLSubscriptionResolver {
318321 fun onItemCreated (env : DataFetchingEnvironment ) =
319- Publisher <Item > { subscriber ->
320- subscriber.onNext(env.getContext<OnItemCreatedContext >().newItem)
322+ Publisher <Item > { subscriber ->
323+ subscriber.onNext(env.getContext<OnItemCreatedContext >().newItem)
321324// subscriber.onComplete()
322- }
325+ }
323326
324327 fun onItemCreatedCoroutineChannel (env : DataFetchingEnvironment ): ReceiveChannel <Item > {
325328 val channel = Channel <Item >(1 )
@@ -343,7 +346,11 @@ class ItemResolver : GraphQLResolver<Item> {
343346 fun batchedName (items : List <Item >) = items.map { it.name }
344347
345348 @Batched
346- fun batchedWithParamsTags (items : List <Item >, names : List <String >? ): List <List <Tag >> = items.map{ it.tags.filter { names?.contains(it.name) ? : true } }
349+ fun batchedWithParamsTags (items : List <Item >, names : List <String >? ): List <List <Tag >> = items.map {
350+ it.tags.filter {
351+ names?.contains(it.name) ? : true
352+ }
353+ }
347354}
348355
349356interface ItemInterface {
@@ -353,8 +360,8 @@ interface ItemInterface {
353360}
354361
355362enum class Type { TYPE_1 , TYPE_2 }
356- data class Item (val id : Int , override val name : String , override val type : Type , override val uuid : UUID , val tags : List <Tag >) : ItemInterface
357- data class OtherItemWithWrongName (val id : Int , override val name : String , override val type : Type , override val uuid : UUID ) : ItemInterface
363+ data class Item (val id : Int , override val name : String , override val type : Type , override val uuid : UUID , val tags : List <Tag >) : ItemInterface
364+ data class OtherItemWithWrongName (val id : Int , override val name : String , override val type : Type , override val uuid : UUID ) : ItemInterface
358365data class ThirdItem (val id : Int )
359366data class ComplexMapItem (val id : Int )
360367data class UndiscoveredItem (val id : Int )
@@ -367,44 +374,56 @@ data class ComplexInputType(val first: String, val second: List<List<ComplexInpu
367374data class ComplexInputTypeTwo (val first : String )
368375data class ItemWithGenericProperties (val keys : List <String >)
369376
370- val customScalarId = GraphQLScalarType (" ID" , " Overrides built-in ID" , object : Coercing <UUID , String > {
371- override fun serialize (input : Any ): String? = when (input) {
372- is String -> input
373- is UUID -> input.toString()
374- else -> null
375- }
376-
377- override fun parseValue (input : Any ): UUID ? = parseLiteral(input)
378-
379- override fun parseLiteral (input : Any ): UUID ? = when (input) {
380- is StringValue -> UUID .fromString(input.value)
381- else -> null
382- }
383- })
384-
385- val customScalarUUID = GraphQLScalarType (" UUID" , " UUID" , object : Coercing <UUID , String > {
386-
387- override fun serialize (input : Any ): String? = when (input) {
388- is String -> input
389- is UUID -> input.toString()
390- else -> null
391- }
392-
393- override fun parseValue (input : Any ): UUID ? = parseLiteral(input)
394-
395- override fun parseLiteral (input : Any ): UUID ? = when (input) {
396- is StringValue -> UUID .fromString(input.value)
397- else -> null
398- }
399- })
400-
401- val customScalarMap = GraphQLScalarType (" customScalarMap" , " customScalarMap" , object : Coercing <Map <String , Any >, Map <String , Any >> {
402-
403- @Suppress(" UNCHECKED_CAST" )
404- override fun parseValue (input : Any? ): Map <String , Any > = input as Map <String , Any >
405-
406- @Suppress(" UNCHECKED_CAST" )
407- override fun serialize (dataFetcherResult : Any? ): Map <String , Any > = dataFetcherResult as Map <String , Any >
408-
409- override fun parseLiteral (input : Any? ): Map <String , Any > = (input as ObjectValue ).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue ).value }
410- })
377+ val customScalarId = GraphQLScalarType .newScalar()
378+ .name(" ID" )
379+ .description(" Overrides built-in ID" )
380+ .coercing(object : Coercing <UUID , String > {
381+ override fun serialize (input : Any ): String? = when (input) {
382+ is String -> input
383+ is UUID -> input.toString()
384+ else -> null
385+ }
386+
387+ override fun parseValue (input : Any ): UUID ? = parseLiteral(input)
388+
389+ override fun parseLiteral (input : Any ): UUID ? = when (input) {
390+ is StringValue -> UUID .fromString(input.value)
391+ else -> null
392+ }
393+ })
394+ .build()
395+
396+ val customScalarUUID = GraphQLScalarType .newScalar()
397+ .name(" UUID" )
398+ .description(" UUID" )
399+ .coercing(object : Coercing <UUID , String > {
400+
401+ override fun serialize (input : Any ): String? = when (input) {
402+ is String -> input
403+ is UUID -> input.toString()
404+ else -> null
405+ }
406+
407+ override fun parseValue (input : Any ): UUID ? = parseLiteral(input)
408+
409+ override fun parseLiteral (input : Any ): UUID ? = when (input) {
410+ is StringValue -> UUID .fromString(input.value)
411+ else -> null
412+ }
413+ })
414+ .build()
415+
416+ val customScalarMap = GraphQLScalarType .newScalar()
417+ .name(" customScalarMap" )
418+ .description(" customScalarMap" )
419+ .coercing(object : Coercing <Map <String , Any >, Map <String , Any >> {
420+
421+ @Suppress(" UNCHECKED_CAST" )
422+ override fun parseValue (input : Any? ): Map <String , Any > = input as Map <String , Any >
423+
424+ @Suppress(" UNCHECKED_CAST" )
425+ override fun serialize (dataFetcherResult : Any? ): Map <String , Any > = dataFetcherResult as Map <String , Any >
426+
427+ override fun parseLiteral (input : Any? ): Map <String , Any > = (input as ObjectValue ).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue ).value }
428+ })
429+ .build()
0 commit comments