Dotnet using graphQL client, Data response not mapped

Viewed 1040

I'm using the last GraphQL client NuGet package (3.2.1) on .NET Core 3.1 project and calling a GraphQL API.

When I do the "SendQueryAsync()" or "SendMutationAsync()" the response status code is OK but the Data property is always Null.

I think it's related to the serialization but idk where is the problem.

How I use it

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

var request = new GraphQLRequest
        {
            Query = @"query CurrentUserCards {
                currentUser {
                    cardsCount
                    cards {
                        name
                        pictureUrl
                        position
                        player {
                            displayName
                        }
                    }
                }
            }"
        };
var data = await graphQLClient.SendQueryAsync<Data>(request);

Even if I put "Rootobject" class it's null.

My model

I generated my model with "Paste JSON as classes" feature on Visual studio, from the JSON result.

public class Rootobject
{
    public Data data { get; set; }
}

public class Data
{
    public Currentuser currentUser { get; set; }
}

public class Currentuser
{
    public int cardsCount { get; set; }
    public Card[] cards { get; set; }
}

public class Card
{
    public string name { get; set; }
    public string pictureUrl { get; set; }
    public string position { get; set; }
    public Player player { get; set; }
}

public class Player
{
    public string displayName { get; set; }
}

Response from Postman

{
"data": {
    "currentUser": {
        "cardsCount": 12,
        "cards": [
            {
                "name": "Henry",
                "pictureUrl": "",
                "position": "Coach",
                "player": {
                    "displayName": "Thierry Henry",
                    
                }
            },
            {
                "name": "Zidane",
                "pictureUrl": "",
                "position": "Coach",
                "player": {
                    "displayName": "Zinedine Zidane",
                }
            }
            ...
        ]
    }
}

}

1 Answers

I have solved this by removing the Rootobject class and use the Data class as root. I think that the response always has a data property so it skips that in the deserialization.

Related