Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runService function and error handling small refactor #33

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,35 @@ object GraphQLApi extends GenericSchema[Any] {
item: ItemId => Task[Option[Item]]
)

def adaptError(e: DomainError): Throwable = e match {
case RepositoryError(cause) => cause
case ValidationError(msg) => new Exception(msg)
}

case class Mutations(
addItem: ItemInput => Task[ItemId],
deleteItem: ItemId => Task[Unit])

def api(repository: ItemRepository): GraphQL[Console with Clock] =
def api(repository: ItemRepository): GraphQL[Console with Clock] = {

def runService[A](f: ZIO[ItemRepository, DomainError, A]): Task[A] =
f.provide(repository).mapError(_.asThrowable)

graphQL(
RootResolver(
Queries(
args => ApplicationService.getItemByName(args.name).mapError(adaptError).provide(repository),
args => ApplicationService.getItemsCheaperThan(args.price).mapError(adaptError).provide(repository),
ApplicationService.getItems.mapError(adaptError).provide(repository),
id => ApplicationService.getItem(id).mapError(adaptError).provide(repository)
args => runService(ApplicationService.getItemByName(args.name)),
args => runService(ApplicationService.getItemsCheaperThan(args.price)),
runService(ApplicationService.getItems),
id => runService(ApplicationService.getItem(id))
),
Mutations(
item => ApplicationService.addItem(item.name, item.price).mapError(_.asThrowable).provide(repository),
id => ApplicationService.deleteItem(id).mapError(e => new Throwable(""))).provide(repository)
item => runService(ApplicationService.addItem(item.name, item.price)),
id => runService(ApplicationService.deleteItem(id))
)
)
) @@
maxFields(200) @@ // query analyzer that limit query fields
maxDepth(30) @@ // query analyzer that limit query depth
timeout(3 seconds) @@ // wrapper that fails slow queries
printSlowQueries(500 millis) @@ // wrapper that logs slow queries
apolloTracing // wrapper for https://github.com/apollographql/apollo-tracing
}

val live: ZLayer[ItemRepository with Has[ActorSystem], CalibanError.ValidationError, GraphQLApi] =
ZLayer.fromFunctionM { env =>
Expand Down