/* Ċ Ġ* ĠCopyright Ġ 2 0 2 1 ĠExp edia , Ġ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 . exp edi ag roup . graphql . generator . test . integration Ċ Ċ import Ġcom . exp edi ag roup . graphql . generator . Schema Generator Ċ import Ġcom . exp edi ag roup . graphql . generator . Schema Generator Config Ċ import Ġcom . exp edi ag roup . graphql . generator . TopLevel Object Ċ import Ġcom . exp edi ag roup . graphql . generator . annotations . GraphQL Directive Ċ import Ġcom . exp edi ag roup . graphql . generator . annotations . GraphQL Union Ċ import Ġcom . exp edi ag roup . graphql . generator . extensions . deep Name Ċ import Ġcom . exp edi ag roup . graphql . generator . extensions . unwrap Type Ċ import Ġcom . exp edi ag roup . graphql . generator . test Schema Config Ċ import Ġcom . exp edi ag roup . graphql . generator . to Schema Ċ import Ġgraphql . schema . GraphQL ObjectType Ċ import Ġgraphql . schema . GraphQL Union Type Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġkotlin . reflect . K Class Ċ import Ġkotlin . test . assertEquals Ċ import Ġkotlin . test . assert Fails Ċ import Ġkotlin . test . assertNotNull Ċ import Ġkotlin . test . assertSame Ċ Ċ class ĠCustom Union Annotation Test Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` custom Ġun ions Ġcan Ġbe Ġdefined Ġwith Ġa Ġvariety Ġof Ġreturn Ġtypes `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġto Schema ( test Schema Config , ĠlistOf ( TopLevel Object ( Query ()))) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema ) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" One ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Two ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Three ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Four ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Even ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Odd ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Number ")) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( schema . getType (" Prime ")) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Even !", Ġschema . query Type . getField Definition (" even "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Odd !", Ġschema . query Type . getField Definition (" odd "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Number !", Ġschema . query Type . getField Definition (" number "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Number ", Ġschema . query Type . getField Definition (" nullable Number "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals ("[ Number !] !", Ġschema . query Type . getField Definition (" list Numbers "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals ("[ Number ]", Ġschema . query Type . getField Definition (" nullable List Numbers "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Prime !", Ġschema . query Type . getField Definition (" prime "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Prime ", Ġschema . query Type . getField Definition (" nullable Prime "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals ("[ Prime !] !", Ġschema . query Type . getField Definition (" list Pri mes "). type . deep Name ) ĊĠĠĠĠĠĠĠ ĠassertEquals ("[ Prime ]", Ġschema . query Type . getField Definition (" nullable List Pri mes "). type . deep Name ) ĊĊĠĠĠĠĠĠĠ Ġval Ġunion With Directive Ġ= Ġschema . getType (" Prime ") Ġas ĠGraphQL Union Type ĊĠĠĠĠĠĠĠ ĠassertNotNull ( union With Directive . applied Directives ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġunion With Directive . applied Directives . size ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Test Directive ", Ġunion With Directive . applied Directives [ 0 ]. name ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` verify Ġexception Ġis Ġthrown Ġwhen Ġunion Ġreturns Ġdifferent Ġtypes `() Ġ{ ĊĠĠĠĠĠĠĠ Ġassert Fails Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġto Schema ( test Schema Config , ĠlistOf ( TopLevel Object ( Invalid Query ()))) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` verify Ġexception Ġis Ġthrown Ġwhen Ġcustom Ġunion Ġreturn Ġtype Ġis Ġnot ĠAny `() Ġ{ ĊĠĠĠĠĠĠĠ Ġassert Fails Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġto Schema ( test Schema Config , ĠlistOf ( TopLevel Object ( Invalid ReturnType Number ()))) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġassert Fails Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġto Schema ( test Schema Config , ĠlistOf ( TopLevel Object ( Invalid ReturnType Prime ()))) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` verify ĠMeta ĠUnion ĠAnnotation Ġwhen Ġadding Ġas Ġadditional Ġtype `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġgenerator Ġ= ĠCustom Schema Generator ( test Schema Config ) ĊĠĠĠĠĠĠĠ Ġgenerator . add Types ( My Annotation :: class ) ĊĠĠĠĠĠĠĠ Ġval Ġtypes Ġ= Ġgenerator . generate Custom Additional Types () ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( 2 , Ġtypes . size ) ĊĠĠĠĠĠĠĠ Ġval Ġmeta Union Ġ= Ġtypes . find Ġ{ Ġit . deep Name Ġ== Ġ" Prime " Ġ} Ġas ĠGraphQL Union Type ĊĠĠĠĠĠĠĠ ĠassertNotNull ( meta Union ) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( meta Union . applied Directives ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġmeta Union . applied Directives . size ) ĊĠĠĠĠĠĠĠ ĠassertEquals (" Test Directive ", Ġmeta Union . applied Directives [ 0 ]. name ) ĊĠĠĠĠĠĠĠ ĠassertSame ( meta Union , Ġ( types . find Ġ{ Ġit . deep Name Ġ!= Ġ" Prime " Ġ} Ġas ĠGraphQL ObjectType ). getField (" union "). type . unwrap Type ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġclass ĠOne ( val Ġvalue : ĠString ) ĊĠĠĠ Ġclass ĠTwo ( val Ġvalue : ĠString ) ĊĠĠĠ Ġclass ĠThree ( val Ġvalue : ĠString ) ĊĠĠĠ Ġclass ĠFour ( val Ġvalue : ĠString ) ĊĊĠĠĠ Ġclass ĠQuery Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Even ", Ġpossible Types Ġ= Ġ[ Two :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġeven ( first : ĠBoolean ): ĠAny Ġ= Ġif Ġ( first ) ĠTwo (" 2 ") Ġelse ĠFour (" 4 ") ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Odd ", Ġpossible Types Ġ= Ġ[ One :: class , ĠThree :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġodd ( first : ĠBoolean ): ĠAny Ġ= Ġif Ġ( first ) ĠOne (" 1 ") Ġelse ĠThree (" 3 ") ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class , ĠThree :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnumber (): ĠAny Ġ= ĠOne (" 1 ") ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class , ĠThree :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnullable Number ( isNull : ĠBoolean ): ĠAny ? Ġ= Ġif Ġ( isNull ) Ġnull Ġelse ĠOne (" 1 ") ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class , ĠThree :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġlist Numbers (): ĠList < Any > Ġ= ĠlistOf ( One (" 1 "), ĠTwo (" 2 ")) ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class , ĠThree :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnullable List Numbers (): ĠList < Any ?> ? Ġ= Ġnull ĊĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġfun Ġprime ( first : ĠBoolean ): ĠAny Ġ= Ġif Ġ( first ) ĠTwo (" 2 ") Ġelse ĠThree (" 3 ") ĊĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġfun Ġnullable Prime ( isNull : ĠBoolean ): ĠAny ? Ġ= Ġif Ġ( isNull ) Ġnull Ġelse ĠTwo (" 2 ") ĊĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġfun Ġlist Pri mes (): ĠList < Any > Ġ= ĠlistOf ( Two (" 2 "), ĠThree (" 3 ")) ĊĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġfun Ġnullable List Pri mes (): ĠList < Any ?> ? Ġ= Ġnull ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠEach Ġunion Ġhere Ġis Ġvalid , Ġbut Ġusing Ġthem Ġtogether Ġin Ġthe Ġsame Ġschema Ġmeans Ġthe Ġunion Ġ" Number " ĊĠĠĠĠ Ġ* Ġis Ġdefined Ġtwice Ġwith Ġdifferent Ġdefinitions Ġand Ġshould Ġfail ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġclass ĠInvalid Query Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnumber 1 (): ĠAny Ġ= ĠOne (" 1 ") ĊĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ Three :: class , ĠFour :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnumber 2 (): ĠAny Ġ= ĠThree (" 1 ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠWhile Ġit Ġis Ġvalid Ġto Ġcompile , Ġlibrary Ġusers Ġshould Ġreturn ĠAny Ġfor Ġthe Ġcustom ĊĠĠĠĠ Ġ* Ġunion Ġannotation ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġclass ĠInvalid ReturnType Number Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Number ", Ġpossible Types Ġ= Ġ[ One :: class , ĠTwo :: class ]) ĊĠĠĠĠĠĠĠ Ġfun Ġnumber 1 (): ĠOne Ġ= ĠOne (" one ") ĊĊĠĠĠĠĠĠĠ Ġfun Ġnumber 2 (): ĠOne Ġ= ĠOne (" two ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠWhile Ġit Ġis Ġvalid Ġto Ġcompile , Ġlibrary Ġusers Ġshould Ġreturn ĠAny Ġfor Ġthe Ġannotation ĊĠĠĠĠ Ġ* Ġwith Ġthe Ġmeta Ġunion Ġannotation ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġclass ĠInvalid ReturnType Prime Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġfun Ġprime (): ĠTwo Ġ= ĠTwo (" two ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GraphQL Directive ( name Ġ= Ġ" Test Directive ") ĊĠĠĠ Ġannotation Ġclass ĠTest Directive ĊĊĠĠĠ Ġannotation Ġclass ĠMy Annotation ĊĊĠĠĠ Ġ@ Test Directive ĊĠĠĠ Ġ@ My Annotation ĊĠĠĠ Ġ@ GraphQL Union ( name Ġ= Ġ" Prime ", Ġpossible Types Ġ= Ġ[ Two :: class , ĠThree :: class ]) ĊĠĠĠ Ġannotation Ġclass ĠPrime Union ĊĊĠĠĠ Ġ@ My Annotation ĊĠĠĠ Ġdata Ġclass ĠMy Type ( ĊĠĠĠĠĠĠĠ Ġ@ Prime Union ĊĠĠĠĠĠĠĠ Ġval Ġunion : ĠAny ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġclass ĠCustom Schema Generator ( config : ĠSchema Generator Config ) Ġ: ĠSchema Generator ( config ) Ġ{ ĊĠĠĠĠĠĠĠ Ġinternal Ġfun Ġadd Types ( annotation : ĠK Class <* >) Ġ= Ġadd Additional Types With Annotation ( annotation ) ĊĊĠĠĠĠĠĠĠ Ġinternal Ġfun Ġgenerate Custom Additional Types () Ġ= Ġgenerate Additional Types () ĊĠĠĠ Ġ} Ċ } Ċ