From 19c9683e2126c68cb9d231293162c756d69c51fb Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 8 Feb 2018 10:41:16 -0500 Subject: WIP --- frontend/enhancers/enhancers.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'frontend/enhancers') diff --git a/frontend/enhancers/enhancers.js b/frontend/enhancers/enhancers.js index 525c720..ff1712f 100644 --- a/frontend/enhancers/enhancers.js +++ b/frontend/enhancers/enhancers.js @@ -1,13 +1,37 @@ -import { ALL_ITEMS_QUERY } from "../queries/index"; -import { graphql } from "react-apollo"; +import { ALL_ITEMS_QUERY, REMOVE_ITEM_MUTATION, SINGLE_ITEM_QUERY } from '../queries/index'; +import { graphql } from 'react-apollo'; export const itemEnhancer = graphql(ALL_ITEMS_QUERY, { - name: "allItemsQuery", + name: 'itemsQuery', options({ page }) { return { variables: { - skip: page * 3 - 3 - } + skip: page * 3 - 3, + }, }; - } + }, +}); + +export const removeItemMutation = graphql(REMOVE_ITEM_MUTATION, { + name: 'removeItem', + options: { + update: (proxy, { data: { deleteItem } }) => { + // grab the data from our cache + const data = proxy.readQuery({ query: ALL_ITEMS_QUERY }); + console.log(data); + // filter out the deleted item + data.items = data.items.filter(item => item.id !== deleteItem.id); + + // and then "set state" (update cache), so it will update wherever we have used this data on the page + proxy.writeQuery({ query: ALL_ITEMS_QUERY, data }); + }, + }, +}); + +export const singleItemEnhancer = graphql(SINGLE_ITEM_QUERY, { + name: 'findItem', + // This comes from Props + options: ({ id }) => ({ + variables: { id }, + }), }); -- cgit v1.3