[英]Nestjs - Mongoose - virtual field - Not able to Query in graphql playground
我正在嘗試在 nestjs-mongoose 中創建一個虛擬字段。 代碼沒有顯示任何錯誤,但我無法在 graphql 操場中查詢該虛擬字段。
我厭倦了使用 schema.virtual("virtualFieldName") 添加虛擬字段。 然后我為相應的架構啟用了 {virtuals: true}。 但仍然無法查詢此字段。
我是否需要在 DTO 文件中添加任何內容?
Entity.ts / 架構文件
@Schema({
collection: 'v3_customer_account_selectors',
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
toJSON: { virtuals: true },
})
@Injectable({ scope: Scope.TRANSIENT })
export class AccountSelectorEntity extends BaseEntity {
@AliasableProp({ alias: '_type', type: AccountSelectorType })
type: AccountSelectorType;
@Prop()
name?: string;
@AliasableProp({ alias: 'last_checked', type: Date })
lastChecked?: Date;
@AliasableProp({
type: SchemaTypes.ObjectId,
alias: 'customer_id',
})
customerId?: Types.ObjectId;
}
export const AccountSelectorEntitySchema = SchemaFactory.createForClass(
AccountSelectorEntity
);
AccountSelectorEntitySchema.set('toJSON', { getters: true, virtual: true });
AccountSelectorEntitySchema.set('toObject', { getters: true, virtual: true });
AccountSelectorEntitySchema.virtual('selector_type').get(function () {
return 'TestReturn';
});```
您必須創建一個類型或另一個類,而不是使用實體。 實體虛擬對象不在實體文檔類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.