c# - Dotnet 使用 graphQL 客户端,数据响应未映射

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

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

当我执行“SendQueryAsync()”或“SendMutationAsync()”时,响应状态代码正常,但数据属性始终为 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; }
}

postman 的回应

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

最佳答案

我通过删除 Rootobject 类并使用 Data 类作为根解决了这个问题。我认为响应始终具有数据属性,因此它会在反序列化中跳过该属性。

关于c# - Dotnet 使用 graphQL 客户端,数据响应未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65794056/

相关文章:

c# - SendMessage 消息编号的解释?

C# : how can i switch between my two network interface

c# - "VsTsc"任务无法用其输入 > 参数初始化

c# - 如何在 View 之外使用 ASP.NET Core 生成完整的 URL?

c# - C# 中的蓝牙支持

c# - 每次使用 "Not Allowed"使用 ASP Identity 登录失败(即使 'email' 和 'username' 具有相同的值))

node.js - 如何在prisma-binding npm中获得总匹配记录数

python - 使用带有 graphql 查询的 API 在 R 上抛出 401 错误,但在 Python 上工作正常

asp.net - GraphQL.NET 如何将枚举添加到 GraphQL 类型

c# - Vlc.DotNet - 在播放音频之前无法设置音量