2017-04-23 25 views
2

所以基本上我想在突變後更新我的UI,但這確實是一個痛苦,現在已經停留了幾天。突變後Apollo客戶端更新無法映射原始結果?

我的一些組件依賴於我的根查詢,並映射到結果上。

每當我創建新資源並更新我的根查詢時,原始結果不會再加載,這會導致can't read property map of undefined,因爲結果不再存在。添加一個支票什麼都不做,它只是一直加載。

這是我的突變:

export const withCreateLink = graphql(createLink, { 
// Mutate normally gets passed as prop to wrapped component 
props({ownProps, mutate}) { 
    return { 
     // But for updating the store we're actually hijacking mutate to a custom function   
     createLink(url, description, group) { 
      return mutate({ 
       variables: { 
        url, 
        description, 
        group 
       }, 
       optimisticResponse: { 
        createLink: { 
         __typename: 'Link', 
         id: -1, 
         url, 
         description, 
         group: { 
          id: group 
         } 
        } 
       }, 
       update: (proxy, mutationResult) => { 
        const query = getAllGroups; 
        const data = proxy.readQuery({query}); 

        data.allGroups.forEach((groupData) => { 
         if(groupData.id == group) 
          groupData.links.push(mutationResult.data.createLink); 
        }); 

        proxy.writeQuery({ 
         query, 
         data 
        }) 
       } 
      }) 
     } 
    } 
} 
}); 

我只想簡單地只刷新我的資源添加到組更新我的UI。有人可以幫我嗎?

回答

0

轉化dataIdFromObject檢查類型名的伎倆:

dataIdFromObject: (result) => { 
    if (result.id && result.__typename) { 
     return result.__typename + ':' + result.id; 
    } 
    return null; 
} 

我想我寫不出來我的緩存與只使用標識。這顯然是一個默認提議。

+0

這是1.0以來的默認值,所以你應該沒有提供dataIdFromObject。 – helfer