gpt4 book ai didi

sequelize.js - GraphQL/Sequelize : SQL alias

转载 作者:行者123 更新时间:2023-12-03 22:20:00 25 4
gpt4 key购买 nike

使用中的技术:

  • 用于 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
    }

    当从操场执行查询时,来自模型的字段“SAP”已被填满,但字段别名“UnitVol”设置为空(参见图像 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 结果,而是返回结果中的 'datavalues' 数组:

    return !result ? [] : result.dataValues;

    我必须控制结果不为空(graphQL 请求没有条目)。如果有人有更清晰的答案,我很乐意测试它!谢谢。

    关于sequelize.js - GraphQL/Sequelize : SQL alias,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568529/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com