Skip to content

Commit

Permalink
validate idFields
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Dec 9, 2022
1 parent 42a3b16 commit 4fbe508
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/houdini/src/runtime/cache/publicWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ Please acknowledge this by setting acceptImperativeInstability to true in your c
// return the record proxy for the given type/id combo
get(type: string, data: any) {
this.validateInstabilityWarning()

// verify that

// compute the id for the record
let recordID = this._internal_unstable._internal_unstable.id(type, data)
if (!recordID) {
throw new Error('todo')
}

// return the proxy
return new RecordProxy({ cache: this, type, id: recordID, idFields: data })
}

Expand Down Expand Up @@ -73,6 +79,15 @@ export class RecordProxy {
this.id = id
this.type = type
this.idFields = idFields

// make sure that we have all of the necessary fields for the id
if (id !== rootID) {
for (const key of keyFieldsForType(this.cache.config, type)) {
if (!(key in idFields)) {
throw new Error('Missing key in idFields: ' + key)
}
}
}
}

set({ field, args, value }: { field: string; args?: any; value: any }): any {
Expand Down Expand Up @@ -116,6 +131,8 @@ export class RecordProxy {

// use the id fields as the value
value = value.idFields
} else {
throw new Error('Value must be a RecordProxy if the field is a link to another record')
}

// write the value to the cache by constructing the correct selection
Expand Down
11 changes: 10 additions & 1 deletion packages/houdini/src/runtime/cache/tests/public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from 'vitest'

import { testConfigFile } from '../../../test'
import { Cache, rootID } from '../cache'
import { CacheProxy } from '../publicWrapper'
import { CacheProxy, RecordProxy } from '../publicWrapper'

const testCache = () => new CacheProxy(new Cache(testConfigFile()))

Expand Down Expand Up @@ -209,10 +209,19 @@ test('can read and write linked records', function () {
})
})

test('record proxies need every field to compute the id', function () {
const cache = testCache()
expect(() => new RecordProxy({ cache, id: '1', type: 'User', idFields: {} })).toThrowError()
})

test.todo('writing a field should reset its lifetime')

test.todo('complex keys')

test.todo('scalar subscriptions')

test.todo('linked record subscriptions')

test.todo('set list of linked values - flat list')

test.todo('set list of linked values - connection')

0 comments on commit 4fbe508

Please sign in to comment.