看完这篇文章后,我想知道假设“代码优先”范式的正确方法是什么。下面的方法似乎有效——使用Location作为输入和输出(在模式文件中隐式生成两个独立的部分,从相同的代码派生)。
@ObjectType()
@InputType('LocationInput')
export class Location {
@Field()
lat: number;
@Field()
lon: number;
}
我知道对于共享代码的输入和输出类型有很多不好的地方——输入有各种验证,而输出没有,等等。但我认为Location是一个很好的例外,因为从大的角度来看,让我们假设Location像String或Int——在整个模式中被视为一个原语:
@InputType
class MyInput {
@Field(type => Location)
location: Location
}
@ObjectType
class MyPlace {
@Field(type => Location, { nullable: true })
location?: Location
}
然后我不明白为什么我不应该重复使用像Location这样简单的东西。
其他人对此有看法吗?