Apollo updateQueries Not Called?

孤街浪徒 提交于 2019-12-22 14:59:48

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/40166659/apollo-updatequeries-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!