首页 文章

使用Sequelize在GraphQL Mutation中必须返回什么?

提问于
浏览
4

我正在尝试实现一个在后端使用Sequelize的GraphQL服务器(在后台使用MSSQL) . 我有一个完美的查询(从单个SQL表中检索数据)按预期方式 .

但后来我为同一个模式设置了一个变异,当我在GraphiQL中运行变异时,我发现,虽然它确实执行了变异的resolve函数内部的东西(也就是创建了我的Sequelize模式的一个实例),但是不要把我的对象还给我 .

enter image description here

现在,我认为这是因为我的Sequelize .create 函数返回一个承诺并且解决方法无法解决这个问题?

这是我到目前为止所得到的:

resolve(_,args){
  Forecast.create({
    bunit: args.bunit,
    season: args.season,
    position: args.position,
    currency: args.currency,
    settle_date: new Date(args.settle_date),
    reference: args.reference
  }).then(forecast => {
    return forecast;
  }).catch(err => {
    console.error(err);
    return err;
  });
}

我找不到任何明确的解释或教程,告诉我如何构建解析函数在我异步执行某些操作时需要返回的内容 . 或者我只是不明白,这也很可能 .

2 回答

  • 0

    @Adam 's suggestion above worked in this instance. I am surprised we don' t需要读取 .create 方法调用的promise,但它似乎工作正常 .

    resolve(_,args){
          return Forecast.create({
            bunit: args.bunit,
            season: args.season,
            position: args.position,
            currency: args.currency,
            settle_date: new Date(args.settle_date),
            reference: args.reference
          });
          // .then(forecast => {
          //   console.error(err);
          //   //return forecast;
          // }).catch(err => {
          //   console.error(err);
          //   //return err;
          // });
    
          //return newForecast;
        }
      }
    

    enter image description here

    谢谢!

  • 0

    删除 thencatch 实现有效但不应该't be done, you'已经遇到了承诺 then -chain . 每个 then (或catch) return 部分都会更改解析程序的返回值 .

    因此,在 Forecast 上返回 return 并在 thencatch 内返回是正确的方法

    resolve(_,args){
          return Forecast.create({
            bunit: args.bunit,
            season: args.season,
            position: args.position,
            currency: args.currency,
            settle_date: new Date(args.settle_date),
            reference: args.reference
          });
           .then(forecast => {
             console.error(err);
             return forecast;
           }).catch(err => {
             console.error(err);
             return err;
           });
    
          return newForecast;
        }
      }
    

相关问题