GraphQL Client is retuning null data in C#

2.1k views Asked by At

I have a query defined at server side as

public TradeQuery(ITradeService tradeservice)
    {
        Name = nameof(TradeQuery);
        Field<ListGraphType<TradeType>>("trades", resolve: r => { return tradeservice.GetTrades(); });
    }

when I call if from GraphiQL client I am getting proper response. Request from GraphiQL is

{
  tradeQuery {
    trades {
      id
      price
      quantity
     
    }
  }
}

and getting response as

{"data": {
    "tradeQuery": {
      "trades": [
        {
          "id": 3,
          "price": 600,
          "quantity": 600
        },
        {
          "id": 4,
          "price": 100,
          "quantity": 100
        }]}}}

But When I tried to call same query using GraphQLHttpClient it is returning as null In below code I am getting null values in availableTrades object.

 string apiEndpoint = "https://localhost:44342/graphql";

            var graphQLClient = new GraphQLHttpClient(new GraphQLHttpClientOptions { 
                EndPoint = new Uri (apiEndpoint)
            }, new NewtonsoftJsonSerializer());

            var tradeQuery = @"{
                              tradeQuery{
                               trades {
                                id
                                price
                                quantity}}}";
            GraphQLRequest graphQLRequest = new GraphQLRequest(tradeQuery);
            var tradeQueryResponse = await graphQLClient.SendQueryAsync<TradeQuery>(graphQLRequest);
            var availableTrades = tradeQueryResponse.Data.Trades;

Response type TradeQuery object defined as below

 public class TradeQuery
        {
            public List<Trade> Trades { get; set; }
        }

Can you please guide what could be wrong?

1

There are 1 answers

2
KyoD On

Incase anyone else is having this issue, I spent hours today trying all different combinations of deserializing the result from the query and using dynamic was the only way to return anything. I eventually found a fix by doing the following.

Prerequisites

You will need two nuget packages for this to work correctly:

  1. GraphQL.Client.Abstractions
  2. GraphQL.Client.Serializer.SystemTextJson;

Instead of using new GraphQLHttpClient(apiEndpoint, new NewtonsoftJsonSerializer());

Use new GraphQLHttpClient(apiEndpoint, new SystemTextJsonSerializer());

Query Format

You will need to format your query by giving it an alias like I have done here. I have a query called fakeLogin but by prepending data: I have now giving the result of this query an alias of data.

GraphQLRequest loginRequest = new GraphQLRequest
{               
    Query = @"
    {
        data:fakeLogin
        {
            token
        }
    }"
};

Sending Query and Consuming result

Now that your request is created you now need to send the query in the following format:

LoginToken responseData = _graphQlClient.SendQueryAsync(loginRequest, () => new { Data = new LoginToken()}).Result.Data.Data;