From 7f8401ff4bbfcd3e4cc7cdf21e5267bc5a9a4309 Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 31 Jul 2020 23:21:09 -0700 Subject: [PATCH] fix apollo client 3 initial item load bug I guess if you return a reference to an object that doesn't exist, it registers as null; and you need to provide the `true` here to declare that it _is_ real and should be treated as an _insufficiently_ defined object? --- src/app/WardrobePage/useOutfitState.js | 2 +- src/app/apolloClient.js | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/app/WardrobePage/useOutfitState.js b/src/app/WardrobePage/useOutfitState.js index efb622f..2248874 100644 --- a/src/app/WardrobePage/useOutfitState.js +++ b/src/app/WardrobePage/useOutfitState.js @@ -62,7 +62,7 @@ function useOutfitState() { } ); - const items = (data && data.items) || []; + const items = data?.items || []; const itemsById = {}; for (const item of items) { itemsById[item.id] = item; diff --git a/src/app/apolloClient.js b/src/app/apolloClient.js index f1fb727..2e2b065 100644 --- a/src/app/apolloClient.js +++ b/src/app/apolloClient.js @@ -8,8 +8,11 @@ const typePolicies = { // when you remove an item from your outfit, or add an item from search, // Apollo knows it already has the data it needs and doesn't need to ask // the server again! - items: (_, { args, toReference }) => - args.ids.map((id) => toReference({ __typename: "Item", id })), + items: (_, { args, toReference }) => { + return args.ids.map((id) => + toReference({ __typename: "Item", id }, true) + ); + }, // Teach Apollo how to serve `petAppearance` queries from the cache. That // way, when you switch pet poses, Apollo knows it already has the @@ -17,7 +20,7 @@ const typePolicies = { petAppearance: (_, { args, toReference }) => { const { speciesId, colorId, pose } = args; const id = `${speciesId}-${colorId}-${pose}`; - return toReference({ __typename: "PetAppearance", id }); + return toReference({ __typename: "PetAppearance", id }, true); }, }, },