【发布时间】:2020-11-25 02:26:59
【问题描述】:
我有一个像这样的阿波罗模式:
type Batch {
Batch: String,
ItemCode: String,
PV: String,
ExpirationDate: DateTime,
Freezed: Boolean,
Item:Item,
Quantity: Float,
Fefo: Boolean
}
以及像这样的续集查询:
await models.Batch.findAll({
attributes: ['ItemCode','Lotto','ExpirationDate','Freezed', [Sequelize.literal(`CASE WHEN "Batch" ='${Batch}' THEN true ELSE false END`), "Fefo"] ],
where: whereClause,
include: [models.Item],
order: [
// Will escape title and validate DESC against a list of valid direction parameters
['ExpirationDate']
]
// attributes: models.mapAttribute(models.Batches, info)
})
返回Batch 对象的数组。
所有运行都没有问题,graphql 查询运行良好:
{
getFefoBatches (ItemCode:"200200201",Batch:"20201118B")
{
Batch, ExpirationDate, Fefo, Item { ItemCode ItemName }
}
}
但“Fefo”字段未使用“sequelize.literal”值进行评估。它始终为空。
也许我不知道正确的语法,但我尝试了所有可能的方法,并且我搜索了解决方案但没有成功。
如何在 sequelize 字段和 Apollo 服务器模式之间进行正确的映射?
提前致谢。
【问题讨论】:
-
尝试查看生成的 SQL 查询
-
查询正确,返回数据正常。唯一的区别是表字段被命名为“Batch”。“nameofthefield”,而“case when”生成的文件被命名为“Fefo”,没有表前缀。
-
我尝试了
attributes: ['id', [sequelize.literal('case when total_amount > 0 then 1 else 0 end'), 'test_alias']]之类的方法,效果很好。
标签: node.js graphql sequelize.js apollo-server