GraphQL .NET 中的自定义响应类型

Custom response type in GraphQL .NET

我有一项服务现在将使用 GraphQL API 只是为了 return 图像 URL 用于前端应用程序。

这是我第一次使用 GraphQL,这是我必须使用的查询结构:

var request = new GraphQLRequest
        {
            Query = @"
            query($status: Status = PUBLISHED, $path: String){
                brand (status: $status, where: { path: $path }, first: 1){ 
                    card { 
                        contentItems { 
                            ... on Cartao { 
                                imagemFrente { 
                                    urls
                                } 
                            } 
                        } 
                    } 
                } 
            }",
            Variables = new
            {
                path = "[EXAMPLE-PATH]"
            }
        };

我遇到的问题是我的 class ReturnType 必须如何才能从下面的请求中访问 url 属性?

var graphQLResponse = Task.Run(async () => await graphQLClient.SendQueryAsync<ReturnType>(request));

我只使用 GraphQLClient 包。

运行 你的查询可以在某个地方获取 json 结果,将其复制到剪贴板,然后在 VS 中,Edit/Paste Special/Paste JSON作为 类.

或使用许多 JSON 在线 C# 转换器中的一个,例如 https://json2csharp.com/

这将为您提供可在 SendQueryAsync<> 中使用的 C# 类。