11package com.apurebase.kgraphql.schema.dsl
22
3- import com.apurebase.kgraphql.schema.*
3+ import com.apurebase.kgraphql.schema.Publisher
4+ import com.apurebase.kgraphql.schema.Schema
5+ import com.apurebase.kgraphql.schema.SchemaException
6+ import com.apurebase.kgraphql.schema.dsl.operations.MutationDSL
7+ import com.apurebase.kgraphql.schema.dsl.operations.QueryDSL
8+ import com.apurebase.kgraphql.schema.dsl.operations.SubscriptionDSL
9+ import com.apurebase.kgraphql.schema.dsl.types.*
10+ import com.apurebase.kgraphql.schema.model.EnumValueDef
11+ import com.apurebase.kgraphql.schema.model.MutableSchemaDefinition
12+ import com.apurebase.kgraphql.schema.model.TypeDef
13+ import com.apurebase.kgraphql.schema.structure.SchemaCompilation
414import com.fasterxml.jackson.core.JsonParser
515import com.fasterxml.jackson.databind.DeserializationContext
616import com.fasterxml.jackson.databind.deser.std.StdDeserializer
717import com.fasterxml.jackson.databind.module.SimpleModule
8- import com.apurebase.kgraphql.schema.model.EnumValueDef
9- import com.apurebase.kgraphql.schema.model.TypeDef
10- import com.apurebase.kgraphql.schema.model.MutableSchemaDefinition
11- import com.apurebase.kgraphql.schema.structure.SchemaCompilation
1218import kotlin.reflect.KClass
1319
1420/* *
1521 * SchemaBuilder exposes rich DSL to setup GraphQL schema
1622 */
17- class SchemaBuilder < Context : Any >( private val init : SchemaBuilder < Context >.() -> Unit ) {
23+ class SchemaBuilder internal constructor( ) {
1824
1925 private val model = MutableSchemaDefinition ()
2026
2127 private var configuration = SchemaConfigurationDSL ()
2228
2329 fun build (): Schema {
24- init ()
2530 return SchemaCompilation (configuration.build(), model.toSchemaDefinition()).perform()
2631 }
2732
@@ -33,75 +38,79 @@ class SchemaBuilder<Context : Any>(private val init: SchemaBuilder<Context>.() -
3338 // OPERATIONS
3439 // ================================================================================
3540
36- fun query (name : String , init : QueryOrMutationDSL .() -> Unit ): Publisher {
37- val query = QueryOrMutationDSL (name, init ).toKQLQuery()
41+ fun query (name : String , init : QueryDSL .() -> Unit ): Publisher {
42+ val query = QueryDSL (name)
43+ .apply (init )
44+ .toKQLQuery()
3845 model.addQuery(query)
3946 return query
4047 }
4148
42- fun mutation (name : String , init : QueryOrMutationDSL .() -> Unit ): Publisher {
43- val mutation = QueryOrMutationDSL (name, init ).toKQLMutation()
49+ fun mutation (name : String , init : MutationDSL .() -> Unit ): Publisher {
50+ val mutation = MutationDSL (name)
51+ .apply (init )
52+ .toKQLMutation()
53+
4454 model.addMutation(mutation)
4555 return mutation
4656 }
4757
4858 fun subscription (name : String , init : SubscriptionDSL .() -> Unit ){
49- model.addSubscription(SubscriptionDSL (name, init ).toKQLSubscription())
59+ val subscription = SubscriptionDSL (name)
60+ .apply (init )
61+ .toKQLSubscription()
62+
63+ model.addSubscription(subscription)
5064 }
5165
5266 // ================================================================================
5367 // SCALAR
5468 // ================================================================================
5569
56- fun <T : Any >stringScalar (kClass : KClass <T >, block : ScalarDSL <T , String >.() -> Unit ){
57- val scalar = StringScalarDSL (kClass, block)
58- val coercion = scalar.createCoercion()
70+ fun <T : Any > stringScalar (kClass : KClass <T >, block : ScalarDSL <T , String >.() -> Unit ) {
71+ val scalar = StringScalarDSL (kClass).apply (block)
5972 configuration.appendMapper(scalar, kClass)
60- model.addScalar(TypeDef .Scalar (scalar.name, kClass, coercion , scalar.description))
73+ model.addScalar(TypeDef .Scalar (scalar.name, kClass, scalar.createCoercion() , scalar.description))
6174 }
6275
6376 inline fun <reified T : Any > stringScalar (noinline block : ScalarDSL <T , String >.() -> Unit ) {
6477 stringScalar(T ::class , block)
6578 }
6679
67- fun <T : Any >intScalar (kClass : KClass <T >, block : ScalarDSL <T , Int >.() -> Unit ){
68- val scalar = IntScalarDSL (kClass, block)
69- val coercion = scalar.createCoercion()
80+ fun <T : Any > intScalar (kClass : KClass <T >, block : ScalarDSL <T , Int >.() -> Unit ) {
81+ val scalar = IntScalarDSL (kClass).apply (block)
7082 configuration.appendMapper(scalar, kClass)
71- model.addScalar(TypeDef .Scalar (scalar.name, kClass, coercion , scalar.description))
83+ model.addScalar(TypeDef .Scalar (scalar.name, kClass, scalar.createCoercion() , scalar.description))
7284 }
7385
7486 inline fun <reified T : Any > intScalar (noinline block : ScalarDSL <T , Int >.() -> Unit ) {
7587 intScalar(T ::class , block)
7688 }
7789
78- fun <T : Any >floatScalar (kClass : KClass <T >, block : ScalarDSL <T , Double >.() -> Unit ){
79- val scalar = DoubleScalarDSL (kClass, block)
80- val coercion = scalar.createCoercion()
90+ fun <T : Any > floatScalar (kClass : KClass <T >, block : ScalarDSL <T , Double >.() -> Unit ) {
91+ val scalar = DoubleScalarDSL (kClass).apply (block)
8192 configuration.appendMapper(scalar, kClass)
82- model.addScalar(TypeDef .Scalar (scalar.name, kClass, coercion , scalar.description))
93+ model.addScalar(TypeDef .Scalar (scalar.name, kClass, scalar.createCoercion() , scalar.description))
8394 }
8495
8596 inline fun <reified T : Any > floatScalar (noinline block : ScalarDSL <T , Double >.() -> Unit ) {
8697 floatScalar(T ::class , block)
8798 }
8899
89- fun <T : Any >longScalar (kClass : KClass <T >, block : ScalarDSL <T , Long >.() -> Unit ){
90- val scalar = LongScalarDSL (kClass, block)
91- val coercion = scalar.createCoercion()
100+ fun <T : Any > longScalar (kClass : KClass <T >, block : ScalarDSL <T , Long >.() -> Unit ) {
101+ val scalar = LongScalarDSL (kClass).apply (block)
92102 configuration.appendMapper(scalar, kClass)
93- model.addScalar(TypeDef .Scalar (scalar.name, kClass, coercion , scalar.description))
103+ model.addScalar(TypeDef .Scalar (scalar.name, kClass, scalar.createCoercion() , scalar.description))
94104 }
95105
96- inline fun <reified T : Any >longScalar (noinline block : ScalarDSL <T , Long >.() -> Unit ) {
106+ inline fun <reified T : Any > longScalar (noinline block : ScalarDSL <T , Long >.() -> Unit ) {
97107 longScalar(T ::class , block)
98108 }
99109
100- fun <T : Any >booleanScalar (kClass : KClass <T >, block : ScalarDSL <T , Boolean >.() -> Unit ){
101- val scalar = BooleanScalarDSL (kClass, block)
102- val coercion = scalar.createCoercion()
110+ fun <T : Any > booleanScalar (kClass : KClass <T >, block : ScalarDSL <T , Boolean >.() -> Unit ) {
111+ val scalar = BooleanScalarDSL (kClass).apply (block)
103112 configuration.appendMapper(scalar, kClass)
104- model.addScalar(TypeDef .Scalar (scalar.name, kClass, coercion , scalar.description))
113+ model.addScalar(TypeDef .Scalar (scalar.name, kClass, scalar.createCoercion() , scalar.description))
105114 }
106115
107116 inline fun <reified T : Any > booleanScalar (noinline block : ScalarDSL <T , Boolean >.() -> Unit ) {
@@ -112,8 +121,8 @@ class SchemaBuilder<Context : Any>(private val init: SchemaBuilder<Context>.() -
112121 // TYPE
113122 // ================================================================================
114123
115- fun <T : Any >type (kClass : KClass <T >, block : TypeDSL <T >.() -> Unit ){
116- val type = TypeDSL (model.unionsMonitor, kClass, block)
124+ fun <T : Any > type (kClass : KClass <T >, block : TypeDSL <T >.() -> Unit ) {
125+ val type = TypeDSL (model.unionsMonitor, kClass). apply ( block)
117126 model.addObject(type.toKQLObject())
118127 }
119128
@@ -129,8 +138,12 @@ class SchemaBuilder<Context : Any>(private val init: SchemaBuilder<Context>.() -
129138 // ENUM
130139 // ================================================================================
131140
132- fun <T : Enum <T >>enum (kClass : KClass <T >, enumValues : Array <T >, block : (EnumDSL <T >.() -> Unit )? = null){
133- val type = EnumDSL (kClass, block)
141+ fun <T : Enum <T >> enum (kClass : KClass <T >, enumValues : Array <T >, block : (EnumDSL <T >.() -> Unit )? = null) {
142+ val type = EnumDSL (kClass).apply {
143+ if (block != null ) {
144+ block()
145+ }
146+ }
134147
135148 val kqlEnumValues = enumValues.map { value ->
136149 type.valueDefinitions[value]?.let { valueDSL ->
@@ -159,8 +172,8 @@ class SchemaBuilder<Context : Any>(private val init: SchemaBuilder<Context>.() -
159172 // UNION
160173 // ================================================================================
161174
162- fun unionType (name : String , block : UnionTypeDSL .() -> Unit ) : TypeID {
163- val union = UnionTypeDSL (block)
175+ fun unionType (name : String , block : UnionTypeDSL .() -> Unit ): TypeID {
176+ val union = UnionTypeDSL (). apply ( block)
164177 model.addUnion(TypeDef .Union (name, union.possibleTypes, union.description))
165178 return TypeID (name)
166179 }
@@ -169,8 +182,8 @@ class SchemaBuilder<Context : Any>(private val init: SchemaBuilder<Context>.() -
169182 // INPUT
170183 // ================================================================================
171184
172- fun <T : Any >inputType (kClass : KClass <T >, block : InputTypeDSL <T >.() -> Unit ) {
173- val input = InputTypeDSL (kClass, block)
185+ fun <T : Any > inputType (kClass : KClass <T >, block : InputTypeDSL <T >.() -> Unit ) {
186+ val input = InputTypeDSL (kClass). apply ( block)
174187 model.addInputObject(TypeDef .Input (input.name, kClass, input.description))
175188 }
176189
0 commit comments