Update Cache

This commit is contained in:
Belen Curcio
2017-05-17 23:53:07 -03:00
parent 6664f73d2d
commit f57fa7cb7b
3 changed files with 47 additions and 6 deletions
@@ -216,7 +216,7 @@ class Comment extends React.Component {
addTag({
id: comment.id,
name: BEST_TAG,
asset_id: asset.id
assetId: asset.id
}),
() => 'Failed to tag comment as best'
);
+46 -3
View File
@@ -95,6 +95,16 @@ export const withDeleteAction = withMutation(
}}),
});
const COMMENT_FRAGMENT = gql`
fragment CoralRespect_UpdateFragment on Comment {
tags {
tag {
name
}
}
}
`;
export const withAddTag = withMutation(
gql`
mutation AddCommentTag($id: ID!, $asset_id: ID!, $name: String!) {
@@ -104,13 +114,35 @@ export const withAddTag = withMutation(
}
`, {
props: ({mutate}) => ({
addTag: ({id, name, asset_id}) => {
addTag: ({id, name, assetId}) => {
return mutate({
variables: {
id,
name,
asset_id
}
asset_id: assetId
},
optimisticResponse: {
deleteAction: {
__typename: 'DeleteActionResponse',
errors: null,
}
},
update: (proxy) => {
const fragmentId = `Comment_${id}`;
// Read the data from our cache for this query.
const data = proxy.readFragment({fragment: COMMENT_FRAGMENT, id: fragmentId});
data.tags.push({
tag: {
__typename: 'TagLink',
name
}
});
// Write our data back to the cache.
proxy.writeFragment({fragment: COMMENT_FRAGMENT, id: fragmentId, data});
},
});
}}),
});
@@ -132,6 +164,17 @@ export const withRemoveTag = withMutation(
id,
name,
asset_id
},
update: (proxy) => {
const fragmentId = `Comment_${id}`;
// Read the data from our cache for this query.
const data = proxy.readFragment({fragment: COMMENT_FRAGMENT, id: fragmentId});
console.log('remove', data);
// Write our data back to the cache.
proxy.writeFragment({fragment: COMMENT_FRAGMENT, id: fragmentId, data});
}
});
}}),
-2
View File
@@ -9,8 +9,6 @@ export const BEST_TAG = 'BEST';
export const commentIsBest = ({tags} = {}) => tags.some(t => t.tag.name === BEST_TAG);
// const commentIsBest = tags => !!tags.filter(i => i.tag.name === 'BEST_TAG).length;
const name = 'coral-plugin-best';
const lang = new I18n(translations);