未调用 Apollo updateQueries?
Apollo updateQueries Not Called?
我正在研究 GitHunt-React 和 GitHunt-API 中的 Apollo pub-sub。当我 运行 这些应用程序并输入新评论时,评论通过提交调用保存,然后 updateQueries 代码块 运行s 在这里:
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 代码块 not 运行:
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
}
} `;
Slack 上的 @fabio_oliveira 提供了答案。在 updateQueries 中,我不得不将密钥的名称更改为 getIMS,即原始数据收集查询的名称——不是 Mutation 查询的名称:
updateQueries: {
getIMs: (prev, { mutationResult }) => {
debugger;
[.....]
我正在研究 GitHunt-React 和 GitHunt-API 中的 Apollo pub-sub。当我 运行 这些应用程序并输入新评论时,评论通过提交调用保存,然后 updateQueries 代码块 运行s 在这里:
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 代码块 not 运行:
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
}
} `;
@fabio_oliveira 提供了答案。在 updateQueries 中,我不得不将密钥的名称更改为 getIMS,即原始数据收集查询的名称——不是 Mutation 查询的名称:
updateQueries: {
getIMs: (prev, { mutationResult }) => {
debugger;
[.....]