graphql-dotnet / graphql-client

A GraphQL Client for .NET Standard
MIT License
623 stars 132 forks source link

Why I do not receive a real object? #362

Open ZedZipDev opened 3 years ago

ZedZipDev commented 3 years ago

I run a GraphQL client and send the queries

            var response3 = await client2.SendQueryAsync<ADescriptorResponse2>(request2);
            var response2 = await client2.SendQueryAsync<dynamic>(request2);
            var response1 = await client2.SendQueryAsync<object>(request2);

There is

    public class ADescriptorResponse2
    {
        public ADescriptor aDescriptor { get; set; }
    }
    public class ADescriptor
    {
        public long  Action { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

But in the 1st case var response3 = await client2.SendQueryAsync(request2); i receive the response3.Data ==null, type ADescriptorResponse2 In other both cases I receive Data correct but it isNewtonsoft.Json.Linq.JObject type. Why I do not receive the Data of ADescriptorResponse2 filled with data? What is incorrect?

rose-a commented 3 years ago

Please post your query string... I suspect you're missing the root data object...

ErlendFax commented 2 years ago

I'm having the same problem here. What do you mean by root data object in the query string @rose-a?

rose-a commented 2 years ago

The client deserializes the data field in the GraphQL response, not the first element mentioned in the query... a common mistake to make is to forget to wrap the desired object in an object containing an accordingly named field of that type (the ResponseType in the Readme example).

arekucr commented 2 years ago

@rose-a have a similar problem here, if I understand ok, does the client expects a root element "data" from the graphql response from the server?

No matter if I use dynamic object or a object that matches the response, the response.data is always null here, any idea?

image

public class ApiResult { public string ClientIP { get; set; } public string ExecutionTime { get; set; } public string ServerName { get; set; } public AgentApiResult Result { get; set; } public string StatusCode { get; set; } }

public class AgentApiResult
{
    public List<AgentPolicy> agentPolicies { get; set; }
}

public class AgentPolicy
{
    public int builderId { get; set; }
    public string type { get; set; }
    public string level { get; set; }
    public List<Market> markets { get; set; }

    public List<Division> divisions { get; set; }

    public class Market
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class Division
    {
        public int builderId { get; set; }
        public string builderName { get; set; }
    }
}

Response looks like this

{ "StatusCode": "Success", "ServerName": "NHSDEVAPI1", "ClientIP": "172.16.2.251", "ExecutionTime": "00:00:00.3523738", "Result": { "agentPolicies": [ { "builderId": 34, "type": "CoOpConfirmation", "level": "Market", "divisions": [ { "builderId": 38 }, { "builderId": 394 }, { "builderId": 396 }, ], "markets": [ { "id": 18 }, { "id": 19 }, { "id": 21 }, ] } ] } }

arekucr commented 2 years ago

What if the graphql server defined a different format for the response that this case were in our API

image

sungam3r commented 2 years ago

So this server is not compatible with GraphQL official specification.