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

try to reproduce issue #1041 #1156

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

try to reproduce issue #1041 #1156

wants to merge 2 commits into from

Conversation

yanns
Copy link
Contributor

@yanns yanns commented Nov 7, 2024

No description provided.

@roman0x58
Copy link

@yanns I think there should be something like this, it should reproduce the issue:

object IOExecutionSchemeSpec {
  import sangria.catseffect.schema.AsyncValue._

  private def resolve[F[_]: Async](): F[String] = {
    import cats.syntax.functor._
    Async[F].pure(Option("hello")).map {
      case Some(value) => value
      case None => throw new Exception("No value")
    }
  }

  private val QueryType: ObjectType[Unit, Unit] = ObjectType(
    "Query",
    () =>
      fields[Unit, Unit](
        Field("ids", ListType(IntType), resolve = _ => List(1, 2)),
        Field(
          "parent",
          StringType,
          resolve = { _ => resolve[IO]() }
        )
      )
  )

  private val schema = Schema(QueryType)
}

@yanns
Copy link
Contributor Author

yanns commented Nov 8, 2024

Thanks, I can now reproduce the issue.
The solution I had by using low priority implicits sadly does not work.
I don't have any solution for this.

@roman0x58
Copy link

@yanns I played around with this issue a bit, maybe we could use something like this:

  def async[Ctx, Val, Res, Out, F[_]](
                                 name: String,
                                 fieldType: OutputType[Out],
                                 description: Option[String] = None,
                                 arguments: List[Argument[_]] = Nil,
                                 resolve: Context[Ctx, Val] => F[Res],
                                 possibleTypes: => List[PossibleObject[_, _]] = Nil,
                                 tags: List[FieldTag] = Nil,
                                 complexity: Option[(Ctx, Args, Double) => Double] = None,
                                 deprecationReason: Option[String] = None,
                                 astDirectives: Vector[ast.Directive] = Vector.empty
                               )(implicit ev: ValidOutType[Res, Out],
                                 asyncToAction: (Context[Ctx, Val] => F[Res]) => Context[Ctx, Val] => Action[Ctx, Res]): Field[Ctx, Val] =
    Field[Ctx, Val](
      name,
      fieldType,
      description,
      arguments,
      asyncToAction(resolve),
      deprecationReason,
      tags,
      complexity,
      () => possibleTypes.map(_.objectType),
      astDirectives,
      Vector.empty)

and the resolver implicit conversion:

object AsyncResolver {
  implicit def asyncToAction[Ctx, Val, Res, F[_]: Async](resolver: Context[Ctx, Val] => F[Res]): Context[Ctx, Val] => Action[Ctx, Res] = {
    context => AsyncValue(resolver(context))
  }
}

This will require an additional signature for Field, but we’ll avoid the ambiguity issue. Or alternatively, we could simply expose the asyncToAction function to wrap the resolve function explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants