ChilliCream / ChilliCream/graphql-platform

Support F# Async<_> expressions

Open
#7,023 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🌶️ hot chocolate Area: F#
Dominant language
C#
Stars
5.8k
Forks
810
Avg merge
15h 39m
Merged PRs (30d)
98

Description

Product

Hot Chocolate

I think this request is conceptually fairly simple - it just requires a trivial wrapper.

As we all know, HotChocolate has built-in support for Task<_>. The following works fine:

type Query() =

    member _.GetFoo([<Service>] fooRepo: FooRepository, cancellationToken) : Task<Foo> =
        task {
            let! foo = fooRepo.GetFoo(cancellationToken)
            return foo
        }

However, in F#, we generally use Async<_> instead of Task<_>. This has several benefits, such as implicit cancellation token passing, better composability, etc. The following is how I ideally would like to write this, but it does not work, since HotChocolate does not know about Async:

type Query() =

    member _.GetFoo([<Service>] fooRepo: FooRepository) : Async<Foo> =
        async {
            let! foo = fooRepo.GetFoo()
            return foo
        }

However, simply by wrapping the async computation in Async.StartImmediateAsTask (and passing the cancellation token from HotChocolate), it works as expected:

    member _.GetFoo([<Service>] fooRepo: FooRepository, cancellationToken) : Task<Foo> =
        let comp =
            async {
                let! foo = fooRepo.GetFoo()
                return foo
            }

        Async.StartImmediateAsTask(comp, cancellationToken)

This is a simple workaround, but it's a drag to have to do in it every async resolver. I would like HotChocolate (possibly in HotChocolate.Types.FSharp or another F#-oriented package) to do this for me. That way, the middle of the three code snippets above would work out of the box.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining Hot Chocolate's existing Task<> resolver support and the proposed HotChocolate.Types.FSharp integration point. Confirm how an F# Async<> resolver could be recognized and cancellation passed through, then add coverage showing that the Async<> form works like the Task<> example.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.