Apollo updateQueries 未调用? [英] Apollo updateQueries Not Called?
问题描述
我正在 GitHunt-React 和 GitHunt-API 中学习 Apollo pub-sub.当我运行这些应用程序并输入新评论时,评论会被提交调用保存,然后 updateQueries 代码块在此处运行:
I'm studying Apollo pub-sub in GitHunt-React and GitHunt-API. When I run those apps and enter a new comment, the comment is saved by the call to submit, and then the updateQueries codeblock runs here:
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 代码块不运行:
I have duplicated this code to my app. The mutation is saved correctly, but the updateQueries code block does not run:
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 调用没有运行?在此先感谢大家提供任何信息.
Why doesn't my updateQueries call run? Thanks in advance to all for any info.
更新:每个请求,这里是 CREATE_IM_MUTATION 的代码:
Update: per request, here is the code of 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
}
}
`;
更新:根据@fabio_oliveira 在 Slack 上的请求,这是我正在更新的查询:
Update: Per request of @fabio_oliveira on Slack, here is the query I am updating:
const GETIMS_QUERY = gql`
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
id,
fromID,
toID,
msgText
}
} `;
推荐答案
Slack 上的@fabio_oliveira 提供了答案.在updateQueries 中,我不得不将key 的名称更改为getIMS,即原始数据收集查询的名称——不是 Mutation 查询的名称:
@fabio_oliveira on Slack provided the answer. In updateQueries I had to change the name of the key to getIMS, that is, the name of the original data-gathering query-- not the name of the Mutation query:
updateQueries: {
getIMs: (prev, { mutationResult }) => {
debugger;
[.....]
这篇关于Apollo updateQueries 未调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!