gpt4 book ai didi

asp.net - 有没有办法在 GraphQL 类型上默认忽略所有字段并只添加想要的字段?

转载 作者:行者123 更新时间:2023-12-04 14:44:20 26 4
gpt4 key购买 nike

有没有办法在 GraphQL 类型上默认忽略所有字段并只添加想要的字段?
Hot Chocolate 自动从 C# 类型推断出 GraphQL 类型成员。
这意味着以下代码...

public class Foo
{
public string Bar { get; set; }

public string? Baz { get; set; }
}
public class FooType : ObjectType<Foo>
{
}
将导致以下 GraphQL 类型:
type Foo {
bar: String!
baz: String
}
在我的用例中,我想更改此行为并明确定义在 GraphQL 类型中使用我的 C# 类型的哪个类型成员。

最佳答案

Hot Chocolate 允许您反转每种类型或整个模式的行为。
要声明一种特定类型的所有字段,请明确执行以下操作:

public class FooType : ObjectType<Foo>
{
protected override void Configure(IObjectTypeDescriptor<Person> descriptor)
{
// this defines that fields shall only be defined explicitly
descriptor.BindFieldsExplicitly();

// now declare the fields that you want to define.
descriptor.Field(t => t.Bar);
}
}
type Foo {
bar: String!
baz: String
}
如果要在架构中的所有类型上显式声明字段,可以设置以下选项:
services
.AddGraphQLServer()
.AddQueryType<Query>()
// this option will, by default, define that you want to declare everything explicitly.
.ModifyOptions(c => c.DefaultBindingBehavior = BindingBehavior.Explicit);
如果您全局设置它,您可以始终按类型覆盖它,这意味着您可以在这种情况下定义以按类型隐式绑定(bind)成员。

关于asp.net - 有没有办法在 GraphQL 类型上默认忽略所有字段并只添加想要的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65773349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com