任何关于使用自动注册对象图类型与 GraphQL.net 的好例子



谁能帮我获取一些示例代码,其中包含有关在核心项目中使用 AutoRegisteringObjectGraphType 的示例 asp.net?我没有找到任何带有文档的具体示例。

非常感谢对此的任何帮助。

下面是自动注册输入图形类型的示例:

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。

您可以尝试在此处使用UT进行探索:https://github.com/graphql-dotnet/graphql-dotnet/blob/c3ca5d68a119c126e4bd64bc7ae9faade76e53c7/src/GraphQL.Tests/Types/ComplexGraphTypeTests.cs#L85

文档中的示例链接:https://github.com/graphql-dotnet/graphql-dotnet/blob/master/docs2/site/docs/guides/known-issues.md

最新更新