graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Exception for empty response body

Open VikingRedleg opened this issue 3 years ago • 2 comments
trafficstars

Hello,

I am trying to build a client for a very basic GraphQL request. This works for most of the response types, but the server sends 204 and 504 responses for specific scenarios, along with an empty response body. When this happens I get a NullReferenceException with the following stacktrace:

   at GraphQL.Client.Http.GraphQLHttpResponse`1..ctor(GraphQLResponse`1 response, HttpResponseHeaders responseHeaders, HttpStatusCode statusCode)
   at GraphQL.Client.Http.GraphQLResponseExtensions.ToGraphQLHttpResponse[T](GraphQLResponse`1 response, HttpResponseHeaders responseHeaders, HttpStatusCode statusCode)
   at GraphQL.Client.Http.GraphQLHttpClient.<SendHttpRequestAsync>d__28`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at GraphQL.Client.Http.GraphQLHttpClient.<SendQueryAsync>d__23`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Application.Providers.Services.GqlApi.<GetGqlResponseAsync>d__18.MoveNext() in D:\tfs\codebase\..\GqlApi.cs:line 97

The normal response is some JSON object. Is there any way to just get back the status and null for the object data?

VikingRedleg avatar Jan 31 '22 19:01 VikingRedleg

I also experienced such NullReferenceException when the body is empty, even if the response is 200.

In my case, the empty body occurred only in a .Net full framework application. On .Net core or later, it works fine. The reason is that in full framework, the HttpClient add by default the header "Expect: 100-continue" in POST requests. In .Net core, this header is not added. This can be solved using :

httpClient.DefaultRequestHeaders.ExpectContinue = false;

I can pass my own HttpClient to GraphQLHttpClient but for consistency, it would be nice to add that line in the GraphQLHttpClient constructor...

LoOnGitH avatar Feb 23 '22 15:02 LoOnGitH

I ended up being able to resolve this by catching the exception. I am not in .NET Core. It was a bit of a journey though, perhaps I will share the specific catch block at some point soon.

VikingRedleg avatar Feb 23 '22 17:02 VikingRedleg