Skip to content

Commit

Permalink
Effect handler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
swalker2m committed Jan 9, 2025
1 parent e4fe38a commit 0f6ba7a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
63 changes: 63 additions & 0 deletions modules/circe/src/test/scala/CirceEffectHandlerErrorData.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// Copyright (c) 2016-2023 Grackle Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import cats.effect.Sync
import cats.implicits._
import fs2.concurrent.SignallingRef

import grackle.Cursor
import grackle.Query
import grackle.Query.EffectHandler
import grackle.Result
import grackle.circe.CirceMapping
import grackle.syntax._

class TestCirceEffectHandlerErrorMapping[F[_]: Sync](ref: SignallingRef[F, Int]) extends CirceMapping[F] {
val schema =
schema"""
type Query {
n: Int!
s: String!
}
"""

val QueryType = schema.ref("Query")

// case class TestEffectHandler[A: Encoder](value: A) extends EffectHandler[F] {
case class TestEffectHandler[A](value: A) extends EffectHandler[F] {
def runEffects(queries: List[(Query, Cursor)]): F[Result[List[Cursor]]] =
queries.traverse { case (_, _) =>
ref.update(_ + 1).as(
Result.failure[Cursor](s"value: $value")
)
}.map(_.sequence)
}

val nHandler = TestEffectHandler(42)
val sHandler = TestEffectHandler("hi")

val typeMappings = List(
ObjectMapping(
tpe = QueryType,
fieldMappings =
List(
EffectField("n", nHandler, Nil),
EffectField("s", sHandler, Nil)
)
)
)


}
51 changes: 51 additions & 0 deletions modules/circe/src/test/scala/CirceEffectHandlerErrorSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// Copyright (c) 2016-2023 Grackle Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import cats.effect.IO
import fs2.concurrent.SignallingRef
import io.circe.Json
import io.circe.literal._
import munit.CatsEffectSuite

final class CirceEffectHandlerErrorSuite extends CatsEffectSuite {
test("circe effect handler") {
val query = """
query {
s,
n
}
"""

val expected = json"""
{
"errors" : [
{ "message": "value: hi" },
{ "message": "value: 42" }
]
}
"""

val prg: IO[(Json, Int)] =
for {
ref <- SignallingRef[IO, Int](0)
map = new TestCirceEffectHandlerErrorMapping(ref)
res <- map.compileAndRun(query)
eff <- ref.get
} yield (res, eff)

assertIO(prg, (expected, 2))
}

}

0 comments on commit 0f6ba7a

Please sign in to comment.