Skip to content

try to reproduce issue #1041#1156

Draft
yanns wants to merge 2 commits into
mainfrom
issue_1041
Draft

try to reproduce issue #1041#1156
yanns wants to merge 2 commits into
mainfrom
issue_1041

Conversation

@yanns

@yanns yanns commented Nov 7, 2024

Copy link
Copy Markdown
Contributor

No description provided.

@roman0x58

Copy link
Copy Markdown

@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

yanns commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown

@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