Skip to content

Commit

Permalink
Fix issue when writing list over null (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Jun 22, 2023
1 parent 3b43098 commit 35cc897
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-lobsters-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini': patch
---

Fix issue when writing oevr previously null value
6 changes: 4 additions & 2 deletions packages/houdini/src/runtime/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,9 @@ class CacheInternal {
else if (
Array.isArray(value) &&
// make typescript happy
(typeof previousValue === 'undefined' || Array.isArray(previousValue))
(typeof previousValue === 'undefined' ||
previousValue === null ||
Array.isArray(previousValue))
) {
// make a shallow copy of the previous value we can mutate
let oldIDs = [...(previousValue || [])] as (string | null)[]
Expand Down Expand Up @@ -732,7 +734,7 @@ class CacheInternal {
// or we got content for a new list which could already be known. If we just look at
// whether the IDs are the same, situations where we have old data that
// is still valid would not be triggered
const contentChanged = !deepEquals(linkedIDs, oldIDs)
const contentChanged = !deepEquals(linkedIDs, oldIDs) || previousValue === null

// we need to look at the last time we saw each subscriber to check if they need to be added to the spec
if (contentChanged || forceNotify) {
Expand Down
59 changes: 59 additions & 0 deletions packages/houdini/src/runtime/cache/tests/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2880,3 +2880,62 @@ test('reverting optimistic remove notifies subscribers', function () {
test.todo('can write to and resolve layers')

test.todo("resolving a layer with the same value as the most recent doesn't notify subscribers")

test('overwrite null value with list', function () {
// instantiate the cache
const cache = new Cache(config)

const selection: SubscriptionSelection = {
fields: {
friends: {
type: 'User',
visible: true,
keyRaw: 'friends',
selection: {
fields: {
id: {
type: 'ID',
visible: true,
keyRaw: 'id',
},
firstName: {
type: 'String',
visible: true,
keyRaw: 'firstName',
},
},
},
},
},
}

// add some data to the cache
cache.write({
selection,
data: {
friends: null,
},
})

// a function to spy on that will play the role of set
const set = vi.fn()

// subscribe to the fields
cache.subscribe({
rootType: 'Query',
selection: selection,
set: set,
})

// add some data to the cache
cache.write({
selection,
data: {
friends: [],
},
})

expect(set).toHaveBeenCalledWith({
friends: [],
})
})

0 comments on commit 35cc897

Please sign in to comment.