From f57fa7cb7bb9753f461c244b956df3a9060efdbb Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 17 May 2017 23:53:07 -0300 Subject: [PATCH] Update Cache --- .../src/components/Comment.js | 2 +- client/coral-framework/graphql/mutations.js | 49 +++++++++++++++++-- client/coral-plugin-best/BestButton.js | 2 - 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/client/coral-embed-stream/src/components/Comment.js b/client/coral-embed-stream/src/components/Comment.js index 6525f528f..a0d293bdb 100644 --- a/client/coral-embed-stream/src/components/Comment.js +++ b/client/coral-embed-stream/src/components/Comment.js @@ -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' ); diff --git a/client/coral-framework/graphql/mutations.js b/client/coral-framework/graphql/mutations.js index ab07b4441..2ad2d6f50 100644 --- a/client/coral-framework/graphql/mutations.js +++ b/client/coral-framework/graphql/mutations.js @@ -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}); } }); }}), diff --git a/client/coral-plugin-best/BestButton.js b/client/coral-plugin-best/BestButton.js index 691b3c889..b279c0adc 100644 --- a/client/coral-plugin-best/BestButton.js +++ b/client/coral-plugin-best/BestButton.js @@ -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);