【问题标题】:Support for `{where: 'raw query'}` has been removed已删除对“{where: 'raw query'}”的支持
【发布时间】:2020-07-16 16:52:39
【问题描述】:

我正在使用 AWS Lambda 上的无服务器框架运行 GraphQL 服务器。 我正在使用 apollo-link-batch-http 获取 UI 中的数据。

如果我使用serverless-offline 在本地运行它,它可以正常工作。但如果我在 AWS Lambda 上运行它,它会成功解析 fooResolver 而不是 barResolver,因为它会抛出上述错误消息。

Model.cached(300) 是我制作的一个小型缓存包装器。在这里你可以看到它: https://gist.github.com/lookapanda/4676083186849bb6c5ae6f6230ad7d8f 它基本上只是让我能够使用我自己的findById 函数等等。

奇怪的是,这个错误只有在我使用apollo-link-batch-http 时才会出现,但在我使用apollo-link-http 时不会出现。因此,如果将请求批处理为单个 GraphQL 请求,则不会出现此类错误(虽然,然后我收到此错误:https://github.com/sequelize/sequelize/issues/9242

我真的不知道那里发生了什么,在任何这些解析器中都没有原始 where 查询。它变得更加奇怪:它只发生在缓存的结果中。第一个请求完全有效且成功,但随后每个连续请求都失败并显示上述错误消息。

我真的希望有人能帮助我,我快疯了:D

export const fooResolver = async () => {
  const Model = db.getDB().sequelize.models.fooModel;
  const data = await Model.cached(300).findAll({
      where: {
          time: {
              [Op.gt]: Model.sequelize.literal('CURRENT_TIMESTAMP()'),
          },
          enabled: true,
          state: 'PLANNED',
      },
      order: [['time', 'DESC']],
      limit: 5,
  });
  return data.value;
};

export const barResolver = async () => {
  const models = db.getDB().sequelize.models;
  const Model = models.fooModel;
  const data = await Model.findById(data.id, {
    include: [
      {
        model: models.barModel,
        include: [
          {
            association: 'fooAssociation',
            include: [{ association: 'barAssociation' }],
            order: ['showOrder', 'ASC'],
          },
        ],
      },
    ],
  });

  return {
    data,
  };
};  

【问题讨论】:

    标签: javascript lambda sequelize.js graphql serverless-framework


    【解决方案1】:

    好的,经过繁琐的调试后,我发现在可缓存包装器中我使用了这个 sn-p: https://github.com/sequelize/sequelize/issues/2325#issuecomment-366060303

    我仍然不知道,为什么这个错误只出现在 Lambda 而不是本地,但是当我只使用 selectQuery() 方法并且只返回它而不是整个 Model.addHook 东西时它停止了错误和很快。所以基本上改了这个

    export const getSqlFromSelect = (Model, method, args) => {
      if (!SUPPORTED_SELECT_METHODS.includes(method)) {
        throw new Error('Unsupported method.');
      }
    
      const id = generateRandomHash(10);
    
      return new Promise((resolve, reject) => {
        Model.addHook('beforeFindAfterOptions', id, options, => {
          Model.removeHook('beforeFindAfterOptions', id);
    
          resolve(
            Model.sequelize.dialect.QueryGenerator.selectQuery(
              Model.getTableName(),
              options,
              Model
            ).slice(0, -1)
          );
        });
    
        return Model[method](...args).catch(reject);
      });
    };
    

    到这里

    export const getSqlFromSelect = (Model, identifier, options) => {
      if (typeof identifier === 'number' || typeof identifier === 'string' || Buffer.isBuffer(identifier) {
        options.where = {
          [Model.primaryKeyAttribute]: identifier,
        };
      };
    
      return Model.sequelize.dialect.QueryGenerator.selectQuery(
        Model.getTableName(),
        options,
        Model
      ).slice(0, -1);
    };
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的情况,除了在我的情况下使用下面的代码效果很好:

      .findAll({
          where: {
              title: req.params.title 
          } 
      })
      

      【讨论】:

        猜你喜欢
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 2022-06-27
        • 2010-11-06
        相关资源
        最近更新 更多