在GraphQL/Apollo中使用承诺和变异查询?
我正在中运行以下变异查询: 突变在GraphQL/Apollo中使用承诺和变异查询?,graphql,apollo,apollostack,Graphql,Apollo,Apollostack,我正在中运行以下变异查询: 突变 mutation($fromID: String!, $toID: String!, $msgText: String!){ createIM(fromID: $fromID, toID: $toID, msgText: $msgText){ fromID toID msgText } } 查询变量 { "fromID": "1", "toID": "2", "msgText": "Test from GraphIQ
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数据库中创建了一个新记录。最后一个中的console.log(comment)
,然后block(checkpoint#3)记录以下内容:
[ { 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
}
}
}
我是否需要在最后的中做一些不同的事情?然后块?根据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);
});
},
},
您是想返回32条评论,还是只返回一条?我怀疑GraphQL正在寻找一个注释对象,而不是它们的数组?我的最终then
块返回最新记录是否正确,或者这是由于竞争条件导致的潜在错误?明白了!检查点2处的then
代码块是不必要的。收到的参数实际上是刚刚添加的数据库记录。GraphiQL现在正在显示正确的结果@VikR很想看到你的答案贴出来,如果你的答案有效的话!