我正在GitHunt-React和GitHunt-API学习Apollo pub-sub。当我运行这些应用程序并输入新注释时,通过调用提交保存注释,然后updateQueries代码块在此处运行:
const CommentsPageWithMutations = graphql(SUBMIT_COMMENT_MUTATION, {
props({ ownProps, mutate }) {
console.log('in CommentsPageWithMutations');
return {
submit({ repoFullName, commentContent }) { <==RUNS THE MUTATION
debugger;
return mutate({
variables: { repoFullName, commentContent },
optimisticResponse: {
__typename: 'Mutation',
submitComment: {
__typename: 'Comment',
id: null,
postedBy: ownProps.currentUser,
createdAt: +new Date,
content: commentContent,
},
},
updateQueries: {
Comment: (prev, { mutationResult }) => {
debugger; // <== RUNS AFTER THE MUTATION IS SENT TO SERVER
const newComment = mutationResult.data.submitComment;
if (isDuplicateComment(newComment, prev.entry.comments)) {
return prev;
}
return update(prev, {
entry: {
comments: {
$unshift: [newComment],
},
},
});
},
},
});
},
};
},
})(CommentsPage);
我已将此代码复制到我的应用中。突变保存正确,但updateQueries代码块执行不运行:
const CreateIMPageWithMutations = graphql(CREATE_IM_MUTATION, {
props({ ownProps, mutate }) {
debugger;
return {
submit({ fromID, toID, msgText }) { <==SAVES SUCCESSFULLY
debugger;
return mutate({
variables: {
"fromID": fromID,
"toID": toID,
"msgText": msgText
},
optimisticResponse: {
__typename: 'Mutation',
createIM: {
__typename: 'createIM',
fromID: fromID,
toID: toID,
createdAt: +new Date,
msgText: msgText,
},
},
updateQueries: {
createIM: (prev, { mutationResult }) => {
debugger; <== THIS CODE BLOCK IS NEVER CALLED
const newMsg = mutationResult.data.createIM;
return update(prev, {
entry: {
IMs: {
$unshift: [newMsg],
},
},
});
},
},
});
},
};
},
})(CreateIM);
为什么我的updateQueries调用没有运行?提前感谢所有信息。
更新:每个请求,以下是CREATE_IM_MUTATION的代码:
const CREATE_IM_MUTATION = gql`
mutation createIM ($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`;
更新:根据Slack的@fabio_oliveira请求,以下是我要更新的查询:
const GETIMS_QUERY = gql`
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
id,
fromID,
toID,
msgText
}
} `;
答案 0 :(得分:2)
updateQueries: {
getIMs: (prev, { mutationResult }) => {
debugger;
[.....]