.net - 输入类型对象可以包含接口类型的属性吗?
问题描述
我才开始在 .net 平台上学习 GraphQL。如果输入类型对象可以包含接口类型的属性,那么如何在 HotChocolate 库中配置它?
模型示例:
public interface ILocationFilter {
double Lat { get; set;}
double Lng { get; set;}
}
public class LocationFilter : ILocationFilter {
// ...
}
public class FilterModel {
public string Search { get; set; }
public ILocationFilter Location { get; set; } = new LocationFilter();
// ...
}
GraphQL 查询示例:
public class Query {
public IEnumerable<SomeModel> GetByFilter(FilterModel filter) {
// ...
}
}
Startup.cs 示例:
// ...
services.AddGraphQL(SchemaBuilder.New()
.AddQueryType<Query>()
.Create(),
new QueryExecutionOptions { IncludeExceptionDetails = true });
// ...
app.UseGrapQL();
// ...
现在我得到一个异常“无法从类型引用 Input: ILocationFilter 推断或解析模式类型”。
顺便说一句:如果删除接口一切都会工作。
我应该配置什么来更正使用接口类型的属性?
解决方案
public class FilterModelType : InputObjectType<FilterModel> {
protected override void Configure(IInputObjectTypeDescriptor<FilterModel> descriptor)
{
descriptor.Field(x => x.Location).Type(typeof(LocationFilter));
}
}
我添加了一个描述 FilterModel 的新类。之后我也在 Startup.cs 中注册了这个类型
SchemaBuilder.New()
.AddType<FilterModelType>()
.AddQueryType<Query>()
.Create()
这个对我有用。
推荐阅读
- java - 如何检查一个单词是否以通用符号结尾和开头并将其替换为 1
- r - 将 3d 矩阵转换为数据框
- android - Android Studio 无法启动守护进程
- bash - bash shell reworking 变量用下划线替换点
- wordpress - 在 wordpress 上定义视口时,页脚搞砸了
- api - Dialogflow Fulfillment 中的 API 请求(Javascript)
- php - 如何在不同的行中插入来自 for / foreach 的数据?
- botframework - 如何将现有的已登录 Web 应用程序用户传递给 Azure Web 聊天机器人,以便只有经过身份验证和授权的用户才能使用 Web 聊天机器人?
- c# - 如何在运行时创建表时查询数据库
- javascript - 通过匹配键/值对重新组合对象数组