ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.26k stars 746 forks source link

How to correctly return null values from a resolver? #603

Closed eduleite closed 5 years ago

eduleite commented 5 years ago

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?

michaelstaib commented 5 years ago

can you paste you code?

eduleite commented 5 years ago

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

eduleite commented 5 years ago

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
    }
  }
}
eduleite commented 5 years ago

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.

michaelstaib commented 5 years ago

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

michaelstaib commented 5 years ago

I might create a custom error for that

michaelstaib commented 5 years ago

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

michaelstaib commented 5 years ago

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.