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

add minimal support for resolve function returning IO #987

Merged
merged 1 commit into from
Mar 31, 2023
Merged

Conversation

yanns
Copy link
Contributor

@yanns yanns commented Mar 31, 2023

Add experimental support for cats effect IO in a new experimental module.
The base classes are depending on Async, so it should be possible to support any effect library compatible with it.

The support is very minimal as the Async will be converted to a standard scala Future for the moment.

How to use it:

first import the library:

libraryDependencies += "org.sangria-graphql" %% "sangria-cats-effect-experimental" % "<latest version>"

to use IO in resolve functions:

import sangria.catseffect.schema.AsyncValue._
private val QueryType: ObjectType[Unit, Unit] = ObjectType(
  "Query",
  () =>
    fields[Unit, Unit](
      Field("parent", StringType, resolve = _ => IO("hello"))
    ))

to get an IO when executing a query:

import sangria.catseffect.execution.IOExecutionScheme._
val query =
  gql"""
    query q1 {
      parent
    }
  """
val res: IO[Json] = Executor.execute(schema, query)

FAQ

What happens if I don't use the same effect type in resolve and in the execution scheme?

This will fail miserably.
Making this type safe will probably never be implemented. I've tried, and the code is impacted at too many places.

What's the point of this change if we are still relying on Future under the hood?

  • This allows to check how the API could look like.
  • It opens the possibility to implement a Resolver without Future in a later phase.
  • And for some use-cases, having one conversion is likely not being an issue.

When will we have a Resolver totally implemented with Async?

I don't know. If you want to join the party, you're welcome.

Does this change also provide better support for fs2 streams?

Not yet. I first focus on IO.

Why is the code in the sangria main library and not in a separated code base?

I have to change the structure of the code of sangria to enable this integration:

Those changes are easier if the implementations live in the same code base for the moment. Once this is stable, we can extract it into another library.

Is sangria depending on cats-effect now?

No, sangria does not have any dependency on cats-effect. Only the new library has this dependency.

@yanns yanns force-pushed the IO_action branch 7 times, most recently from 7290d32 to ef5858c Compare March 31, 2023 13:46
@yanns yanns marked this pull request as ready for review March 31, 2023 14:20
@yanns yanns added this pull request to the merge queue Mar 31, 2023
Merged via the queue into main with commit 7898f17 Mar 31, 2023
@yanns yanns deleted the IO_action branch March 31, 2023 14:27
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.

1 participant