FSharp.Data.GraphQL icon indicating copy to clipboard operation
FSharp.Data.GraphQL copied to clipboard

Incorrect check for null when the function "buildResolverTree" is executed

Open OniVe opened this issue 6 years ago • 3 comments

Description

An error occurs when trying to get a non-existent value: Object reference not set to an instance of an object.

Repro steps

  1. 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 avatar May 08 '18 14:05 OniVe

@OniVe how does your server-side resolver logic looks like?

Horusiath avatar May 08 '18 18:05 Horusiath

@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

OniVe avatar May 09 '18 02:05 OniVe

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?

ivelten avatar Jun 06 '18 19:06 ivelten