Skip to content

Commit 686b544

Browse files
committed
feat(example): Added example module
- Added module `example` - Removed artifact `org.cufy:graphkt:TAG` now the user must add artifacts individually. - Updated README
1 parent 95e2dbb commit 686b544

6 files changed

Lines changed: 234 additions & 68 deletions

File tree

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ repositories {
1818

1919
dependencies {
2020
// Replace TAG with the desired version
21-
implementation("org.cufy:graphkt:TAG")
21+
val graphkt_version = "TAG"
22+
implementation("org.cufy.graphkt:core:$graphkt_version")
23+
implementation("org.cufy.graphkt:ktor:$graphkt_version")
24+
implementation("org.cufy.graphkt:graphql-java:$graphkt_version")
2225
}
2326
```
2427

@@ -31,14 +34,17 @@ data class Entity(
3134
val name: String
3235
)
3336

34-
val EntityObjectType = GraphQLObjectType<Entity>("Entity") {
37+
val EntityObjectType: GraphQLObjectType<Entity> = GraphQLObjectType {
38+
name("Entity")
3539
description { "Some entity." }
3640

37-
field(Entity::name, GraphQLStringType) {
41+
field(Entity::name) {
42+
type { GraphQLStringType }
3843
description { "The name of the entity." }
3944
}
4045

41-
field("nameWithCustomVar", GraphQLNullableType(GraphQLStringType)) {
46+
field("nameWithCustomVar") {
47+
type { GraphQLStringType.Nullable }
4248
description { "The name of the entity with the customVar in the context." }
4349

4450
get {
@@ -61,18 +67,20 @@ fun Application.configureGraphQL() {
6167
// engine-specific configuration
6268
}
6369

64-
context {
65-
it["myCustomVar"] = Math.random()
70+
before {
71+
context["myCustomVar"] = Math.random()
6672
}
6773

6874
schema {
6975
query {
7076
description { "The root query." }
7177

72-
field("getEntityWithName", EntityObjectType) {
78+
field("getEntityWithName") {
79+
type { EntityObjectType }
7380
description { "Get an entity instance." }
7481

75-
val nameArg = argument<String>("name", GraphQLStringType) {
82+
val nameArg = argument<String>("name") {
83+
type { GraphQLStringType }
7684
description { "The name of the entity." }
7785
}
7886

@@ -82,10 +90,12 @@ fun Application.configureGraphQL() {
8290
mutation {
8391
description { "The root mutation" }
8492

85-
field("pushEntity", EntityObjectType) {
93+
field("pushEntity") {
94+
type { EntityObjectType }
8695
description { "Push an entity to subscribers" }
8796

88-
val nameArg = argument<String>("name", GraphQLStringType) {
97+
val nameArg = argument<String>("name") {
98+
type { GraphQLStringType }
8999
description { "The name of the entity." }
90100
}
91101

@@ -99,7 +109,8 @@ fun Application.configureGraphQL() {
99109
subscription {
100110
description { "The root subscription" }
101111

102-
field("subscribeToEntities", EntityObjectType) {
112+
field("subscribeToEntities") {
113+
type { EntityObjectType }
103114
description { "Subscribe to pushed entities" }
104115

105116
getFlow { EntitiesFlow }

build.gradle.kts

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,20 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
plugins {
4-
kotlin("jvm") version kotlin_version
5-
kotlin("plugin.serialization") version kotlin_version
2+
kotlin("jvm") version kotlin_version apply false
3+
kotlin("plugin.serialization") version kotlin_version apply false
64
id("maven-publish")
75
}
86

97
group = "org.cufy"
108
version = "2.0.0"
119

12-
repositories {
13-
mavenCentral()
14-
maven { url = uri("https://jitpack.io") }
15-
}
16-
17-
dependencies {
18-
implementation(project(":core"))
19-
implementation(project(":ktor"))
20-
implementation(project(":graphql-java"))
21-
22-
implementation(kotlin("stdlib"))
23-
24-
implementation(Dependencies.Kotlin.serialization)
25-
implementation(Dependencies.Kotlin.coroutines_core)
26-
implementation(Dependencies.Kotlin.coroutines_reactive)
27-
implementation(Dependencies.Kotlin.reflect)
28-
29-
testImplementation(Dependencies.Ktor.server_core_jvm)
30-
testImplementation(Dependencies.Ktor.server_websockets)
31-
testImplementation(Dependencies.Ktor.host_common_jvm)
32-
testImplementation(Dependencies.Ktor.netty_jvm)
33-
testImplementation(Dependencies.Ktor.call_logging_jvm)
34-
testImplementation(Dependencies.Ktor.content_negotiation_jvm)
35-
testImplementation(Dependencies.Ktor.serialization_kotlinx_json_jvm)
36-
37-
testImplementation(Dependencies.Logging.logback_core)
38-
testImplementation(Dependencies.Logging.logback_classic)
39-
testImplementation(Dependencies.Logging.self4j)
40-
41-
testImplementation(Dependencies.rxjava)
42-
testImplementation(Dependencies.graphql_java)
43-
44-
testImplementation(kotlin("test"))
45-
}
46-
47-
tasks.test {
48-
useJUnitPlatform()
49-
}
50-
51-
tasks.withType<KotlinCompile> {
52-
kotlinOptions.jvmTarget = "1.8"
53-
}
54-
55-
afterEvaluate {
56-
publishing {
57-
publications {
58-
create<MavenPublication>("maven") {
59-
from(components["java"])
60-
artifactId = "graphkt"
61-
}
62-
}
10+
subprojects {
11+
repositories {
12+
mavenCentral()
13+
maven { url = uri("https://jitpack.io") }
6314
}
64-
}
6515

66-
subprojects {
16+
if (name == "example") return@subprojects
17+
6718
group = "org.cufy.graphkt"
6819

6920
afterEvaluate {

example/build.gradle.kts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
plugins {
2+
kotlin("jvm") version kotlin_version
3+
kotlin("plugin.serialization") version kotlin_version
4+
}
5+
6+
repositories {
7+
mavenCentral()
8+
maven { url = uri("https://jitpack.io") }
9+
}
10+
11+
dependencies {
12+
implementation(project(":core"))
13+
implementation(project(":ktor"))
14+
implementation(project(":graphql-java"))
15+
16+
implementation(kotlin("stdlib"))
17+
implementation(kotlin("test"))
18+
19+
implementation(Dependencies.Ktor.server_core_jvm)
20+
implementation(Dependencies.Ktor.server_websockets)
21+
implementation(Dependencies.Ktor.host_common_jvm)
22+
implementation(Dependencies.Ktor.netty_jvm)
23+
implementation(Dependencies.Ktor.call_logging_jvm)
24+
implementation(Dependencies.Ktor.content_negotiation_jvm)
25+
implementation(Dependencies.Ktor.serialization_kotlinx_json_jvm)
26+
27+
implementation(Dependencies.Logging.logback_core)
28+
implementation(Dependencies.Logging.logback_classic)
29+
implementation(Dependencies.Logging.self4j)
30+
31+
implementation(Dependencies.rxjava)
32+
implementation(Dependencies.graphql_java)
33+
}
34+
35+
tasks.getByName<Test>("test") {
36+
useJUnitPlatform()
37+
}

example/src/main/kotlin/example.kt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package testing
2+
3+
import io.ktor.serialization.kotlinx.json.*
4+
import io.ktor.server.application.*
5+
import io.ktor.server.engine.*
6+
import io.ktor.server.netty.*
7+
import io.ktor.server.plugins.callloging.*
8+
import io.ktor.server.plugins.contentnegotiation.*
9+
import io.ktor.server.request.*
10+
import io.ktor.server.websocket.*
11+
import kotlinx.coroutines.flow.MutableSharedFlow
12+
import kotlinx.coroutines.flow.filter
13+
import org.cufy.graphkt.java.`graphql-java`
14+
import org.cufy.graphkt.ktor.graphql
15+
import org.cufy.graphkt.ktor.playground
16+
import org.cufy.graphkt.ktor.schema
17+
import org.cufy.graphkt.schema.*
18+
import org.slf4j.event.Level
19+
import java.time.Duration
20+
21+
class CustomScalar(val value: String)
22+
class CustomObject(val name: String, val value: String)
23+
24+
val CustomFlow = MutableSharedFlow<CustomObject>()
25+
26+
val CustomScalarType = GraphQLScalarType<CustomScalar> {
27+
name("CustomScalar")
28+
description { "An example of a custom scalar." }
29+
encode { GraphQLString(it.value) }
30+
decode {
31+
require(it is GraphQLString) { "Expected GraphQLString but got ${it::class.simpleName}" }
32+
CustomScalar(it.value)
33+
}
34+
}
35+
36+
val CustomObjectType: GraphQLObjectType<CustomObject> = GraphQLObjectType {
37+
name("Custom")
38+
description { "An example of a custom object type." }
39+
field(CustomObject::name) {
40+
type { GraphQLStringType }
41+
description { "The name of the object." }
42+
}
43+
field(CustomObject::value) {
44+
type { GraphQLStringType }
45+
description { "The value of the object." }
46+
}
47+
}
48+
49+
fun main() {
50+
embeddedServer(
51+
Netty,
52+
4444,
53+
module = Application::myApplicationModule
54+
).start(true)
55+
}
56+
57+
fun Application.myApplicationModule() {
58+
install(CallLogging) {
59+
level = Level.INFO
60+
filter { call -> call.request.path().startsWith("/") }
61+
}
62+
install(ContentNegotiation) {
63+
json()
64+
}
65+
install(WebSockets) {
66+
pingPeriod = Duration.ofSeconds(15)
67+
timeout = Duration.ofSeconds(15)
68+
maxFrameSize = Long.MAX_VALUE
69+
masking = false
70+
}
71+
72+
playground()
73+
// graphiql()
74+
graphql {
75+
`graphql-java`
76+
77+
schema {
78+
query {
79+
queries()
80+
}
81+
mutation {
82+
mutations()
83+
}
84+
subscription {
85+
subscriptions()
86+
}
87+
}
88+
}
89+
}
90+
91+
@GraphQLDsl
92+
fun GraphQLRoute<Unit>.queries() {
93+
field("cat") {
94+
type { CustomScalarType }
95+
description { "Return the input" }
96+
97+
val valueArg = argument("value") {
98+
type { CustomScalarType }
99+
description { "The value to return" }
100+
}
101+
102+
get { valueArg() }
103+
}
104+
}
105+
106+
@GraphQLDsl
107+
fun GraphQLRoute<Unit>.mutations() {
108+
field("emit") {
109+
type { GraphQLVoidType }
110+
111+
val nameArg = argument("name") {
112+
type { GraphQLStringType }
113+
description { "The name to be associated with the emitted value." }
114+
}
115+
val valueArg = argument("value") {
116+
type { GraphQLStringType }
117+
description { "The value to be emitted" }
118+
}
119+
120+
get {
121+
val name = nameArg()
122+
val value = valueArg()
123+
124+
CustomFlow.emit(CustomObject(name, value))
125+
}
126+
}
127+
}
128+
129+
@GraphQLDsl
130+
fun GraphQLRoute<Unit>.subscriptions() {
131+
field("collect") {
132+
type { CustomObjectType }
133+
134+
val nameArg = argument("name") {
135+
type { GraphQLStringType }
136+
description { "The name to filter the messages by." }
137+
}
138+
139+
getFlow {
140+
val name = nameArg()
141+
142+
CustomFlow.filter { it.name == name }
143+
}
144+
}
145+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<configuration>
2+
<appender name="STDOUT"
3+
class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
<root level="trace">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
<logger name="org.eclipse.jetty"
12+
level="INFO"/>
13+
<logger name="io.netty"
14+
level="INFO"/>
15+
<logger name="org.mongodb.driver"
16+
level="ERROR"/>
17+
<logger name="graphql"
18+
level="ERROR"/>
19+
<logger name="notprivacysafe"
20+
level="ERROR"/>
21+
</configuration>

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ rootProject.name = "graphkt"
22
include(":core")
33
include(":graphql-java")
44
include(":ktor")
5+
include(":example")

0 commit comments

Comments
 (0)