1

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?

Oxygen
  • 831
  • 4
  • 17
  • 42
  • it is working fine in Insomnia. No Auth implementation – Oxygen May 23 '21 at 13:57
  • I think there is a problem in desterilizing json. It worked when I use SendQueryAsync(dynamic). Do you know how can we avoid using dynamic and get it into required object. – Oxygen May 23 '21 at 16:10

1 Answers1

3

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;
KyoD
  • 51
  • 6
  • Only thing that worked in .Net 6 thank you. It isn't very clean though. – Raztuf Oct 27 '22 at 09:26
  • "GraphQL.Client.Abstractions" is definitely needed, without that , the deserialization won't work. also you can use generic type like :await _graphQLClient.SendQueryAsync(query); – ValidfroM Nov 21 '22 at 17:16