Apollo updateQueries未被调用?[英] Apollo updateQueries Not Called?

本文是小编为大家收集整理的关于Apollo updateQueries未被调用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在研究githunt-react和githunt-api中的阿波罗酒吧.当我运行这些应用程序并输入评论时,该评论将由呼叫提交的呼叫保存,然后在此处运行update Querteries CodeBlock:

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);

为什么我的Update Quermeries呼叫不运行?事先感谢所有信息.

更新:根据请求,这是Create_im_muarn的代码:

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上的请求,这是我要更新的查询:

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的密钥名称,即原始数据收集查询的名称 - 不是突变查询的名称:

                updateQueries: {
                     getIMs: (prev, { mutationResult }) => {
                        debugger;
                        [.....]

本文地址:https://www.itbaoku.cn/post/1938068.html

问题描述

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);

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);

Why doesn't my updateQueries call run? Thanks in advance to all for any info.

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
                    }
                }
`;

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
  }
}  `;

推荐答案

@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;
                        [.....]