【发布时间】:2020-06-21 13:31:51
【问题描述】:
我正在使用 Web API 将 Hot Chocolate(GraphQL) 添加到现有的 ASP.Net Core 项目中,并重用 Web API 使用的模型。
其中一个模型具有 IDictionary
模型
public class Theme
{
...
public IDictionary<string, object> Styles { get; set; }
...
}
用Web API可以序列化为
{
...
styles: {
header: {
color: "#fff",
background: "rgba(255, 255, 255, 0)",
boxShadow: "", position: "fixed"},
...
},
borderRadius: "4px",
...
}
...
}
使用 Hot Chocolate,我正在重用模型并在启动时添加 GraphQL
services.AddGraphQL(sp => SchemaBuilder.New()
.AddServices(sp)
.AddQueryType(d => d.Name("Query"))
...
.AddType<Theme>()
.Create());
生成的架构变成
type Theme {
...
styles: [KeyValuePairOfStringAndObject!]
...
}
只能检索密钥
{
themes(...) {
edges {
node {
name
styles{
key
}
}
}
}
}
回复如下
{
"themes": {
"edges": [
{
"node": {
...
"styles": [
{
"key": "header"
},
{
"key": "borderRadius"
},
...
]
}
}
]
}
}
我想设置 Hot Chocolate 以使编写 GraphQL 查询成为可能,该查询提供与 Web API 相同的动态结果。
更新
源是一个 JSON 字符串,它被反序列化为 Dictionary
var jsonString = "{\"header\":{\"color\":\"#fff\"},\"borderRadius\":\"4px\"}";
theme.Styles = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
问题是生成的字典没有递归序列化为 Dictionary
【问题讨论】:
标签: asp.net graphql hotchocolate