在 GraphQL HotChocolate 中,你可以有可选参数或使用构造函数吗?
In GraphQL HotChocolate can you have optional parameters or use a constructor?
我正在使用 HotChocolate 作为我 ASP.NET Core Api
的 GraphQL
服务器。请求的参数需要有一个可选参数,一个Guid,但是如果Guid为null那么模型需要生成一个随机Guid。
public class MutationType : ObjectType<Mutation> {
protected override void Configure(IObjectTypeDescriptor<Mutation> desc)
{
desc
.Field((f) => f.CreateAction(default))
.Name("createAction");
}
}
classMutation
有如下方法
public ActionCommand CreateAction(ActionCommand command) {
...
return command;
}
ActionCommand class 看起来像这样:
public class ActionCommand {
public Guid Id { get; set; }
public string Name { get; set; }
public ActionCommand(string name, Guid id = null) {
Name = name;
Id = id ?? Guid.NewGuid()
}
}
这个命令就是有问题的。我希望能够在 GraphQL 中将此逻辑用于 Id 属性,文档不清楚(在我看来),任何人都可以对此有所了解吗?
谢谢!
这个问题的解决方案是创建一个抽象的基础 CommandType,如下所示:
public abstract class CommandType<TCommand> : InputObjectType<TCommand>
where TCommand : Command {
protected override void Configure(IInputObjectTypeDescriptor<TCommand> desc) {
desc.Field(f => f.CausationId).Ignore();
desc.Field(f => f.CorrelationId).Ignore();
}
}
然后让自定义输入类型继承此 class,如下所示:
public class SpecificCommandType : CommandType<SpecificCommand> {
protected override void Configure(IInputObjectTypeDescriptor<SpecificCommand> desc) {
base.Configure(desc);
desc.Field(t => t.Website).Type<NonNullType<UrlType>>();
}
}
如果不需要进一步配置,也可以使用简写。
public class SpecificCommandType : CommandType<SpecificCommand> { }
命令本身派生自命令 class,它根据需要为值生成 Guid。
public abstract class Command {
protected Command(Guid? correlationId = null, Guid? causationId = null) {
this.CausationId = this.CorrelationId = Guid.NewGuid();
}
public Guid CausationId { get; set; }
public Guid CorrelationId { get; set; }
}
我正在使用 HotChocolate 作为我 ASP.NET Core Api
的 GraphQL
服务器。请求的参数需要有一个可选参数,一个Guid,但是如果Guid为null那么模型需要生成一个随机Guid。
public class MutationType : ObjectType<Mutation> {
protected override void Configure(IObjectTypeDescriptor<Mutation> desc)
{
desc
.Field((f) => f.CreateAction(default))
.Name("createAction");
}
}
classMutation
有如下方法
public ActionCommand CreateAction(ActionCommand command) {
...
return command;
}
ActionCommand class 看起来像这样:
public class ActionCommand {
public Guid Id { get; set; }
public string Name { get; set; }
public ActionCommand(string name, Guid id = null) {
Name = name;
Id = id ?? Guid.NewGuid()
}
}
这个命令就是有问题的。我希望能够在 GraphQL 中将此逻辑用于 Id 属性,文档不清楚(在我看来),任何人都可以对此有所了解吗?
谢谢!
这个问题的解决方案是创建一个抽象的基础 CommandType,如下所示:
public abstract class CommandType<TCommand> : InputObjectType<TCommand>
where TCommand : Command {
protected override void Configure(IInputObjectTypeDescriptor<TCommand> desc) {
desc.Field(f => f.CausationId).Ignore();
desc.Field(f => f.CorrelationId).Ignore();
}
}
然后让自定义输入类型继承此 class,如下所示:
public class SpecificCommandType : CommandType<SpecificCommand> {
protected override void Configure(IInputObjectTypeDescriptor<SpecificCommand> desc) {
base.Configure(desc);
desc.Field(t => t.Website).Type<NonNullType<UrlType>>();
}
}
如果不需要进一步配置,也可以使用简写。
public class SpecificCommandType : CommandType<SpecificCommand> { }
命令本身派生自命令 class,它根据需要为值生成 Guid。
public abstract class Command {
protected Command(Guid? correlationId = null, Guid? causationId = null) {
this.CausationId = this.CorrelationId = Guid.NewGuid();
}
public Guid CausationId { get; set; }
public Guid CorrelationId { get; set; }
}