diff --git a/README.md b/README.md index e6f418f..1748259 100644 --- a/README.md +++ b/README.md @@ -125,13 +125,34 @@ flowchart LR 1. Open http://0.0.0.0:8888/ ## GraphQL +[KGraphQL](https://kgraphql.io/) 1. [Playground](http://0.0.0.0:8888/graphql) -2. Example Curl +2. Query documents via curl: ```bash curl 'http://0.0.0.0:8888/graphql' \ -H 'Content-Type: application/json' \ --data-binary '{"query":"query {\n documents {\n info\n }\n}\n"}' ``` +3. Create document via curl: + ```bash + curl 'http://0.0.0.0:8888/graphql' \ + -H 'Content-Type: application/json' \ + -H 'Accept: application/json' \ + --data-binary '{"query":"# query {\n# documents {\n# info\n# }\n# }\n\nmutation Document($info: String!) {\n documents(info: $info)\n}","variables":{"info":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sed eleifend urna. Vestibulum sodales cursus tellus, sit amet posuere felis pellentesque sed. Phasellus non lacus lobortis, iaculis nunc a, vulputate neque. Duis fringilla scelerisque augue, id ornare massa mollis sed. Integer augue enim, tincidunt in nunc ac, sollicitudin tincidunt mauris. Sed enim dolor, fermentum vel tincidunt at, interdum at est. Nunc posuere libero eu urna ultricies lobortis."}}' + ``` + +## MongoDB +1. https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/ +```bash +% mongosh +test> db.documents.insertOne( + { + info: "Lorem ipsum dolor sit amet...", + } +) + +test> db.documents.find() +``` That's a wrap for now. diff --git a/applications/basic-server/src/main/kotlin/io/initialcapacity/web/App.kt b/applications/basic-server/src/main/kotlin/io/initialcapacity/web/App.kt index 1ab778b..a6a2969 100644 --- a/applications/basic-server/src/main/kotlin/io/initialcapacity/web/App.kt +++ b/applications/basic-server/src/main/kotlin/io/initialcapacity/web/App.kt @@ -2,20 +2,16 @@ package io.initialcapacity.web import com.apurebase.kgraphql.GraphQL import freemarker.cache.ClassTemplateLoader +import io.initialcapacity.documents.CreateDocuments import io.initialcapacity.documents.QueryDocuments -import io.ktor.server.application.Application -import io.ktor.server.application.ApplicationCall -import io.ktor.server.application.call -import io.ktor.server.application.install -import io.ktor.server.engine.embeddedServer -import io.ktor.server.freemarker.FreeMarker -import io.ktor.server.freemarker.FreeMarkerContent -import io.ktor.server.http.content.staticResources -import io.ktor.server.netty.Netty -import io.ktor.server.response.respond -import io.ktor.server.routing.Routing -import io.ktor.server.routing.get -import io.ktor.util.pipeline.PipelineContext +import io.ktor.server.application.* +import io.ktor.server.engine.* +import io.ktor.server.freemarker.* +import io.ktor.server.http.content.* +import io.ktor.server.netty.* +import io.ktor.server.response.* +import io.ktor.server.routing.* +import io.ktor.util.pipeline.* import java.util.* fun Application.module() { @@ -35,6 +31,9 @@ fun Application.module() { query("documents") { resolver { -> QueryDocuments().list() } } + mutation("documents") { + resolver { info: String -> CreateDocuments().execute(info) } + } } } } diff --git a/components/data-documents/build.gradle.kts b/components/data-documents/build.gradle.kts index e7bc61f..fb393c8 100644 --- a/components/data-documents/build.gradle.kts +++ b/components/data-documents/build.gradle.kts @@ -2,4 +2,6 @@ dependencies { implementation(project(":support:workflow-support")) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") + implementation("org.mongodb:mongodb-driver-kotlin-coroutine:5.1.0") + implementation("org.mongodb:bson-kotlin:5.1.0") } \ No newline at end of file diff --git a/components/data-documents/src/main/kotlin/io/initialcapacity/documents/QueryDocuments.kt b/components/data-documents/src/main/kotlin/io/initialcapacity/documents/QueryDocuments.kt index 13ce44f..640e3f9 100644 --- a/components/data-documents/src/main/kotlin/io/initialcapacity/documents/QueryDocuments.kt +++ b/components/data-documents/src/main/kotlin/io/initialcapacity/documents/QueryDocuments.kt @@ -1,7 +1,15 @@ package io.initialcapacity.documents +import com.mongodb.ConnectionString +import com.mongodb.kotlin.client.coroutine.MongoClient +import kotlinx.coroutines.flow.toList + class QueryDocuments() { - fun list(): List { - return listOf(Document("Lorem Ipsum...")) + suspend fun list(): List { + val connectionString = ConnectionString("mongodb://localhost:27017") + val mongoClient = MongoClient.create(connectionString) + val db = mongoClient.getDatabase("test") + val collection = db.getCollection("documents") + return collection.find().toList() } } \ No newline at end of file