Skip to content

Commit 38925b1

Browse files
committed
feat: Engine shortcut
1 parent 1f9705d commit 38925b1

4 files changed

Lines changed: 83 additions & 36 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ fun Application.configureGraphQL() {
5656
playground() // recommended
5757

5858
graphql {
59-
engine(GraphQLJava) {
59+
// add import org.cufy.graphkt.java.`graphql-java`
60+
`graphql-java` {
6061
// engine-specific configuration
6162
}
6263

core/src/main/kotlin/GraphktEngine.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,55 @@ interface GraphktEngine {
7171
local: Map<Any?, Any?> = emptyMap()
7272
): Flow<GraphQLResponse>
7373
}
74+
75+
/**
76+
* An interface for builders with graphkt engine.
77+
*
78+
* Important Note: these interface might change in
79+
* the future. It was made to make it easier to
80+
* implement features for different kids of tweaks
81+
* with less code and not to be used by regular
82+
* users.
83+
*
84+
* @author LSafer
85+
* @since 2.0.0
86+
*/
87+
interface WithEngine<TConfiguration> {
88+
/**
89+
* The engine factory.
90+
*/
91+
@AdvancedGraphktApi("Use `engine()` instead")
92+
var engineFactory: GraphktEngineFactory<TConfiguration>? // REQUIRED
93+
94+
/**
95+
* Code to be executed to configure the engine.
96+
*/
97+
@AdvancedGraphktApi("Use `engine()` instead")
98+
val engineBlock: MutableList<TConfiguration.() -> Unit>
99+
}
100+
101+
/**
102+
* Set the engine factory.
103+
*
104+
* @param factory the engine factory.
105+
* @param block the engine configuration.
106+
* @since 2.0.0
107+
*/
108+
@OptIn(AdvancedGraphktApi::class)
109+
fun <TConfiguration> WithEngine<TConfiguration>.engine(
110+
factory: GraphktEngineFactory<TConfiguration>,
111+
block: TConfiguration.() -> Unit = {}
112+
) {
113+
engineFactory = factory
114+
engineBlock += block
115+
}
116+
117+
/**
118+
* Configure the engine with the given [block].
119+
*/
120+
@OptIn(AdvancedGraphktApi::class)
121+
fun <TConfiguration> WithEngine<TConfiguration>.engine(
122+
block: TConfiguration.() -> Unit
123+
) {
124+
engineBlock += block
125+
}

graphql-java/src/main/kotlin/GraphQLJava.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,26 @@ class GraphQLJavaEngine(
102102
return GraphQLResponse(result)
103103
}
104104
}
105+
106+
107+
/**
108+
* Set the engine factory to be [GraphQLJava].
109+
*
110+
* @param block the engine configuration.
111+
* @since 2.0.0
112+
*/
113+
@Suppress("FunctionName")
114+
fun WithEngine<GraphQLJavaConfiguration>.`graphql-java`(
115+
block: GraphQLJavaConfiguration.() -> Unit
116+
) {
117+
engine(GraphQLJava, block)
118+
}
119+
120+
/**
121+
* Set the engine factory to be [GraphQLJava].
122+
*
123+
* @since 2.0.0
124+
*/
125+
@Suppress("ObjectPropertyName")
126+
val WithEngine<GraphQLJavaConfiguration>.`graphql-java`
127+
get() = `graphql-java` {}

ktor/src/main/kotlin/Configuration.kt

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.cufy.graphkt.ktor
1818
import io.ktor.server.application.*
1919
import org.cufy.graphkt.AdvancedGraphktApi
2020
import org.cufy.graphkt.GraphktEngineFactory
21+
import org.cufy.graphkt.WithEngine
2122
import org.cufy.graphkt.schema.*
2223
import kotlin.time.Duration
2324

@@ -28,7 +29,9 @@ import kotlin.time.Duration
2829
* @author LSafer
2930
* @since 2.0.0
3031
*/
31-
class Configuration<TConfiguration> : WithDeferredBuilder {
32+
class Configuration<TConfiguration> :
33+
WithEngine<TConfiguration>,
34+
WithDeferredBuilder {
3235
/**
3336
* True, to enable `graphql-ws` implementation.
3437
*
@@ -43,19 +46,13 @@ class Configuration<TConfiguration> : WithDeferredBuilder {
4346
*/
4447
var connectionInitWaitTimeout: Duration? = null
4548

46-
/**
47-
* The engine factory.
48-
*/
4949
@AdvancedGraphktApi("Use `engine()` instead")
50-
var engineFactory: GraphktEngineFactory<TConfiguration>? = null
50+
override var engineFactory: GraphktEngineFactory<TConfiguration>? = null
5151

5252
/* blocks */
5353

54-
/**
55-
* Code to be executed to configure the engine.
56-
*/
5754
@AdvancedGraphktApi("Use `engine()` instead")
58-
val engineBlock: MutableList<TConfiguration.() -> Unit> = mutableListOf()
55+
override val engineBlock: MutableList<TConfiguration.() -> Unit> = mutableListOf()
5956

6057
/**
6158
* Code to be executed to configure the schema.
@@ -98,32 +95,6 @@ fun <TConfiguration> Configuration<TConfiguration>.disableWebsocket() {
9895
websocket = false
9996
}
10097

101-
/**
102-
* Set the engine factory.
103-
*
104-
* @param factory the engine factory.
105-
* @param block the engine configuration.
106-
* @since 2.0.0
107-
*/
108-
@OptIn(AdvancedGraphktApi::class)
109-
fun <TConfiguration> Configuration<TConfiguration>.engine(
110-
factory: GraphktEngineFactory<TConfiguration>,
111-
block: TConfiguration.() -> Unit = {}
112-
) {
113-
engineFactory = factory
114-
engineBlock += block
115-
}
116-
117-
/**
118-
* Configure the engine with the given [block].
119-
*/
120-
@OptIn(AdvancedGraphktApi::class)
121-
fun <TConfiguration> Configuration<TConfiguration>.engine(
122-
block: TConfiguration.() -> Unit
123-
) {
124-
engineBlock += block
125-
}
126-
12798
/**
12899
* Configure the schema with the given [block].
129100
*/

0 commit comments

Comments
 (0)