Sequelize-如何在查询结果中包括新属性?

时间:2019-08-23 14:19:15

标签: node.js graphql sequelize.js

我在NodeJS中使用"sequelize": "5.3.0",并希望根据GraphQL请求计算距离并将其返回查询结果。

我已经计算了距离并将其添加到如下属性中:

export async function getJobsNear(parentValue,
  { latitude = 0, longitude = 0, radius = 0 }, { auth }) {


  const location = models.sequelize.literal(`ST_GeomFromText('POINT(${ latitude } ${  longitude })')`);
  const pickupLocation = models.sequelize.literal(`POINT(pickup_latitude, pickup_longitude)`);
  const distance = models.sequelize.fn('ST_Distance_Sphere', pickupLocation, location)


  return await models.Job.findAll({
    attributes: {
      include: [[distance, 'distance']]
    },
    where: models.sequelize.where(distance, {[models.Sequelize.Op.lte]: radius}),
    logging: console.log
  })
}

当我在查询中添加distance字段时,它失败:

{
  jobsNear(
    latitude: "0"
    longitude: "0"
    radius: "1000"
  ){
    id
    title
    pickup_latitude
    pickup_longitude
    distance    <-- I want this field to be returned
  }
}

当前,它返回以下错误输出:

{
  "error": {
    "errors": [
      {
        "message": "Cannot query field \"distance\" on type \"job\". Did you mean \"est_distance\"?",
        "locations": [
          {
            "line": 7,
            "column": 5
          }
        ]
      }
    ]
  }
}

如何获取计算出的distance

0 个答案:

没有答案