Skip to content

Instantly share code, notes, and snippets.

@gaerfield
gaerfield / GraphQL vs. REST vs. RPC.md
Last active August 20, 2020 17:03
Graphql-thougts

GraphQL vs. REST vs. RPC

REST is a concept to decouple server and client by providing an API providing access to server-side data as ressources and provide methods to change these ressources.

RPC on the other hand is a binary protocol for invoking Procedure at a remote-Computer. In its simplest form an port is opened processing data in an closed binary protocol. A more standard-approach would be using SOAP+XML.

GraphQL is a Query-Language, specification and set of tools operating on a single http-endpoint. With the query-language the client is allowed to define, which data it wishes to receive and thus possibly reducing the number of requests and the amount of data to be send.

With Spring-Boot and several other frameworks REST-endpoints are very easy to implement and allowing a good access to a domain-model. Some kind of operations don't act on a single ressource. They don't result in creating a new ressource or they maybe changing multiple ressources at once. Such operations are easier to implement as R

@gaerfield
gaerfield / resources.kt
Last active March 27, 2020 16:50
I hate working with resources in Java or Kotlin. The idea if resources is good, but loading them is unbelievable error-prone ... every time. This one makes it a little easier. It just looks for the given file or path according to the package-path of the class.
import java.io.File
import kotlin.reflect.KClass
object Resources {
/**
* Loads a resource from within the resources-Folder (usually src/main/resources):
* ```
* val resource : File = this::class.resourceFile("myResource.xml")
* ```
*/
@gaerfield
gaerfield / EventBus.kt
Last active May 19, 2021 09:49
Simple Kotlin-EventBus including Unit-Tests
package infrastructure
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.*
internal class EventBus {
// Initialize the bus with an empty Event, so we can drop the first element everytime on a new subscription
// This is neccesary, because on subscription usually the current element is send (although it was added
// before subscription)
@gaerfield
gaerfield / FlowVsChannel.kt
Last active February 26, 2020 10:20
Comparison of kotlins Flow vs. Channel-Approach
package de.kramhal.coffeebutts
import de.kramhal.coffeebutts.Consumer.*
import de.kramhal.coffeebutts.FlowOperator.*
import de.kramhal.coffeebutts.Producer.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.flow.Flow
@gaerfield
gaerfield / ktor.kts
Created January 13, 2020 15:11
using Kscript for a simple ktor-server
#!/usr/bin/env kscript
@file:MavenRepository("jcenter","https://jcenter.bintray.com" )
@file:DependsOn("io.ktor:ktor-server-netty:1.2.6")
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
@gaerfield
gaerfield / FunnyExpensiveCast.kts
Last active November 25, 2019 15:58
Just a fun way to do casts in kotlin
inline fun <reified T> Any.cast() =
try { val toBeCasted = this; with(jacksonObjectMapper()) { readValue<T>(writeValueAsString(toBeCasted)) } }
catch (e: Exception) { throw ClassCastException("Can't convert from [${this::class}] to [${T::class}]:\n\t${e.message}") }
data class AClass(val name: String, val age: Int)
data class BClass(val name: String, val age: Int)
@Test
fun example(){
val a = AClass("whoop", 42)
@gaerfield
gaerfield / systemProcess.kts
Last active November 16, 2018 16:24
Why kscript is awesome
import java.io.File
/** Executes a shell-command and returns the result */
fun String.exe(dir: File? = null): String {
val process = ProcessBuilder("/bin/sh", "-c", this)
.redirectErrorStream(true)
.directory(dir)
.start()
val exitCode = process.waitFor()
if(exitCode != 0) {

Keybase proof

I hereby claim:

  • I am gaerfield on github.
  • I am gaerfield (https://keybase.io/gaerfield) on keybase.
  • I have a public key ASBdxOCKNyT49pL7_sIJChFDpXesopEFboq3SqnFRR195wo

To claim this, I am signing this object: