Beska
Beska

Reputation: 12667

View pre-deserialized response from GraphQL query

I have a simple GraphQL query that I'm making out to a server, and trying to use GraphQL.Client.Serializer.Newtonsoft to deserialize it. I'm getting a response, but the response does not make the NewtonsoftJsonSerializer happy. It complains:

This converter can only parse when the root element is a JSON Object.

Okay, fair enough. The response must not be what was expected. But how can I view the response to see what may be wrong? The GraphQLHttpClient.SendQueryAsync<>() method requires a type to try to deserialize to...which is the part that is failing. I don't see a way to just get the text response so I can see what is wrong with it.

I've included sample code at the bottom, but the only real line of interest (I believe) is the last one:

var graphQLResponse = await graphQLClient.SendQueryAsync<object>(theRequest);

Is there some way to just get the text of the response?

var graphQLClient = new GraphQLHttpClient("https://<TheDomain>.com/graphql/api/v1", new NewtonsoftJsonSerializer());

graphQLClient.HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer <MyKey>");
graphQLClient.HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");

var theRequest = new GraphQLRequest
{
    Query = "{ __schema { queryType { fields { name } } } }"
};

var graphQLResponse = await graphQLClient.SendQueryAsync<object>(theRequest);

Using Fiddler Everywhere, I was able to pull in the request and response, both of which seem superficially valid (I updated the query in the question to match the current query I'm passing in...a general schema query)

Request:

{
  "query": "{ __schema { queryType { fields { name } } } }"
}

Response: (I've redacted the names of the endpoints and removed much of the repetition from the middle of the response. But the rest is unchanged, and looks superficially okay to me...in particular, it looks like valid JSON, so I'm unclear why the converter is complaining.)

{
  "errors": [],
  "data": {
    "__schema": {
      "queryType": {
        "fields": [
          {
            "name": "getData1"
          },
          {
            "name": "getData2"
          },
          ...
          <a bunch more here>
          ...
          {
            "name": "getData100"
          }
        ]
      }
    }
  },
  "extensions": null,
  "dataPresent": true
}

Upvotes: 1

Views: 2603

Answers (2)

PSanetra
PSanetra

Reputation: 524

I debugged the GraphQL.Client.Serializer.Newtonsoft.NewtonsoftJsonSerializer and discovered it throws when the extensions property (or generally any property of type GraphQL.Map) of a GraphQL response is null. I think this is an implementation bug.

Related GitHub issue: https://github.com/graphql-dotnet/graphql-client/issues/601

Upvotes: 3

Rahul Sharma
Rahul Sharma

Reputation: 8312

After a few hit and trials and discussion with the O.P, it was found that while initializing GraphQLHttpClient with GraphQL.Client.Serializer.Newtonsoft, the serializer is not correctly doing the deserialization of the response despite the response being a valid JSON string.

After switching to GraphQL.Client.Serializer.SystemTextJson, the response is being correctly parsed as expected which suggests that there could be a bug in the GraphQL.Client.Serializer.Newtonsoft serializer

Upvotes: 2

Related Questions