useMutation不改变本地状态

时间:2020-01-16 09:39:14

标签: react-native graphql react-apollo apollo-client graphql-js

在尝试更改阿波罗中的本地状态时遇到此错误。

errInvariant Violation:期望解析的GraphQL文档。也许您需要将查询字符串包装在“ gql”标签中? http://docs.apollostack.com/apollo-client/core.html#gql

初始状态

registration: {
    __typename: 'Registration',
    tempMerchantId: '',
    authorizeProfile: {
      __typename: 'AuthorizePersonProfile',
      nid_front: '',
      nid_back: '',
      authorized_person_photo: ''
    }
  }

我的突变

export const setAuthorizePersonQuery = gql`
    mutation setAuthorizePersonProfileInfo($authorizePerosnData: Object!){
        setAuthorizePersonProfileInfo(authorizePersonData: $authorizePerosnData) @client
    }
`;

我的解析器

export const setAuthorizePersonProfileInfo = (
  _, { authorizePersonData }, { cache }
) => {
  try {
    const prevData = cache.readQuery({ getAuthorizePersonProfileQuery });
    cache.writeQuery({
      getAuthorizePersonProfileQuery,
      data: {
        registration: {
          __typename: 'Registration',
          authorizeProfile: {
            __typename: 'AuthorizePersonProfile',
            ...prevData.registration.authorizeProfile,
            ...authorizePersonData
          }
        }
      }
    });
  } catch (e) {
    console.log(`err${e}`);
  }
  return null;
};

我试图在按下按钮时改变本地状态,该功能是

const handlePressedNext = () => {
    Promise.all([
      setAuthorizePersonProfileInfo({
        variables: { authorizePersonData: generateNidData() }
      })
    ])
      .then(() => {
        navigation.navigate('Photograph');
      });
  };

generateNidData功能类似于波纹管

const generateNidData = () => ({
    nid_front: nidFrontImage,
    nid_back: nidBackImage
  });

我是apollo客户的新手。我不明白我在做什么错。谁能帮我解决问题?

1 个答案:

答案 0 :(得分:2)

getAuthorizePersonProfileQuery不是readQuery的有效选项。大概是用query来代替的。