Here is a draft of my post "How to build REST API ...
# arrow
r
Here is a draft of my post "How to build REST API with Arrow, Ktor and requery in Kotlin": https://medium.com/@rafal.piotrowski/e5438c951418 Any feedback is welcome 🙂
2
🙂 4
arrow 8
p
I'd say you could work with 0.10-SNAPSHOT because the release is imminent and you'll be able to leverage
IO.effect
. I don't know if it's too much of a rewrite then 😄 I'll keep on reading.
Copy code
override fun findAll(): IOOf<List<Employee>> = IO {
in there you can make it return
IO<List<Employeee>>
and the compiler won't complain!!
👍 1
r
Is there any date or roadmap for 0.10.0 ?
r
it´s about to come out
we are finishing up some bugs
p
that Suspendable typeclass kiss fingertips
r
So I will need to update article soon 🙂
p
I love the approach, it leverages typeclasses perfectly
you got it perfectly right, support for either kind of routing with rx or IO by bridging to suspend
I don't believe we have Suspendable typeclass in the repo, do we?
through any other means?
s
Nice article! One question: Why is the code in your examples calling
runBlocking { ... }
? I think wrapping it in runBlocking is not necessary. The call to
suspendCoroutine { ... }
is enough, I think.
r
@pakoito It seems that
Suspendable
is not in 0.10.0-SNAPSHOT I got wrong impression about it talking with @simon.vergauwen I'll remove info about this class in 0.10.0 release of Arrow from my article
p
@streetsofboston you cannot call suspend functions on
suspendCoroutine
.
launch
is the equivalent of
unsafeRunSync
, except that it swallows all errors and doesn't wire cancellation and you have to fix that. Getting everything piped together is a bit messy. We've done it tho.
For this article's purposes it's close enough 🙂
r
Well, it works without
runBlocking
since
suspendCoroutine
is invoked in
suspend fun
, good catch @streetsofboston 👍
☝️ 2
👍 1
r
Great article! We just built out a rest API at work with ktor, arrow, and requery and this was a great reference! Using the typeclasses the way you did was brilliant. Thanks!
b
I like the
with (employeeRouting) { ... }
construct; that suggests to me it is possible to aggregate typed routes, similar to the syntax enabled by
http4s
I still want to see if it is possible to use
call
differently, though
s
I like the
with (employeeRouting) { ... }
construct; that suggests to me it is possible to aggregate typed routes, similar to the syntax enabled by
http4s
That’s a technique @raulraja has used to combine dependencies for a receiver. https://gist.github.com/raulraja/62de72039b11b05ed6d472cb8178da0c
If you can share some code, or the desired syntax and the types involved I’d happily take a look
b
TBH I think between you, @raulraja and @pakoito all of the code has already been written
r
with compiler plugins that technique is gonna be obsolete because we have injectable coherent type classes injected in the this scope automatically
d
@Rafal Piotrowski Thanks for writing this! I've been dying for some articles on Arrow+Ktor and I just so happen to use requery as well. I've been using exclusively Kotlin for almost two years now on Android and recently on a mobile API using Ktor. I'm still new to FP but I love it and try and use it as much as I can on Android and with Ktor.
r
You welcome @Derek Seroky. Some time ago I was searching Internet a lot to find a way to integrate Ktor with Arrow and I found almost nothing. So when I finally did that and had some time I wrote the above article. I hope it will be helpful for others.
👍 1
About requery: I was first using https://github.com/davidmoten/rxjava2-jdbc but there is too much SQL to write when using it, with requery there is some magic related to annotation processor, but there is no need to write SQL queries, CRUD operations are already implemented.
d
Requery definitely makes some things easier, though I've been bitten by that "magic" (especially around relationships) and sometimes wished I could just write the SQL. I wish there was more documentation, real life project samples or even more of a community around it. If you find any good resources, I would love to look into it more. Also, on a side note, have you found any persistence frameworks that have any Arrow integrations?
r
If someone wants to write a ktor module for arrow there is room for it and we could help
1
We are in the process of writing something similar to Doobie that would use the arrow type class hierarchy
❤️ 4
It's in the early stages discussion at 47 or find some other kind to integrate with
We already have AQL which if we find a target runtime we can interpret to sql
But that is a DSL is not ment to be used for jdbc queries but data structures instead
That satisfy the type classes
Like functor filter and others. It's more like LINQ
d
I would definitely be willing to try and help with Ktor module though I'm pretty new to FP so I'm not sure how much of help I would be. Always willing to help contribute where I can even if it means contributing documentation
r
I think a first integration is being able to expose ktor endpoints return type to accept IO and behind the scenes run the IO program in a non blocking fashion
Which would probably be with unsafeRunAsync or similar combinator
b
I still think I want to compare
ktor
to
http4s
. I'm not convinced that bridging
IO
is sufficient when the method for forming responses in
ktor
involves calling mutation methods on the
call
object supplied as an argument to the endpoint handler in the routing configuration. That doesn't feel very RT to me
yeah, a quick look over
http4s
suggests to me that response objects are RT, usually created by constructors (e.g.
Response(status=200, headers=(/* map literal */), "body")
) or by pure functions that return a new copy of the response object (e.g.
Response(...).addCookie(_ => ...)
) Definitely at odds with ktor's mutational methods on
call
d
@raulraja Is the
IO
api stabilizing in
0.10.0
? I've been avoiding using it since there was some discussion a few months ago about the API being re-worked. We've been basically just marking code with comments that will need to be refactored to use the new IO api once it is completed.
s
The API of IO is stable in
0.10.0
. Any chances to it’s API will only be additional combinators, beside any normal activity of course.
💯 2