From 4fbe508b95201bfa5c49521219e7ff67c26afa4c Mon Sep 17 00:00:00 2001 From: Alec Aivazis Date: Thu, 8 Dec 2022 18:47:42 -0800 Subject: [PATCH] validate idFields --- .../houdini/src/runtime/cache/publicWrapper.ts | 17 +++++++++++++++++ .../src/runtime/cache/tests/public.test.ts | 11 ++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/houdini/src/runtime/cache/publicWrapper.ts b/packages/houdini/src/runtime/cache/publicWrapper.ts index ea91d70b95..d9aff23e03 100644 --- a/packages/houdini/src/runtime/cache/publicWrapper.ts +++ b/packages/houdini/src/runtime/cache/publicWrapper.ts @@ -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 }) } @@ -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 { @@ -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 diff --git a/packages/houdini/src/runtime/cache/tests/public.test.ts b/packages/houdini/src/runtime/cache/tests/public.test.ts index 872620aeba..4a4548172e 100644 --- a/packages/houdini/src/runtime/cache/tests/public.test.ts +++ b/packages/houdini/src/runtime/cache/tests/public.test.ts @@ -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())) @@ -209,6 +209,11 @@ 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') @@ -216,3 +221,7 @@ 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')