【问题标题】:Using Promises with Mutation Queries in GraphQL/Apollo?在 GraphQL/Apollo 中使用带有变异查询的 Promise?
【发布时间】:2017-02-21 22:24:42
【问题描述】:

我正在http://localhost:3010/graphiql 中运行以下突变查询:

变异

mutation($fromID: String!, $toID: String!, $msgText: String!){
  createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
    fromID
    toID
    msgText
  }
}

查询变量

{
  "fromID": "1",
  "toID": "2",
  "msgText": "Test from GraphIQL"
}

我的解析器中有这个突变代码:

Mutation: {
    createIM(root, args, context) {
        return Promise.resolve()
            .then(() => {
                console.log('checkpoint #1');
                const temp = connectors.IM.create(args);
                return temp;
            })
            .then((x) => {
                //console.log(x);

                return x;
            })
            .then((args) =>{
                console.log(args);
                console.log('checkpoint #2');
                const temp = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues))
                    return temp;
                }
            )
            .then(comment => {
                // publish subscription notification
                console.log('checkpoint #3');
                console.log(comment);
                pubsub.publish('IMAdded', comment);
                return comment;
            })
            .catch((err)=>{console.log(err);});
    },
},

它成功地在我的 postgres 数据库中创建了一条新记录。最后一个 then 块(检查点 #3)中的 console.log(comment) 记录以下内容:

 [ { id: 1,
     fromID: '1',
     toID: '2',
     msgText: 'testing 123 from graphiql',
     createdAt: Thu Sep 15 2016 12:42:18 GMT-0700 (PDT),
     updatedAt: Thu Sep 15 2016 12:42:18 GMT-0700 (PDT) },
   { id: 2,
     fromID: '1',
     toID: '2',
     msgText: 'test from graphiql',
     createdAt: Sat Sep 17 2016 22:50:43 GMT-0700 (PDT),
     updatedAt: Sat Sep 17 2016 22:50:43 GMT-0700 (PDT) },
   { id: 3,
     fromID: 'DsmkoaYPeAumREsqC',
     toID: '572bddac4ecbbac0ffe37fd4',
     msgText: 'testing 123 from ui',
     createdAt: Sun Sep 18 2016 10:57:12 GMT-0700 (PDT),
     updatedAt: Sun Sep 18 2016 10:57:12 GMT-0700 (PDT) },
[.....]
   { id: 32,
     fromID: '1',
     toID: '2',
     msgText: 'Test from GraphIQL',
     createdAt: Wed Oct 12 2016 16:34:41 GMT-0700 (PDT),
     updatedAt: Wed Oct 12 2016 16:34:41 GMT-0700 (PDT) } ]

但 GraphIQL 给了我以下回应:

{
  "data": {
    "createIM": {
      "fromID": null,
      "toID": null,
      "msgText": null
    }
  }
}

我需要在最后的.then 块中做一些不同的事情吗?

【问题讨论】:

  • 您是要退回 32 个 cmets,还是只退回一个?我怀疑 GraphQL 正在寻找一个评论对象,而不是它们的数组?
  • 我的最终then 块返回最新记录是否被认为是正确的,还是由于竞争条件而导致的潜在错误?
  • 知道了!检查点#2 处的then 代码块是不必要的。收到的参数实际上是刚刚添加的数据库记录。 GraphiQL 现在显示正确的结果!
  • @VikR 很乐意看到你的答案发布,如果你有它的工作!

标签: graphql apollo apollostack


【解决方案1】:

根据 Patrick Scott 的要求,这里是工作代码,感谢@stubailo 的帮助:

Mutation: {
    createIM(root, args, context) {
        return Promise.resolve()
            .then(() => {
                console.log('cp #1');
                const temp = connectors.IM.create(args);
                return temp;
            })
            .then(comment => {
                // publish subscription notification
                console.log('cp #2');
                console.log(comment);
                pubsub.publish('IMAdded', comment);
                return comment;
            })
            .catch((err)=>{
                console.log(err);
            });
    },
},

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2019-01-28
    • 2018-03-02
    • 2018-02-01
    • 2019-05-13
    • 2021-04-03
    • 2018-09-25
    • 2020-04-06
    • 2020-11-04
    相关资源
    最近更新 更多