|
| 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 | +} |
0 commit comments