Hotchocolate: How to correctly return null values from a resolver?

Created on 27 Feb 2019  路  8Comments  路  Source: ChilliCream/hotchocolate

I have a resolver that may return a null value, and the corresponding schema definition allows to return null values, but my client receives an error showing the following message: "Object reference not set to an instance of an object".
The data field contains the right data, but the error stills comes, even if null is a allowed response.
Is there a correct way to setup de server to not throw this error?

bug

All 8 comments

can you paste you code?

I will try to replicate it in a small sample and post here.

I could not replicate the error in a small sample, but I can paste the stack trace here:
The response I receive is this one:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 10,
          "column": 6
        }
      ],
      "path": [
        "estimativa",
        "cliente"
      ],
      "extensions": {
        "message": "Object reference not set to an instance of an object.",
        "stackTrace": "   at HotChocolate.Resolvers.CodeGeneration.___CompiledResolvers__9163361aef8544be859a761587d56374.<>c.<<-cctor>b__60_21>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at HotChocolate.Configuration.ResolverRegistry.<>c__DisplayClass21_0.<<CreateResolverMiddleware>b__0>d.MoveNext() in C:\\hc\\src\\Core\\Types\\Configuration\\ResolverRegistry.cs:line 228\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at HotChocolate.Execution.ExecutionStrategyBase.ExecuteFieldMiddlewareAsync(ResolverTask resolverTask) in C:\\hc\\src\\Core\\Core\\Execution\\ExecutionStrategyBase.Resolver.cs:line 97\r\n   at HotChocolate.Execution.ExecutionStrategyBase.ExecuteMiddlewareAsync(ResolverTask resolverTask, IErrorHandler errorHandler) in C:\\hc\\src\\Core\\Core\\Execution\\ExecutionStrategyBase.Resolver.cs:line 54"
      }
    }
  ],
  "data": {
    "estimativa": {
      "id": "xxxxx",
      "codigo": "xxxxxx",
      "responsavel": {
        "nome": "xxxxxx",
      },
      "cliente": null
    }
  }
}

Oh, I found the problem :)
This is the method:

        public Task<IEmpregado> GetCliente(IEstimativa source, [Service] IForcaTrabalhoService forcaTrabalhoService)
        {
            if (source.Cliente != null)
            {
                return forcaTrabalhoService.ObterPorChaveAsync(source.Cliente);
            }
            return null;
        }

I should be returning Task.FromResult(null), because my method is not declared as async.
Lesson learned :)
Closing the issue. Unless you think the error message should be better here.

I will look into that since others could fall into that trap.

I might create a custom error for that

A fix for this one will be included into 0.8.0-preview.11

This one is now fixed. Since tasks will in the end be awaited by the query engine we are now returning
null if the resolver task is null.

Was this page helpful?
0 / 5 - 0 ratings