Skip to content

Commit f68750f

Browse files
authored
feat: Memoize queries (#2978)
1 parent afdc4b7 commit f68750f

File tree

84 files changed

+3691
-3256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3691
-3256
lines changed

.changeset/gentle-nails-try.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@data-client/normalizr': minor
3+
---
4+
5+
BREAKING CHANGE: buildQueryKey() -> memo.buildQueryKey()
6+
7+
```ts title="Before"
8+
const results = buildQueryKey(schema, args, state.indexes, state.entities);
9+
```
10+
11+
```ts title="After"
12+
const memo = new MemoCached();
13+
memo.buildQueryKey(key, schema, args, state.entities, state.indexes);
14+
```

.changeset/great-geckos-breathe.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"@data-client/normalizr": minor
3+
---
4+
5+
Add MemoCache
6+
7+
`MemoCache` is a singleton to store the memoization cache for denormalization methods
8+
9+
```ts
10+
const memo = new MemoCache();
11+
const data = memo.query(key, schema, args, state.entities, state.indexes);
12+
const { data, paths } = memo.denormalize(input, schema, state.entities, args);
13+
const queryKey = memo.buildQueryKey(
14+
key,
15+
schema,
16+
args,
17+
state.entities,
18+
state.indexes,
19+
);
20+
```

.changeset/little-carpets-drop.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class MyEntity extends Entity {
2727
// highlight-next-line
2828
static queryKey(
2929
args: readonly any[],
30-
indexes: NormalizedIndex,
31-
recurse: any,
32-
entities: any,
30+
queryKey: (...args: any) => any,
31+
getEntity: GetEntity,
32+
getIndex: GetIndex,
3333
): any {
3434
if (SILLYCONDITION) return undefined;
35-
return super.queryKey(args, indexes, recurse, entities);
35+
return super.queryKey(args, queryKey, getEntity, getIndex);
3636
}
3737
}
3838
```

.changeset/sour-apples-sneeze.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@data-client/normalizr": minor
3+
---
4+
5+
BREAKING CHANGE: WeakEntityMap -> WeakDependencyMap
6+
7+
We generalize this data type so it can be used with other dependencies.
8+
9+
```ts title="Before"
10+
new WeakEntityMap();
11+
```
12+
13+
```ts title="After"
14+
new WeakDependencyMap<EntityPath>();
15+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@data-client/normalizr': minor
3+
---
4+
5+
BREAKING CHANGE: denormalizeCached() -> new MemoCache().denormalize()
6+
7+
```ts title="Before"
8+
const endpointCache = new WeakEntityMap();
9+
const entityCache = {};
10+
denormalizeCached(input, schema, state.entities, entityCache, endpointCache, args);
11+
```
12+
13+
```ts title="After"
14+
const memo = new MemoCached();
15+
memo.denormalize(input, schema, state.entities, args);
16+
```

.changeset/warm-tigers-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@data-client/endpoint": patch
3+
---
4+
5+
More robustly handle Union queries when the schema cannot be found

docs/core/api/useQuery.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ and [Union](/rest/api/Union).
5959
interface Queryable {
6060
queryKey(
6161
args: readonly any[],
62-
indexes: NormalizedIndex,
63-
recurse: (...args: any) => any,
64-
entities: EntityTable,
65-
// `{}` means non-void
62+
queryKey: (...args: any) => any,
63+
getEntity: GetEntity,
64+
getIndex: GetIndex,
65+
// Must be non-void
6666
): {};
6767
};
6868
```

docs/graphql/api/GQLEntity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static mergeMetaWithStore(
275275

276276
`mergeMetaWithStore()` is called during normalization when a processed entity is already found in the store.
277277

278-
### static queryKey(args, indexes, recurse): pk? {#queryKey}
278+
### static queryKey(args, queryKey, getEntity, getIndex): pk? {#queryKey}
279279

280280
This method enables `Entities` to be [Queryable](/rest/api/schema#queryable) - allowing store access without an endpoint.
281281

docs/rest/api/Entity.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static mergeMetaWithStore(
371371

372372
`mergeMetaWithStore()` is called during normalization when a processed entity is already found in the store.
373373

374-
### static queryKey(args, indexes, recurse, entities): pk? {#queryKey}
374+
### static queryKey(args, queryKey, getEntity, getIndex): pk? {#queryKey}
375375

376376
This method enables `Entities` to be [Queryable](./schema.md#queryable) - allowing store access without an endpoint.
377377

@@ -385,6 +385,31 @@ When used, expiry policy is computed based on the entity's own meta data.
385385

386386
By **default** uses the first argument to lookup in [pk()](#pk) and [indexes](#indexes)
387387

388+
#### getEntity(key, pk?)
389+
390+
Gets all entities of a type with one argument, or a single entity with two
391+
392+
```ts title="One argument"
393+
const entitiesEntry = getEntity(this.schema.key);
394+
if (entitiesEntry === undefined) return INVALID;
395+
return Object.values(entitiesEntry).map(
396+
entity => entity && this.schema.pk(entity),
397+
);
398+
```
399+
400+
```ts title="Two arguments"
401+
if (getEntity(this.key, id)) return id;
402+
```
403+
404+
#### getIndex(key, indexName, value)
405+
406+
Returns the index entry (value->pk map)
407+
408+
```ts
409+
const value = args[0][indexName];
410+
return getIndex(schema.key, indexName, value)[value];
411+
```
412+
388413
### static createIfValid(processedEntity): Entity | undefined {#createIfValid}
389414

390415
Called when denormalizing an entity. This will create an instance of this class

docs/rest/api/schema.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ and [Union](./Union.md).
224224
interface Queryable {
225225
queryKey(
226226
args: readonly any[],
227-
indexes: NormalizedIndex,
228-
recurse: (...args: any) => any,
229-
entities: EntityTable,
227+
queryKey: (...args: any) => any,
228+
getEntity: GetEntity,
229+
getIndex: GetIndex,
230230
// `{}` means non-void
231231
): {};
232232
};

0 commit comments

Comments
 (0)