Skip to content

Commit

Permalink
Upgrade ZIO to RC18-2 (#279)
Browse files Browse the repository at this point in the history
* Upgrade ZIO

* Upgrade sttp
  • Loading branch information
ghostdogpr authored Mar 12, 2020
1 parent 7d5ded8 commit 7b17d21
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 118 deletions.
4 changes: 2 additions & 2 deletions benchmarks/src/main/scala/caliban/GraphQLBenchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import sangria.marshalling.circe._
import sangria.parser.QueryParser
import sangria.schema._
import zio.internal.Platform
import zio.{ BootstrapRuntime, Runtime, UIO }
import zio.{ BootstrapRuntime, Runtime, UIO, ZEnv }

@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
Expand Down Expand Up @@ -125,7 +125,7 @@ class GraphQLBenchmarks {
}
"""

val runtime: Runtime[Unit] = new BootstrapRuntime {
val runtime: Runtime[ZEnv] = new BootstrapRuntime {
override val platform: Platform = Platform.benchmark
}

Expand Down
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ val allScala = Seq("2.13.1", mainScala)
val catsEffectVersion = "2.1.2"
val http4sVersion = "0.21.1"
val silencerVersion = "1.6.0"
val sttpVersion = "2.0.4"
val zioVersion = "1.0.0-RC18-1"
val zioInteropCatsVersion = "2.0.0.0-RC11"
val sttpVersion = "2.0.5"
val zioVersion = "1.0.0-RC18-2"
val zioInteropCatsVersion = "2.0.0.0-RC12"

inThisBuild(
List(
Expand Down Expand Up @@ -131,7 +131,7 @@ lazy val monixInterop = project
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio-interop-reactivestreams" % "1.0.3.5-RC5",
"dev.zio" %% "zio-interop-reactivestreams" % "1.0.3.5-RC6",
"dev.zio" %% "zio-interop-cats" % zioInteropCatsVersion,
"io.monix" %% "monix" % "3.1.0"
)
Expand Down
101 changes: 62 additions & 39 deletions examples/src/main/scala/caliban/ExampleService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,71 @@ package caliban

import caliban.ExampleData._
import zio.stream.ZStream
import zio.{ Queue, Ref, UIO }

class ExampleService(characters: Ref[List[Character]], subscribers: Ref[List[Queue[String]]]) {

def getCharacters(origin: Option[Origin]): UIO[List[Character]] =
characters.get.map(_.filter(c => origin.forall(c.origin == _)))

def findCharacter(name: String): UIO[Option[Character]] = characters.get.map(_.find(c => c.name == name))

def deleteCharacter(name: String): UIO[Boolean] =
characters
.modify(list =>
if (list.exists(_.name == name)) (true, list.filterNot(_.name == name))
else (false, list)
)
.tap(deleted =>
UIO.when(deleted)(
subscribers.get.flatMap(
// add item to all subscribers
UIO.foreach(_)(queue =>
queue
.offer(name)
.onInterrupt(
subscribers.update(_.filterNot(_ == queue))
) // if queue was shutdown, remove from subscribers
)
)
)
)
import zio.{ Has, Queue, Ref, UIO, URIO, ZLayer }

def deletedEvents: ZStream[Any, Nothing, String] = ZStream.unwrap {
for {
queue <- Queue.unbounded[String]
_ <- subscribers.update(queue :: _)
} yield ZStream.fromQueue(queue)
object ExampleService {

type ExampleService = Has[Service]

trait Service {
def getCharacters(origin: Option[Origin]): UIO[List[Character]]

def findCharacter(name: String): UIO[Option[Character]]

def deleteCharacter(name: String): UIO[Boolean]

def deletedEvents: ZStream[Any, Nothing, String]
}
}

object ExampleService {
def make(initial: List[Character]): UIO[ExampleService] =
def getCharacters(origin: Option[Origin]): URIO[ExampleService, List[Character]] =
URIO.accessM(_.get.getCharacters(origin))

def findCharacter(name: String): URIO[ExampleService, Option[Character]] =
URIO.accessM(_.get.findCharacter(name))

def deleteCharacter(name: String): URIO[ExampleService, Boolean] =
URIO.accessM(_.get.deleteCharacter(name))

def deletedEvents: ZStream[ExampleService, Nothing, String] =
ZStream.accessStream(_.get.deletedEvents)

def make(initial: List[Character]): ZLayer[Any, Nothing, ExampleService] = ZLayer.fromEffect {
for {
state <- Ref.make(initial)
characters <- Ref.make(initial)
subscribers <- Ref.make(List.empty[Queue[String]])
} yield new ExampleService(state, subscribers)
} yield new Service {

def getCharacters(origin: Option[Origin]): UIO[List[Character]] =
characters.get.map(_.filter(c => origin.forall(c.origin == _)))

def findCharacter(name: String): UIO[Option[Character]] = characters.get.map(_.find(c => c.name == name))

def deleteCharacter(name: String): UIO[Boolean] =
characters
.modify(list =>
if (list.exists(_.name == name)) (true, list.filterNot(_.name == name))
else (false, list)
)
.tap(deleted =>
UIO.when(deleted)(
subscribers.get.flatMap(
// add item to all subscribers
UIO.foreach(_)(queue =>
queue
.offer(name)
.onInterrupt(
subscribers.update(_.filterNot(_ == queue))
) // if queue was shutdown, remove from subscribers
)
)
)
)
def deletedEvents: ZStream[Any, Nothing, String] = ZStream.unwrap {
for {
queue <- Queue.unbounded[String]
_ <- subscribers.update(queue :: _)
} yield ZStream.fromQueue(queue)
}
}
}
}
32 changes: 15 additions & 17 deletions examples/src/main/scala/caliban/akkahttp/ExampleApp.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package caliban.akkahttp

import scala.language.postfixOps
import scala.io.StdIn
import scala.language.postfixOps
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import caliban.ExampleData.{ sampleCharacters, Character, CharacterArgs, CharactersArgs, Role }
import caliban.ExampleService.ExampleService
import caliban.GraphQL.graphQL
import caliban.schema.Annotations.{ GQLDeprecated, GQLDescription }
import caliban.schema.GenericSchema
import caliban.wrappers.ApolloTracing.apolloTracing
import caliban.wrappers.Wrappers._
import caliban.{ AkkaHttpAdapter, ExampleService, GraphQL, RootResolver }
import zio.clock.Clock
import zio.console.Console
import caliban.{ AkkaHttpAdapter, ExampleService, RootResolver }
import zio.duration._
import zio.stream.ZStream
import zio.{ Runtime, URIO }

object ExampleApp extends App with GenericSchema[Console with Clock] {
object ExampleApp extends App with GenericSchema[ExampleService] {

implicit val system = ActorSystem()
implicit val executionContext = system.dispatcher
implicit val runtime = Runtime.unsafeFromLayer(Console.live ++ Clock.live)
implicit val runtime = Runtime.default

implicit val roleSchema = gen[Role]
implicit val characterSchema = gen[Character]
Expand All @@ -31,22 +30,22 @@ object ExampleApp extends App with GenericSchema[Console with Clock] {

case class Queries(
@GQLDescription("Return all characters from a given origin")
characters: CharactersArgs => URIO[Console, List[Character]],
characters: CharactersArgs => URIO[ExampleService, List[Character]],
@GQLDeprecated("Use `characters`")
character: CharacterArgs => URIO[Console, Option[Character]]
character: CharacterArgs => URIO[ExampleService, Option[Character]]
)
case class Mutations(deleteCharacter: CharacterArgs => URIO[Console, Boolean])
case class Subscriptions(characterDeleted: ZStream[Console, Nothing, String])
case class Mutations(deleteCharacter: CharacterArgs => URIO[ExampleService, Boolean])
case class Subscriptions(characterDeleted: ZStream[ExampleService, Nothing, String])

def makeApi(service: ExampleService): GraphQL[Console with Clock] =
val api =
graphQL(
RootResolver(
Queries(
args => service.getCharacters(args.origin),
args => service.findCharacter(args.name)
args => ExampleService.getCharacters(args.origin),
args => ExampleService.findCharacter(args.name)
),
Mutations(args => service.deleteCharacter(args.name)),
Subscriptions(service.deletedEvents)
Mutations(args => ExampleService.deleteCharacter(args.name)),
Subscriptions(ExampleService.deletedEvents)
)
) @@
maxFields(200) @@ // query analyzer that limit query fields
Expand All @@ -55,8 +54,7 @@ object ExampleApp extends App with GenericSchema[Console with Clock] {
printSlowQueries(500 millis) @@ // wrapper that logs slow queries
apolloTracing // wrapper for https://github.com/apollographql/apollo-tracing

val service = runtime.unsafeRun(ExampleService.make(sampleCharacters))
val interpreter = runtime.unsafeRun(makeApi(service).interpreter)
val interpreter = runtime.unsafeRun(api.interpreter).provideCustomLayer(ExampleService.make(sampleCharacters))

/**
* curl -X POST \
Expand Down
34 changes: 17 additions & 17 deletions examples/src/main/scala/caliban/finch/ExampleApp.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package caliban.finch

import scala.language.postfixOps
import caliban.ExampleData.{ sampleCharacters, Character, CharacterArgs, CharactersArgs, Role }
import caliban.ExampleService.ExampleService
import caliban.GraphQL.graphQL
import caliban.schema.Annotations.{ GQLDeprecated, GQLDescription }
import caliban.schema.GenericSchema
import caliban.{ ExampleService, FinchAdapter, GraphQL, RootResolver }
import caliban.{ ExampleService, FinchAdapter, RootResolver }
import com.twitter.io.{ Buf, BufReader, Reader }
import com.twitter.util.Await
import io.circe.Json
import io.finch.Endpoint
import zio.clock.Clock
import zio.console.Console
import zio.interop.catz._
import zio.stream.ZStream
import zio.{ Runtime, Task, URIO }
import scala.language.postfixOps

object ExampleApp extends App with GenericSchema[Console with Clock] with Endpoint.Module[Task] {
object ExampleApp extends App with GenericSchema[ExampleService] with Endpoint.Module[Task] {

implicit val runtime = Runtime.unsafeFromLayer(Console.live ++ Clock.live)
implicit val runtime = Runtime.default

implicit val roleSchema = gen[Role]
implicit val characterSchema = gen[Character]
Expand All @@ -27,26 +26,26 @@ object ExampleApp extends App with GenericSchema[Console with Clock] with Endpoi

case class Queries(
@GQLDescription("Return all characters from a given origin")
characters: CharactersArgs => URIO[Console, List[Character]],
characters: CharactersArgs => URIO[ExampleService, List[Character]],
@GQLDeprecated("Use `characters`")
character: CharacterArgs => URIO[Console, Option[Character]]
character: CharacterArgs => URIO[ExampleService, Option[Character]]
)
case class Mutations(deleteCharacter: CharacterArgs => URIO[Console, Boolean])
case class Subscriptions(characterDeleted: ZStream[Console, Nothing, String])
case class Mutations(deleteCharacter: CharacterArgs => URIO[ExampleService, Boolean])
case class Subscriptions(characterDeleted: ZStream[ExampleService, Nothing, String])

import caliban.wrappers.ApolloTracing.apolloTracing
import caliban.wrappers.Wrappers._
import zio.duration._

def makeApi(service: ExampleService): GraphQL[Console with Clock] =
val api =
graphQL(
RootResolver(
Queries(
args => service.getCharacters(args.origin),
args => service.findCharacter(args.name)
args => ExampleService.getCharacters(args.origin),
args => ExampleService.findCharacter(args.name)
),
Mutations(args => service.deleteCharacter(args.name)),
Subscriptions(service.deletedEvents)
Mutations(args => ExampleService.deleteCharacter(args.name)),
Subscriptions(ExampleService.deletedEvents)
)
) @@
maxFields(200) @@ // query analyzer that limit query fields
Expand All @@ -55,8 +54,9 @@ object ExampleApp extends App with GenericSchema[Console with Clock] with Endpoi
printSlowQueries(500 millis) @@ // wrapper that logs slow queries
apolloTracing // wrapper for https://github.com/apollographql/apollo-tracing

val service = runtime.unsafeRun(ExampleService.make(sampleCharacters))
val interpreter = runtime.unsafeRun(makeApi(service).interpreter)
val interpreter = runtime
.unsafeRun(api.interpreter)
.provideCustomLayer(ExampleService.make(sampleCharacters))

/**
* curl -X POST \
Expand Down
Loading

0 comments on commit 7b17d21

Please sign in to comment.