【问题标题】:Dotnet using graphQL client, Data response not mappedDotnet 使用 graphQL 客户端,数据响应未映射
【发布时间】:2021-01-19 14:59:17
【问题描述】:

我在 .NET Core 3.1 项目上使用最后一个 GraphQL 客户端 NuGet 包 (3.2.1) 并调用 GraphQL API。

当我执行“SendQueryAsync()”或“SendMutationAsync()”时,响应状态代码正常,但 Data 属性始终为 Null。

我认为这与序列化有关,但我不知道问题出在哪里。

我如何使用它

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);

即使我放置“Rootobject”类,它也是空的。

我的模型

我使用 Visual Studio 上的“将 JSON 粘贴为类”功能从 JSON 结果生成了我的模型。

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; }
}

邮递员的回应

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

}

【问题讨论】:

  • 我遇到了同样的问题,数据属性为空。你找到解决办法了吗?

标签: c# asp.net-core graphql httpclient json-deserialization


【解决方案1】:

我已经通过删除 Rootobject 类并使用 Data 类作为根解决了这个问题。我认为响应总是有一个数据属性,所以它会在反序列化中跳过它。

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 2012-01-17
    • 2018-02-14
    • 1970-01-01
    • 2017-03-19
    • 2019-06-01
    • 2018-07-22
    • 2017-04-20
    • 2020-06-14
    相关资源
    最近更新 更多