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

Scala 3: Exception returning Future values from GraphQL Endpoint #906

Open
PsyfireX opened this issue Sep 5, 2022 · 5 comments
Open

Scala 3: Exception returning Future values from GraphQL Endpoint #906

PsyfireX opened this issue Sep 5, 2022 · 5 comments
Labels

Comments

@PsyfireX
Copy link

PsyfireX commented Sep 5, 2022

When attempting to return values from a GraphQL endpoint using Sangria and Scala 3, I get the following exception:

    @GraphQLField
    def ping(in: String): Future[String] = {
      implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
      Future.apply(in)
    }
"class scala.concurrent.impl.Promise$Transformation cannot be cast to class java.lang.String (scala.concurrent.impl.Promise$Transformation is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap') 
 java.lang.ClassCastException: class scala.concurrent.impl.Promise$Transformation cannot be cast to class java.lang.String (scala.concurrent.impl.Promise$Transformation is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap') 
 sangria.execution.Resolver.resolveValue(Resolver.scala:1274) 
 sangria.execution.Resolver.$anonfun$20(Resolver.scala:813) 
 scala.collection.immutable.Vector1.map(Vector.scala:1872) 
 scala.collection.immutable.Vector1.map(Vector.scala:375) 
 sangria.execution.Resolver.resolveActionsPar(Resolver.scala:1113) 
 sangria.execution.Resolver.resolveFieldsPar(Resolver.scala:49) 
 sangria.execution.Executor.executeOperation(Executor.scala:275) 
 sangria.execution.Executor.$anonfun$5$$anonfun$2$$anonfun$1$$anonfun$1$$anonfun$1(Executor.scala:206) 
 scala.concurrent.impl.Promise$Transformation.run(Promise.scala:470) 
 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 
 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 
 java.base/java.lang.Thread.run(Thread.java:834)"

I am able to successfully get a schema from the running GraphQL server.

Since this particular error relies on having a running GraphQL server, it'll be a little difficult for me to create a miniature isolated code sample. I might make an attempt at creating an isolated sample, but that would take some time. If you cannot reproduce, let me know here on the ticket. (I'll try to monitor the ticket).


It appears to only happen when returning a Future. The following returns just fine.

    @GraphQLField
    def ping2(in: String): String = {
      in
    }
@yanns yanns added the scala3 label Sep 5, 2022
@PsyfireX PsyfireX changed the title Scala 3: Exception returning values from GraphQL Endpoint Scala 3: Exception returning Future values from GraphQL Endpoint Sep 6, 2022
@muuki88
Copy link

muuki88 commented Nov 30, 2022

Error messages sound aweful familiar to #449

@mprevel
Copy link

mprevel commented Jan 2, 2025

Hi,

Is there a known cause or a workaround for this issue? I guess I'm encountering it on a project that I am upgrading to Scala 3.
The issue seems to appear when the returned value is inside a Future (returning the String directly is OK).

On a case class body I add:

  @GraphQLField
  @GraphQLName("myString")
  def graphqlMyString: Future[String] = Future.successful("Hello World")

A unit test breaks with:

java.lang.ClassCastException: class scala.concurrent.impl.Promise$DefaultPromise cannot be cast to class java.lang.String (scala.concurrent.impl.Promise$DefaultPromise is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
      at sangria.execution.FutureResolver.resolveValue(FutureResolver.scala:1423)
      at sangria.execution.FutureResolver.resolveValue(FutureResolver.scala:960)
      at sangria.execution.FutureResolver.resolveStandardLeafAction(FutureResolver.scala:925)
      at sangria.execution.FutureResolver.resolveLeafAction(FutureResolver.scala:903)
      at sangria.execution.FutureResolver.$anonfun$20(FutureResolver.scala:831)
      at scala.collection.immutable.Vector1.map(Vector.scala:2140)
      at scala.collection.immutable.Vector1.map(Vector.scala:385)
      at sangria.execution.FutureResolver.resolveActionsPar(FutureResolver.scala:827)
      at sangria.execution.FutureResolver.resolveValue(FutureResolver.scala:1482)
      at sangria.execution.FutureResolver.$anonfun$28(FutureResolver.scala:1156)
      at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:467)
      at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1395)
      at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
      at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
      at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
      at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
      at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

@yanns
Copy link
Contributor

yanns commented Jan 2, 2025

I'm not aware of any fixes for this issue for now.
Would you want to invest some time on it?
You could first open a PR with a test reproducing the issue.
Then we could discuss how to fix this.

@robberthcz
Copy link

I encountered the same error today while migrating our project to Scala 3.

@yanns
Copy link
Contributor

yanns commented Jan 7, 2025

I'd encourage all people having this issue to try helping on the PR #1168

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

No branches or pull requests

5 participants