有人能帮助我获得一些示例代码,并举例说明AutoRegisteringObjectGraphType与asp.net核心项目的使用情况吗?我没有找到任何有文件的具体例子。
在这方面的任何帮助都是非常感谢的。
发布于 2021-12-15 16:14:22
下面是一个自动注册输入图类型的示例:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Field<StringGraphType>("addPerson",
arguments: new QueryArguments(
new QueryArgument<AutoRegisteringInputObjectGraphType<Person>> { Name = "value" }
),
resolve: context => {
var person = context.GetArgument<Person>("value");
db.Add(person);
return "ok";
});下面是一个自动注册对象图类型的示例,它修改了一些字段:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime LastUpdated { get; set; }
}
class ProductGraphType : AutoRegisteringObjectGraphType<Product>
{
public ProductGraphType()
: base(x => x.LastUpdated)
{
GetField("Name").Description = "A short name of the product";
}
}
Field<ListGraphType<ProductGraphType>>("products", resolve: _ => db.Products);请注意,您可能需要在依赖项注入框架内注册这些类:
services.AddSingleton<EnumerationGraphType<Episodes>>();
services.AddSingleton<AutoRegisteringInputGraphType<Person>>();
services.AddSingleton<ProductGraphType>();或者,您可以注册打开的泛型类:
services.AddSingleton(typeof(AutoRegisteringInputGraphType<>));
services.AddSingleton(typeof(AutoRegisteringObjectGraphType<>));
services.AddSingleton(typeof(EnumerationGraphType<>));在上面的示例中,仍然需要单独注册ProductGraphType。
文档中的示例链接:https://github.com/graphql-dotnet/graphql-dotnet/blob/master/docs2/site/docs/guides/known-issues.md
https://stackoverflow.com/questions/62522682
复制相似问题