【发布时间】:2019-05-03 06:11:46
【问题描述】:
使用的技术:
- graphQL 的 Apollo-server-express
- Sequelize 以连接到 SQL Server
我继承了一个字段不遵循任何命名约定的数据库。有些有空格和特殊字符。为避免任何问题,我在解析器查询中为这些字段使用了 SQL 别名:
const productAttributes = [
'SAP',
['Unit/V', 'UnitVol']];
...
Query: {
product: async (parent, {SAP} , { models }) => {
const result = await models.Product.find( {
where: {
SAP: SAP
},
attributes:productAttributes,
});
return result;
},
...
GraphQL 架构设置为:
type Product {
SAP: String!
UnitVol: Int
}
从 Playground 执行查询时,来自模型的字段“SAP”已被很好地填充,但字段别名“UnitVol”设置为 null(参见图片 GraphQL result)。
仔细看,Sequelize 很好的执行了下面的 SQL 语句:
Executing (default): SELECT [SAP], [Unit/V] AS [UnitVol] FROM [product codes] WHERE [product codes].[SAP] = N'22736';
上述查询结果返回带有别名的值(console.log(result) 的摘录):
product codes {
dataValues:
{ SAP: '22736',
UnitVol: 9 }
},
因此,解析器返回的 SQL 别名与 graphQL 模式之间似乎缺少链接。任何帮助深表感谢。
【问题讨论】:
-
_____________________________
标签: sequelize.js graphql apollo-server