How to do error handling for GraphQL server using FSharp.Data.GraphQL in F#?

Viewed 84

I am using FSharp.Data.GraphQL for implementing GraphQL server. The documentation is not very clear about error handling. In the following mutation resolver, I wish to return errors if user provides invalid values for email and password:

let login =
    Define.AsyncField(
        name = "login",
        typedef = Nullable Output.Token,
        description = "Login using email and password",
        args = [
            Define.Input("email", String)
            Define.Input("password", String)
        ],
        resolve =
            fun ctx _ ->
                
                // ... some more code related to db and validation

                let customErr: exn = Error.InvalidUserPassword

                // This statement has not effect
                ctx.AddError customErr

                // Should I use raise to explicitly throw exception
                // raise customErr

                Async.result None
    )

Looking into the source code, I found out AddError method on ResolveFieldContext. But it seems to produce no effect on the final result of the mutation. The output is always without any error:

{
    "documentId": 1234,
    "data": {
        "login": null
    },
    
    // The errors array is not available
    "errors": []
}

Should I raise the exception to add the error handling? And, if that is the case, how can I go about adding multiple error codes as part of single query or mutation since I can raise only one exception?

0 Answers
Related