FSharp.Data.GraphQL
FSharp.Data.GraphQL copied to clipboard
Incorrect check for null when the function "buildResolverTree" is executed
Description
An error occurs when trying to get a non-existent value: Object reference not set to an instance of an object.
Repro steps
- Run the query for project "FSharp.Data.GraphQL.Samples.GiraffeServer":
{
hero(id: "wrongId") {
id
}
}
Expected behavior
Query result:
{
"data": {
"hero": null
}
}
Actual behavior
Query result:
{
"data": {
"hero": null
},
"errors": [{
"message": "Object reference not set to an instance of an object.",
"path": [
"hero",
"id"
]
}
]
}
let result = toOption null
// result is -> Some(null)
Known workarounds
https://github.com/fsprojects/FSharp.Data.GraphQL/blob/e38f08ec3c8cc1731adcf5e9222071c472e1736c/src/FSharp.Data.GraphQL.Server/Execution.fs#L230-L234
let private toOption x =
match x with
| null -> None // Check for null before the active patterns
| SomeObj(v)
| v -> Some(v)
@OniVe how does your server-side resolver logic looks like?
@Horusiath I just tested the FSharp.Data.GraphQL.Samples.GiraffeServer
project and got the error in this place:
https://github.com/fsprojects/FSharp.Data.GraphQL/blob/e38f08ec3c8cc1731adcf5e9222071c472e1736c/src/FSharp.Data.GraphQL.Samples.GiraffeServer/Schema.fs#L145
I believe that's a good fix for it. toOption
is used only on fieldResolvers, so it would work adding that matching there. What do you think @Horusiath and @johnberzy-bazinga?