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

Effect handler errors #692

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
62 changes: 62 additions & 0 deletions modules/circe/src/test/scala/CirceEffectHandlerErrorData.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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](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))
}

}
Loading