Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Seanaye/feat/optimistic links (#5502)

Sean Aye6b0779ee8ff1
diff --git a/apps/web/src/lib/graphql-cache/exchange/optimistic.test.ts b/apps/web/src/lib/graphql-cache/exchange/optimistic.test.ts
index 9b9a689191..fc5dee5fb9 100644
--- a/apps/web/src/lib/graphql-cache/exchange/optimistic.test.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/optimistic.test.ts
@@ -1,6 +1,13 @@
import { GroupSoupMembershipDocument } from '@service-storage/graphql/generated/graphql';
import { describe, expect, it } from 'vitest';
-import { prependUnique, remove, select, update } from './optimistic';
+import {
+ prependUnique,
+ remove,
+ removeEmbeddedLink,
+ select,
+ update,
+ upsertEmbeddedLink,
+} from './optimistic';
const input = {
initial: {
@@ -48,6 +55,48 @@ describe('typed optimistic graph updates', () => {
expect(prepend.operation.kind).toBe('prependUnique');
});
+ it('serializes counted embedded link changes', () => {
+ const bins = select(GroupSoupMembershipDocument, { input })
+ .field('user')
+ .field('groupSoup')
+ .field('bins');
+ const entity = { __typename: 'GraphqlSoupDocument', id: 'task-1' };
+ const removal = removeEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: 'high' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entity,
+ });
+ const insertion = upsertEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: 'urgent' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entity,
+ insertFields: { nextCursor: null },
+ });
+
+ expect(removal.operation).toEqual({
+ kind: 'removeEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'high' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ });
+ expect(insertion.path).toEqual([
+ { field: 'user' },
+ { field: 'groupSoup' },
+ { field: 'bins' },
+ ]);
+ expect(insertion.operation).toEqual({
+ kind: 'upsertEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'urgent' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ insertFields: { nextCursor: null },
+ });
+ });
+
it('uses generated operation result and variable types', () => {
const typeAssertions = () => {
// @ts-expect-error GroupSoupMembership requires an input variable.
@@ -67,6 +116,32 @@ describe('typed optimistic graph updates', () => {
root.field('user'),
remove({ __typename: 'GraphqlUser', id: 'user-1' })
);
+ upsertEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: 'urgent' },
+ linkField: 'items',
+ // @ts-expect-error Count fields must be generated numeric fields.
+ countField: 'key',
+ entity: { __typename: 'GraphqlSoupDocument', id: 'task-1' },
+ insertFields: { nextCursor: null },
+ });
+ upsertEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: 'urgent' },
+ // @ts-expect-error Link fields must be generated normalized-entity lists.
+ linkField: 'key',
+ countField: 'totalCount',
+ entity: { __typename: 'GraphqlSoupDocument', id: 'task-1' },
+ insertFields: { nextCursor: null },
+ });
+ upsertEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: 'urgent' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entity: { __typename: 'GraphqlSoupDocument', id: 'task-1' },
+ insertFields: {
+ // @ts-expect-error Managed fields cannot be supplied for insertion.
+ key: 'urgent',
+ },
+ });
};
expect(typeAssertions).toBeTypeOf('function');
diff --git a/apps/web/src/lib/graphql-cache/exchange/optimistic.ts b/apps/web/src/lib/graphql-cache/exchange/optimistic.ts
index 6d68836a31..21e1464945 100644
--- a/apps/web/src/lib/graphql-cache/exchange/optimistic.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/optimistic.ts
@@ -39,6 +39,15 @@ type JsonScalar = string | number | boolean | null;
type ScalarKey<T> = {
[K in StringKey<T>]-?: Present<T[K]> extends JsonScalar ? K : never;
}[StringKey<T>];
+type NumberKey<T> = {
+ [K in StringKey<T>]-?: Present<T[K]> extends number ? K : never;
+}[StringKey<T>];
+type ScalarInsertFields<T, TExcluded extends StringKey<T>> = Partial<{
+ [K in Exclude<ScalarKey<T>, TExcluded>]: Extract<
+ Exclude<T[K], undefined>,
+ JsonScalar
+ >;
+}>;
type SelectionState = {
readonly document: TypedDocumentNode<unknown, AnyVariables>;
readonly variables: AnyVariables;
@@ -74,6 +83,13 @@ export type NormalizedEntityIdentity = {
__typename: string;
id: string;
};
+type NormalizedEntityListKey<T> = {
+ [K in StringKey<T>]-?: Present<T[K]> extends readonly (infer TItem)[]
+ ? Present<TItem> extends NormalizedEntityIdentity
+ ? K
+ : never
+ : never;
+}[StringKey<T>];
/** An idempotent change to a normalized-link list. */
export type LinkDiff =
@@ -171,6 +187,16 @@ function serializeRevalidation(
};
}
+function definedScalarFields(
+ fields: Readonly<Record<string, JsonScalar | undefined>>
+): Record<string, JsonScalar> {
+ return Object.fromEntries(
+ Object.entries(fields).filter(
+ (entry): entry is [string, JsonScalar] => entry[1] !== undefined
+ )
+ );
+}
+
function createSelection<T>(state: SelectionState): Selection<T> {
const selection = {
...state,
@@ -238,6 +264,86 @@ export function update<TItem extends object>(
} as OptimisticUpdate;
}
+/**
+ * Removes an entity link from a selected embedded list item and decrements
+ * its count only when the link was present.
+ */
+export function removeEmbeddedLink<
+ TItem extends object,
+ TSelectorField extends ScalarKey<TItem>,
+ TLinkField extends NormalizedEntityListKey<TItem>,
+ TCountField extends NumberKey<TItem>,
+>(
+ selection: ListSelection<TItem>,
+ args: {
+ listItem: {
+ whereField: TSelectorField;
+ equals: Extract<Present<TItem[TSelectorField]>, JsonScalar>;
+ };
+ linkField: TLinkField;
+ countField: TCountField;
+ entity: NormalizedEntityIdentity;
+ }
+): OptimisticUpdate {
+ const update: OptimisticLinkPatchWire = {
+ query: stringifyDocument(selection.document),
+ operationName: documentOperationName(selection.document),
+ variablesJson: JSON.stringify(selection.variables ?? {}),
+ path: [...selection.path],
+ operation: {
+ kind: 'removeEmbeddedLink',
+ listItem: args.listItem,
+ linkField: args.linkField,
+ countField: args.countField,
+ entityKey: normalizedEntityKey(args.entity),
+ },
+ };
+ return update as OptimisticUpdate;
+}
+
+/**
+ * Prepends an entity link inside a selected embedded list item, creating the
+ * embedded item from scalar fields when its selector does not exist. Its
+ * count is initialized or incremented only when the link is newly inserted.
+ */
+export function upsertEmbeddedLink<
+ TItem extends object,
+ TSelectorField extends ScalarKey<TItem>,
+ TLinkField extends NormalizedEntityListKey<TItem>,
+ TCountField extends NumberKey<TItem>,
+>(
+ selection: ListSelection<TItem>,
+ args: {
+ listItem: {
+ whereField: TSelectorField;
+ equals: Extract<Present<TItem[TSelectorField]>, JsonScalar>;
+ };
+ linkField: TLinkField;
+ countField: TCountField;
+ entity: NormalizedEntityIdentity;
+ insertFields: ScalarInsertFields<
+ TItem,
+ TSelectorField | TCountField | TLinkField
+ >;
+ }
+): OptimisticUpdate {
+ const update: OptimisticLinkPatchWire = {
+ query: stringifyDocument(selection.document),
+ operationName: documentOperationName(selection.document),
+ variablesJson: JSON.stringify(selection.variables ?? {}),
+ path: [...selection.path],
+ operation: {
+ kind: 'upsertEmbeddedLink',
+ listItem: args.listItem,
+ linkField: args.linkField,
+ countField: args.countField,
+ entityKey: normalizedEntityKey(args.entity),
+ insertFields: definedScalarFields(args.insertFields),
+ },
+ };
+ return update as OptimisticUpdate;
+}
+
/**
* Executes `document` with one durable optimistic entity/link transaction.
* The source resolves with the head's network result or a synthetic queued
diff --git a/apps/web/src/lib/graphql-cache/index.ts b/apps/web/src/lib/graphql-cache/index.ts
index d55ff9132d..7a73ed6c17 100644
--- a/apps/web/src/lib/graphql-cache/index.ts
+++ b/apps/web/src/lib/graphql-cache/index.ts
@@ -22,9 +22,11 @@ export {
prependUnique,
type QueryRevalidation,
remove,
+ removeEmbeddedLink,
type Selection,
select,
update,
+ upsertEmbeddedLink,
} from './exchange/optimistic';
export {
type RecordSelection,
diff --git a/apps/web/src/lib/graphql-cache/protocol.ts b/apps/web/src/lib/graphql-cache/protocol.ts
index 4bbb2ffd4b..dd76ca518a 100644
--- a/apps/web/src/lib/graphql-cache/protocol.ts
+++ b/apps/web/src/lib/graphql-cache/protocol.ts
@@ -77,7 +77,29 @@ export type OptimisticLinkPatchWire = {
path: EmbeddedLinkPathSegment[];
operation:
| { kind: 'remove'; entityKey: string }
- | { kind: 'prependUnique'; entityKey: string };
+ | { kind: 'prependUnique'; entityKey: string }
+ | {
+ kind: 'removeEmbeddedLink';
+ listItem: {
+ whereField: string;
+ equals: string | number | boolean | null;
+ };
+ linkField: string;
+ countField: string;
+ entityKey: string;
+ }
+ | {
+ kind: 'upsertEmbeddedLink';
+ listItem: {
+ whereField: string;
+ equals: string | number | boolean | null;
+ };
+ linkField: string;
+ countField: string;
+ entityKey: string;
+ /** Scalar fields used only when the embedded item must be created. */
+ insertFields: Record<string, string | number | boolean | null>;
+ };
};
export type CachedQueryVariantWire = {
diff --git a/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.test.ts b/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.test.ts
index e18bb332b0..abef4fb2ff 100644
--- a/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.test.ts
+++ b/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.test.ts
@@ -156,24 +156,25 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
expect(result.updates).toHaveLength(2);
expect(result.updates.map((patch) => patch.operation)).toEqual([
- { kind: 'remove', entityKey: 'GraphqlSoupDocument:task-1' },
- { kind: 'prependUnique', entityKey: 'GraphqlSoupDocument:task-1' },
+ {
+ kind: 'removeEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'in-progress' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ },
+ {
+ kind: 'upsertEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'completed' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ insertFields: { nextCursor: null },
+ },
]);
expect(result.updates.map((update) => update.path)).toEqual([
- [
- { field: 'user' },
- { field: 'groupSoup' },
- { field: 'bins' },
- { listItem: { whereField: 'key', equals: 'in-progress' } },
- { field: 'items' },
- ],
- [
- { field: 'user' },
- { field: 'groupSoup' },
- { field: 'bins' },
- { listItem: { whereField: 'key', equals: 'completed' } },
- { field: 'items' },
- ],
+ [{ field: 'user' }, { field: 'groupSoup' }, { field: 'bins' }],
+ [{ field: 'user' }, { field: 'groupSoup' }, { field: 'bins' }],
]);
expect(result.revalidations).toHaveLength(1);
expect(onInspect).toHaveBeenCalledWith(
@@ -238,12 +239,18 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
});
expect(result.updates.map((patch) => patch.operation)).toEqual([
- { kind: 'remove', entityKey: 'GraphqlSoupCall:task-1' },
- { kind: 'prependUnique', entityKey: 'GraphqlSoupCall:task-1' },
+ expect.objectContaining({
+ kind: 'removeEmbeddedLink',
+ entityKey: 'GraphqlSoupCall:task-1',
+ }),
+ expect.objectContaining({
+ kind: 'upsertEmbeddedLink',
+ entityKey: 'GraphqlSoupCall:task-1',
+ }),
]);
});
- it('does not make a partial move when the destination bin is absent', async () => {
+ it('creates a missing destination bin as part of the move', async () => {
const result = await buildOptimisticGroupedPropertyUpdates({
host: host({ destination: false }),
entityId: 'task-1',
@@ -252,7 +259,29 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
newGroupKeys: ['completed'],
});
- expect(result.updates).toEqual([]);
+ expect(result.updates).toHaveLength(2);
+ expect(result.updates.map((patch) => patch.operation)).toEqual([
+ {
+ kind: 'removeEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'in-progress' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ },
+ {
+ kind: 'upsertEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'completed' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ insertFields: { nextCursor: null },
+ },
+ ]);
+ expect(result.updates[1]?.path).toEqual([
+ { field: 'user' },
+ { field: 'groupSoup' },
+ { field: 'bins' },
+ ]);
expect(result.revalidations).toHaveLength(1);
});
@@ -281,8 +310,8 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
onRead.mock.calls.map(([request]) => request.variables.input)
).toEqual([input, continuation]);
expect(result.updates.map((patch) => patch.operation.kind)).toEqual([
- 'remove',
- 'prependUnique',
+ 'removeEmbeddedLink',
+ 'upsertEmbeddedLink',
]);
});
@@ -296,8 +325,8 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
});
expect(result.updates.map((patch) => patch.operation.kind)).toEqual([
- 'remove',
- 'prependUnique',
+ 'removeEmbeddedLink',
+ 'upsertEmbeddedLink',
]);
});
@@ -311,7 +340,14 @@ describe('buildOptimisticGroupedPropertyUpdates', () => {
});
expect(result.updates.map((patch) => patch.operation)).toEqual([
- { kind: 'prependUnique', entityKey: 'GraphqlSoupDocument:task-1' },
+ {
+ kind: 'upsertEmbeddedLink',
+ listItem: { whereField: 'key', equals: 'completed' },
+ linkField: 'items',
+ countField: 'totalCount',
+ entityKey: 'GraphqlSoupDocument:task-1',
+ insertFields: { nextCursor: null },
+ },
]);
});
diff --git a/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.ts b/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.ts
index bcfe73dc2c..d3f29bcc2c 100644
--- a/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.ts
+++ b/apps/web/src/lib/queries/soup/grouped/graphql-optimistic.ts
@@ -2,11 +2,10 @@ import { documentOperationName } from '@graphql-cache/exchange/generated-selecti
import { inspectVariants, selectAll } from '@graphql-cache/exchange/inspection';
import {
type OptimisticUpdate,
- prependUnique,
type QueryRevalidation,
- remove,
+ removeEmbeddedLink,
select,
- update,
+ upsertEmbeddedLink,
} from '@graphql-cache/exchange/optimistic';
import type { CacheHost } from '@graphql-cache/host/types';
import { stringifyDocument } from '@urql/core';
@@ -93,7 +92,8 @@ export function groupPagesByLogicalView(
/**
* Discovers every cached property-grouped field and creates constrained link
* recipes only where the loaded membership proves the move is applicable.
- * Missing bins/pages are left untouched and revalidated after success.
+ * Missing destination bins are created on initial pages; missing pages are
+ * left untouched and revalidated after success.
*/
export async function buildOptimisticGroupedPropertyUpdates(
args: BuildArgs
@@ -164,41 +164,48 @@ export async function buildOptimisticGroupedPropertyUpdates(
continue;
}
- const destinationPages = pages.filter(
- (page) =>
- isInitialInput(page.input) &&
- added.every((key) => page.bins.some((bin) => bin.key === key))
- );
- // Never expose a source-only move when this logical view has nowhere to
- // show the destination. Revalidation will recover absent/new groups.
+ const destinationPages = pages.filter((page) => isInitialInput(page.input));
+ // Never expose a source-only move when this logical view has no initial
+ // page where the destination can be shown.
if (added.length > 0 && destinationPages.length === 0) continue;
for (const page of sourcePages) {
for (const key of removed) {
const source = page.bins.find((bin) => bin.key === key);
if (!source?.items.some((item) => item.id === args.entityId)) continue;
- const items = select(GroupSoupMembershipDocument, {
+ const bins = select(GroupSoupMembershipDocument, {
input: page.input,
})
.field('user')
.field('groupSoup')
- .field('bins')
- .item('key', key)
- .field('items');
- updates.push(update(items, remove(entity)));
+ .field('bins');
+ updates.push(
+ removeEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: key },
+ linkField: 'items',
+ countField: 'totalCount',
+ entity,
+ })
+ );
}
}
for (const page of destinationPages) {
for (const key of added) {
- const items = select(GroupSoupMembershipDocument, {
+ const bins = select(GroupSoupMembershipDocument, {
input: page.input,
})
.field('user')
.field('groupSoup')
- .field('bins')
- .item('key', key)
- .field('items');
- updates.push(update(items, prependUnique(entity)));
+ .field('bins');
+ updates.push(
+ upsertEmbeddedLink(bins, {
+ listItem: { whereField: 'key', equals: key },
+ linkField: 'items',
+ countField: 'totalCount',
+ entity,
+ insertFields: { nextCursor: null },
+ })
+ );
}
}
}
diff --git a/crates/client/cache-core/src/engine.rs b/crates/client/cache-core/src/engine.rs
index 1dce7dd423..168e226a58 100644
--- a/crates/client/cache-core/src/engine.rs
+++ b/crates/client/cache-core/src/engine.rs
@@ -1120,7 +1120,7 @@ impl<S: Storage> Engine<S> {
keys: &BTreeSet<EntityKey<'static>>,
) -> Result<HashMap<EntityKey<'static>, Record>, EngineError<S::Error>> {
let mut out = HashMap::new();
- let mut missing: Vec<EntityKey<'static>> = Vec::new();
+ let mut missing = Vec::new();
for key in keys {
match self.hot.peek(key) {
Some(record) => {
@@ -1166,7 +1166,7 @@ impl<S: Storage> Engine<S> {
match resolve_owner(&effective, &operation, &inspection.path)? {
OwnerResolution::Owner(owner) => break recover_variants(&owner, &prepared)?,
OwnerResolution::Absent => return Ok(Vec::new()),
- OwnerResolution::NeedRecord(key) if !candidates.contains(key.as_ref()) => {
+ OwnerResolution::NeedRecord(key) if !candidates.contains(&key) => {
candidates.insert(key.into_owned());
}
OwnerResolution::NeedRecord(_) => return Ok(Vec::new()),
diff --git a/crates/client/cache-core/src/link_patch.rs b/crates/client/cache-core/src/link_patch.rs
index eef760e549..d38a8c9001 100644
--- a/crates/client/cache-core/src/link_patch.rs
+++ b/crates/client/cache-core/src/link_patch.rs
@@ -22,6 +22,8 @@ pub const MAX_PATCHES: usize = 128;
pub const MAX_PATH_DEPTH: usize = 16;
/// Maximum list length traversed by one recipe.
pub const MAX_TRAVERSED_LIST: usize = 10_000;
+/// Maximum scalar fields used to initialize one embedded list item.
+pub const MAX_INSERT_FIELDS: usize = 16;
/// One constrained step through an embedded cache value.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -51,7 +53,7 @@ pub struct ListItemByScalar {
}
/// Idempotent operation on the selected normalized-link list.
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum LinkOperation {
/// Removes every occurrence of the entity reference.
@@ -66,12 +68,50 @@ pub enum LinkOperation {
#[serde(rename = "entityKey")]
entity_key: EntityKey<'static>,
},
+ /// Removes a link from a matching embedded list item and decrements its
+ /// count when the link was present.
+ RemoveEmbeddedLink {
+ /// Scalar selector identifying the embedded list item.
+ #[serde(rename = "listItem")]
+ list_item: ListItemByScalar,
+ /// Field on the embedded item containing normalized links.
+ #[serde(rename = "linkField")]
+ link_field: FieldKey,
+ /// Non-negative integer field tracking the complete link count.
+ #[serde(rename = "countField")]
+ count_field: FieldKey,
+ /// Normalized entity key to remove from the link field.
+ #[serde(rename = "entityKey")]
+ entity_key: EntityKey<'static>,
+ },
+ /// Prepends a link inside a matching embedded list item, creating that
+ /// embedded item when it is absent, and increments its link count.
+ UpsertEmbeddedLink {
+ /// Scalar selector identifying the embedded list item.
+ #[serde(rename = "listItem")]
+ list_item: ListItemByScalar,
+ /// Field on the embedded item containing normalized links.
+ #[serde(rename = "linkField")]
+ link_field: FieldKey,
+ /// Non-negative integer field tracking the complete link count.
+ #[serde(rename = "countField")]
+ count_field: FieldKey,
+ /// Normalized entity key to prepend to the link field.
+ #[serde(rename = "entityKey")]
+ entity_key: EntityKey<'static>,
+ /// Additional scalar fields used only when creating the embedded item.
+ #[serde(rename = "insertFields")]
+ insert_fields: HashMap<FieldKey, Json>,
+ },
}
impl LinkOperation {
fn entity_key(&self) -> EntityKey<'_> {
match self {
- Self::Remove { entity_key } | Self::PrependUnique { entity_key } => entity_key.borrow(),
+ Self::Remove { entity_key }
+ | Self::PrependUnique { entity_key }
+ | Self::RemoveEmbeddedLink { entity_key, .. }
+ | Self::UpsertEmbeddedLink { entity_key, .. } => entity_key.borrowed(),
}
}
}
@@ -156,6 +196,24 @@ pub enum LinkPatchError {
/// The final list contains values other than normalized refs or nulls.
#[error("link patch target list contains non-link values")]
NonLinkTarget,
+ /// An embedded link count was absent or not a non-negative integer.
+ #[error("embedded link count must be a non-negative integer")]
+ InvalidLinkCount,
+ /// Incrementing an embedded link count exceeded the stored integer range.
+ #[error("embedded link count overflow")]
+ LinkCountOverflow,
+ /// Too many scalar fields were supplied for an embedded item insertion.
+ #[error("embedded insert field count {actual} exceeds limit {maximum}")]
+ TooManyInsertFields { actual: usize, maximum: usize },
+ /// An embedded item insertion field conflicts with a managed field.
+ #[error("embedded insert field `{0}` conflicts with a managed field")]
+ ConflictingInsertField(String),
+ /// Two managed embedded-item fields resolve to the same storage key.
+ #[error("embedded managed fields `{first}` and `{second}` conflict")]
+ ConflictingManagedField { first: String, second: String },
+ /// Embedded item insertion values must be JSON scalars.
+ #[error("embedded insert field values must be JSON scalars")]
+ NonScalarInsertField,
}
/// Removes exact duplicate recipes while retaining the first occurrence and
@@ -194,6 +252,67 @@ fn validate_recipe(patch: &OptimisticLinkPatch) -> Result<(), LinkPatchError> {
return Err(LinkPatchError::NonScalarSelector);
}
}
+ match &patch.operation {
+ LinkOperation::RemoveEmbeddedLink {
+ list_item,
+ link_field,
+ count_field,
+ ..
+ } => validate_embedded_link_fields(list_item, link_field, count_field)?,
+ LinkOperation::UpsertEmbeddedLink {
+ list_item,
+ link_field,
+ count_field,
+ insert_fields,
+ ..
+ } => {
+ validate_embedded_link_fields(list_item, link_field, count_field)?;
+ if insert_fields.len() > MAX_INSERT_FIELDS {
+ return Err(LinkPatchError::TooManyInsertFields {
+ actual: insert_fields.len(),
+ maximum: MAX_INSERT_FIELDS,
+ });
+ }
+ for managed in [&list_item.where_field, link_field, count_field] {
+ if insert_fields.contains_key(managed) {
+ return Err(LinkPatchError::ConflictingInsertField(managed.clone()));
+ }
+ }
+ if insert_fields.values().any(|value| !is_json_scalar(value)) {
+ return Err(LinkPatchError::NonScalarInsertField);
+ }
+ }
+ LinkOperation::Remove { .. } | LinkOperation::PrependUnique { .. } => {}
+ }
+ Ok(())
+}
+
+fn validate_embedded_link_fields(
+ list_item: &ListItemByScalar,
+ link_field: &FieldKey,
+ count_field: &FieldKey,
+) -> Result<(), LinkPatchError> {
+ if !is_json_scalar(&list_item.equals) {
+ return Err(LinkPatchError::NonScalarSelector);
+ }
+ if list_item.where_field == *link_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: list_item.where_field.clone(),
+ second: link_field.clone(),
+ });
+ }
+ if list_item.where_field == *count_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: list_item.where_field.clone(),
+ second: count_field.clone(),
+ });
+ }
+ if link_field == count_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: link_field.clone(),
+ second: count_field.clone(),
+ });
+ }
Ok(())
}
@@ -289,7 +408,58 @@ fn apply_one(
field: resolved.field_key.clone(),
})?;
let target = traverse(&mut field_value, &resolved.path)?;
- let CacheValue::List(links) = target else {
+ match &patch.operation {
+ LinkOperation::Remove { entity_key } => {
+ let links = normalized_links(target)?;
+ links.retain(
+ |value| !matches!(value, CacheValue::Ref(key) if key.as_ref() == entity_key.as_ref()),
+ );
+ }
+ LinkOperation::PrependUnique { entity_key } => {
+ prepend_unique(normalized_links(target)?, entity_key.borrowed());
+ }
+ LinkOperation::RemoveEmbeddedLink { entity_key, .. } => {
+ let fields = resolved
+ .embedded_link
+ .as_ref()
+ .ok_or(LinkPatchError::WrongShape)?;
+ remove_embedded_link(
+ target,
+ &fields.list_item,
+ &fields.link_field,
+ &fields.count_field,
+ entity_key.borrowed(),
+ )?;
+ }
+ LinkOperation::UpsertEmbeddedLink { entity_key, .. } => {
+ let fields = resolved
+ .embedded_link
+ .as_ref()
+ .ok_or(LinkPatchError::WrongShape)?;
+ upsert_embedded_link(
+ target,
+ &fields.list_item,
+ &fields.link_field,
+ &fields.count_field,
+ entity_key.borrowed(),
+ &fields.insert_fields,
+ )?;
+ }
+ }
+
+ record
+ .fields
+ .insert(resolved.field_key.clone(), field_value.clone());
+ updates
+ .entry(resolved.parent_entity_key)
+ .or_default()
+ .fields
+ .insert(resolved.field_key, field_value);
+ Ok(())
+}
+
+fn normalized_links(value: &mut CacheValue) -> Result<&mut Vec<CacheValue>, LinkPatchError> {
+ let CacheValue::List(links) = value else {
return Err(LinkPatchError::WrongShape);
};
if links.len() > MAX_TRAVERSED_LIST {
@@ -304,31 +474,186 @@ fn apply_one(
{
return Err(LinkPatchError::NonLinkTarget);
}
+ Ok(links)
+}
+
+fn prepend_unique(links: &mut Vec<CacheValue>, entity_key: EntityKey<'_>) {
+ links.retain(
+ |value| !matches!(value, CacheValue::Ref(key) if key.as_ref() == entity_key.as_ref()),
+ );
+ links.insert(0, CacheValue::Ref(entity_key.into_owned()));
+}
- let entity_key = patch.operation.entity_key();
+fn remove_embedded_link(
+ value: &mut CacheValue,
+ list_item: &ListItemByScalar,
+ link_field: &FieldKey,
+ count_field: &FieldKey,
+ entity_key: EntityKey<'_>,
+) -> Result<(), LinkPatchError> {
+ let items = embedded_items(value)?;
+ let index =
+ embedded_item_index(items, list_item)?.ok_or(LinkPatchError::SelectorMatchCount(0))?;
+ let CacheValue::Object(object) = &mut items[index] else {
+ return Err(LinkPatchError::WrongShape);
+ };
+ let links = object
+ .get_mut(link_field)
+ .ok_or(LinkPatchError::WrongShape)?;
+ let links = normalized_links(links)?;
+ let was_present = contains_link(links, &entity_key);
links.retain(
- |value| !matches!(value, CacheValue::Ref(key) if key.0.as_ref() == entity_key.0.as_ref()),
+ |value| !matches!(value, CacheValue::Ref(key) if key.as_ref() == entity_key.as_ref()),
);
- if matches!(patch.operation, LinkOperation::PrependUnique { .. }) {
- links.insert(0, CacheValue::Ref(entity_key.into_owned()));
+ if was_present {
+ adjust_link_count(object, count_field, CountAdjustment::Decrement)?;
}
+ Ok(())
+}
- record
- .fields
- .insert(resolved.field_key.clone(), field_value.clone());
- updates
- .entry(resolved.parent_entity_key)
- .or_default()
- .fields
- .insert(resolved.field_key, field_value);
+fn upsert_embedded_link(
+ value: &mut CacheValue,
+ list_item: &ListItemByScalar,
+ link_field: &FieldKey,
+ count_field: &FieldKey,
+ entity_key: EntityKey<'_>,
+ insert_fields: &HashMap<FieldKey, Json>,
+) -> Result<(), LinkPatchError> {
+ let items = embedded_items(value)?;
+ match embedded_item_index(items, list_item)? {
+ Some(index) => {
+ let CacheValue::Object(object) = &mut items[index] else {
+ return Err(LinkPatchError::WrongShape);
+ };
+ let links = object
+ .get_mut(link_field)
+ .ok_or(LinkPatchError::WrongShape)?;
+ let links = normalized_links(links)?;
+ let was_present = contains_link(links, &entity_key);
+ prepend_unique(links, entity_key);
+ if !was_present {
+ adjust_link_count(object, count_field, CountAdjustment::Increment)?;
+ }
+ }
+ None => {
+ let mut object = insert_fields
+ .iter()
+ .map(|(field, value)| Ok((field.clone(), cache_scalar(value)?)))
+ .collect::<Result<std::collections::BTreeMap<_, _>, LinkPatchError>>()?;
+ object.insert(
+ list_item.where_field.clone(),
+ cache_scalar(&list_item.equals)?,
+ );
+ object.insert(
+ count_field.clone(),
+ CacheValue::Number(CacheNumber::PosInt(1)),
+ );
+ object.insert(
+ link_field.clone(),
+ CacheValue::List(vec![CacheValue::Ref(entity_key.into_owned())]),
+ );
+ items.insert(0, CacheValue::Object(object));
+ }
+ }
Ok(())
}
+fn embedded_items(value: &mut CacheValue) -> Result<&mut Vec<CacheValue>, LinkPatchError> {
+ let CacheValue::List(items) = value else {
+ return Err(LinkPatchError::WrongShape);
+ };
+ if items.len() > MAX_TRAVERSED_LIST {
+ return Err(LinkPatchError::ListTooLarge {
+ actual: items.len(),
+ maximum: MAX_TRAVERSED_LIST,
+ });
+ }
+ if items
+ .iter()
+ .any(|value| !matches!(value, CacheValue::Object(_)))
+ {
+ return Err(LinkPatchError::WrongShape);
+ }
+ Ok(items)
+}
+
+fn embedded_item_index(
+ items: &[CacheValue],
+ list_item: &ListItemByScalar,
+) -> Result<Option<usize>, LinkPatchError> {
+ let matches: Vec<_> = items
+ .iter()
+ .enumerate()
+ .filter_map(|(index, value)| {
+ let CacheValue::Object(object) = value else {
+ return None;
+ };
+ object
+ .get(&list_item.where_field)
+ .is_some_and(|value| cache_scalar_equals(value, &list_item.equals))
+ .then_some(index)
+ })
+ .collect();
+ match matches.as_slice() {
+ [] => Ok(None),
+ [index] => Ok(Some(*index)),
+ _ => Err(LinkPatchError::SelectorMatchCount(matches.len())),
+ }
+}
+
+fn contains_link(links: &[CacheValue], entity_key: &EntityKey<'_>) -> bool {
+ links
+ .iter()
+ .any(|value| matches!(value, CacheValue::Ref(key) if key.as_ref() == entity_key.as_ref()))
+}
+
+#[derive(Clone, Copy)]
+enum CountAdjustment {
+ Increment,
+ Decrement,
+}
+
+fn adjust_link_count(
+ object: &mut std::collections::BTreeMap<FieldKey, CacheValue>,
+ count_field: &FieldKey,
+ adjustment: CountAdjustment,
+) -> Result<(), LinkPatchError> {
+ let Some(CacheValue::Number(CacheNumber::PosInt(count))) = object.get_mut(count_field) else {
+ return Err(LinkPatchError::InvalidLinkCount);
+ };
+ *count = match adjustment {
+ CountAdjustment::Increment => count
+ .checked_add(1)
+ .ok_or(LinkPatchError::LinkCountOverflow)?,
+ CountAdjustment::Decrement => count.saturating_sub(1),
+ };
+ Ok(())
+}
+
+fn cache_scalar(value: &Json) -> Result<CacheValue, LinkPatchError> {
+ match value {
+ Json::Null => Ok(CacheValue::Null),
+ Json::Bool(value) => Ok(CacheValue::Bool(*value)),
+ Json::Number(value) => Ok(CacheValue::Number(value.into())),
+ Json::String(value) => Ok(CacheValue::String(value.clone())),
+ Json::Array(_) | Json::Object(_) => Err(LinkPatchError::NonScalarInsertField),
+ }
+}
+
#[derive(Debug)]
struct ResolvedTarget {
parent_entity_key: EntityKey<'static>,
field_key: FieldKey,
path: Vec<LinkPathSegment>,
+ embedded_link: Option<ResolvedEmbeddedLink>,
+}
+
+#[derive(Debug)]
+struct ResolvedEmbeddedLink {
+ list_item: ListItemByScalar,
+ link_field: FieldKey,
+ count_field: FieldKey,
+ insert_fields: HashMap<FieldKey, Json>,
}
/// Returns the next normalized record required to resolve one query-rooted
@@ -361,6 +686,7 @@ fn resolve_target(
&operation.selection_set,
&variables,
&patch.path,
+ &patch.operation,
)
}
@@ -371,6 +697,7 @@ fn resolve_from_record(
selections: &[Selection],
variables: &serde_json::Map<String, Json>,
path: &[LinkPathSegment],
+ operation: &LinkOperation,
) -> Result<ResolvedTarget, LinkPatchError> {
let Some(LinkPathSegment::Field {
field: response_key,
@@ -404,6 +731,7 @@ fn resolve_from_record(
selections: &selected.selection_set,
},
&path[1..],
+ operation,
)
}
@@ -421,12 +749,19 @@ fn resolve_from_value(
variables: &serde_json::Map<String, Json>,
cursor: ValueCursor<'_>,
path: &[LinkPathSegment],
+ operation: &LinkOperation,
) -> Result<ResolvedTarget, LinkPatchError> {
if path.is_empty() {
return Ok(ResolvedTarget {
parent_entity_key: cursor.owner,
field_key: cursor.anchor_field,
path: cursor.relative_path,
+ embedded_link: resolve_embedded_link_fields(
+ cursor.selections,
+ cursor.type_name,
+ variables,
+ operation,
+ )?,
});
}
@@ -438,6 +773,7 @@ fn resolve_from_value(
cursor.selections,
variables,
path,
+ operation,
);
}
@@ -474,6 +810,7 @@ fn resolve_from_value(
selections: &selected.selection_set,
},
&path[1..],
+ operation,
)
}
(LinkPathSegment::ListItem { list_item }, CacheValue::List(items)) => {
@@ -521,12 +858,93 @@ fn resolve_from_value(
selections: cursor.selections,
},
&path[1..],
+ operation,
)
}
_ => Err(LinkPatchError::WrongShape),
}
}
+fn resolve_embedded_link_fields(
+ selections: &[Selection],
+ concrete: &str,
+ variables: &serde_json::Map<String, Json>,
+ operation: &LinkOperation,
+) -> Result<Option<ResolvedEmbeddedLink>, LinkPatchError> {
+ let (list_item, link_field, count_field, insert_fields) = match operation {
+ LinkOperation::RemoveEmbeddedLink {
+ list_item,
+ link_field,
+ count_field,
+ ..
+ } => (list_item, link_field, count_field, None),
+ LinkOperation::UpsertEmbeddedLink {
+ list_item,
+ link_field,
+ count_field,
+ insert_fields,
+ ..
+ } => (list_item, link_field, count_field, Some(insert_fields)),
+ LinkOperation::Remove { .. } | LinkOperation::PrependUnique { .. } => return Ok(None),
+ };
+
+ let selector_key = selected_storage_key(
+ selected_field(selections, concrete, &list_item.where_field)?,
+ variables,
+ )?;
+ let resolved_link_field =
+ selected_storage_key(selected_field(selections, concrete, link_field)?, variables)?;
+ let resolved_count_field = selected_storage_key(
+ selected_field(selections, concrete, count_field)?,
+ variables,
+ )?;
+ let mut resolved_insert_fields = HashMap::new();
+ if let Some(insert_fields) = insert_fields {
+ for (field, value) in insert_fields {
+ let storage_key =
+ selected_storage_key(selected_field(selections, concrete, field)?, variables)?;
+ if resolved_insert_fields.contains_key(&storage_key) {
+ return Err(LinkPatchError::ConflictingInsertField(storage_key));
+ }
+ resolved_insert_fields.insert(storage_key, value.clone());
+ }
+ }
+
+ for managed in [&selector_key, &resolved_link_field, &resolved_count_field] {
+ if resolved_insert_fields.contains_key(managed) {
+ return Err(LinkPatchError::ConflictingInsertField(managed.clone()));
+ }
+ }
+ if selector_key == resolved_link_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: selector_key,
+ second: resolved_link_field,
+ });
+ }
+ if selector_key == resolved_count_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: selector_key,
+ second: resolved_count_field,
+ });
+ }
+ if resolved_link_field == resolved_count_field {
+ return Err(LinkPatchError::ConflictingManagedField {
+ first: resolved_link_field,
+ second: resolved_count_field,
+ });
+ }
+
+ Ok(Some(ResolvedEmbeddedLink {
+ list_item: ListItemByScalar {
+ where_field: selector_key,
+ equals: list_item.equals.clone(),
+ },
+ link_field: resolved_link_field,
+ count_field: resolved_count_field,
+ insert_fields: resolved_insert_fields,
+ }))
+}
+
fn selected_field<'a>(
selections: &'a [Selection],
concrete: &str,
@@ -622,13 +1040,19 @@ mod tests {
use serde_json::json;
use std::collections::BTreeMap;
- const QUERY: &str = "query { user { groupSoup { bins { key items { id } } } } }";
+ const QUERY: &str =
+ "query { user { groupSoup { bins { key totalCount nextCursor items { id } } } } }";
fn record() -> (EntityKey<'static>, Record) {
let parent = EntityKey("GraphqlUser:user-1".into());
- let bin = |key: &str, items: Vec<CacheValue>| {
+ let bin = |key: &str, total_count: u64, items: Vec<CacheValue>| {
CacheValue::Object(BTreeMap::from([
("key".into(), CacheValue::String(key.into())),
+ (
+ "totalCount".into(),
+ CacheValue::Number(CacheNumber::PosInt(total_count)),
+ ),
+ ("nextCursor".into(), CacheValue::Null),
("items".into(), CacheValue::List(items)),
]))
};
@@ -637,12 +1061,13 @@ mod tests {
CacheValue::List(vec![
bin(
"in-progress",
+ 1,
vec![
CacheValue::Ref(EntityKey("GraphqlSoupItem:task-1".into())),
CacheValue::Ref(EntityKey("GraphqlSoupItem:task-1".into())),
],
),
- bin("completed", vec![]),
+ bin("completed", 0, vec![]),
]),
)]));
let record = Record {
@@ -680,6 +1105,139 @@ mod tests {
}
}
+ fn upsert_bin_patch(bin: &str) -> OptimisticLinkPatch {
+ OptimisticLinkPatch {
+ query: QUERY.into(),
+ operation_name: None,
+ variables_json: "{}".into(),
+ path: vec![
+ LinkPathSegment::Field {
+ field: "user".into(),
+ },
+ LinkPathSegment::Field {
+ field: "groupSoup".into(),
+ },
+ LinkPathSegment::Field {
+ field: "bins".into(),
+ },
+ ],
+ operation: LinkOperation::UpsertEmbeddedLink {
+ list_item: ListItemByScalar {
+ where_field: "key".into(),
+ equals: json!(bin),
+ },
+ link_field: "items".into(),
+ count_field: "totalCount".into(),
+ entity_key: EntityKey("GraphqlSoupItem:task-1".into()),
+ insert_fields: HashMap::from([("nextCursor".into(), Json::Null)]),
+ },
+ }
+ }
+
+ fn remove_bin_patch(bin: &str) -> OptimisticLinkPatch {
+ OptimisticLinkPatch {
+ query: QUERY.into(),
+ operation_name: None,
+ variables_json: "{}".into(),
+ path: vec![
+ LinkPathSegment::Field {
+ field: "user".into(),
+ },
+ LinkPathSegment::Field {
+ field: "groupSoup".into(),
+ },
+ LinkPathSegment::Field {
+ field: "bins".into(),
+ },
+ ],
+ operation: LinkOperation::RemoveEmbeddedLink {
+ list_item: ListItemByScalar {
+ where_field: "key".into(),
+ equals: json!(bin),
+ },
+ link_field: "items".into(),
+ count_field: "totalCount".into(),
+ entity_key: EntityKey("GraphqlSoupItem:task-1".into()),
+ },
+ }
+ }
+
+ #[test]
+ fn rejects_conflicting_embedded_managed_fields() {
+ let mut direct_conflict = upsert_bin_patch("urgent");
+ let LinkOperation::UpsertEmbeddedLink { link_field, .. } = &mut direct_conflict.operation
+ else {
+ panic!()
+ };
+ *link_field = "key".into();
+ assert_eq!(
+ deduplicate_patches(&[direct_conflict]).unwrap_err(),
+ LinkPatchError::ConflictingManagedField {
+ first: "key".into(),
+ second: "key".into(),
+ }
+ );
+
+ let (parent, initial) = record();
+ let effective = HashMap::from([
+ (
+ EntityKey::root(),
+ Record {
+ fields: BTreeMap::from([("user".into(), CacheValue::Ref(parent.clone()))]),
+ },
+ ),
+ (parent, initial),
+ ]);
+ let mut resolved_conflict = upsert_bin_patch("urgent");
+ resolved_conflict.query = "query { user { groupSoup { bins { selector: key link: key totalCount nextCursor items { id } } } } }".into();
+ let LinkOperation::UpsertEmbeddedLink {
+ list_item,
+ link_field,
+ ..
+ } = &mut resolved_conflict.operation
+ else {
+ panic!()
+ };
+ list_item.where_field = "selector".into();
+ *link_field = "link".into();
+ assert_eq!(
+ resolve_target(&effective, &resolved_conflict).unwrap_err(),
+ LinkPatchError::ConflictingManagedField {
+ first: "key".into(),
+ second: "key".into(),
+ }
+ );
+ }
+
+ #[test]
+ fn rejects_insert_fields_resolving_to_the_same_storage_key() {
+ let (parent, initial) = record();
+ let effective = HashMap::from([
+ (
+ EntityKey::root(),
+ Record {
+ fields: BTreeMap::from([("user".into(), CacheValue::Ref(parent.clone()))]),
+ },
+ ),
+ (parent, initial),
+ ]);
+ let mut duplicate = upsert_bin_patch("urgent");
+ duplicate.query = "query { user { groupSoup { bins { key totalCount firstCursor: nextCursor secondCursor: nextCursor items { id } } } } }".into();
+ let LinkOperation::UpsertEmbeddedLink { insert_fields, .. } = &mut duplicate.operation
+ else {
+ panic!()
+ };
+ *insert_fields = HashMap::from([
+ ("firstCursor".into(), Json::Null),
+ ("secondCursor".into(), Json::Null),
+ ]);
+
+ assert_eq!(
+ resolve_target(&effective, &duplicate).unwrap_err(),
+ LinkPatchError::ConflictingInsertField("nextCursor".into())
+ );
+ }
+
#[test]
fn remove_and_prepend_compose_without_touching_unrelated_values() {
let (parent, initial) = record();
@@ -738,6 +1296,94 @@ mod tests {
assert_eq!(updates.len(), 1);
}
+ #[test]
+ fn embedded_link_changes_create_bins_and_adjust_counts_once() {
+ let (parent, initial) = record();
+ let mut effective = HashMap::from([
+ (
+ EntityKey::root(),
+ Record {
+ fields: BTreeMap::from([("user".into(), CacheValue::Ref(parent.clone()))]),
+ },
+ ),
+ (parent.clone(), initial),
+ ]);
+ let mut updates = RecordUpdates::new();
+ let urgent = upsert_bin_patch("urgent");
+ apply_link_patches(
+ &mut effective,
+ &mut updates,
+ &[urgent.clone(), urgent.clone()],
+ false,
+ )
+ .unwrap();
+ apply_link_patches(&mut effective, &mut updates, &[urgent], false).unwrap();
+
+ let completed = upsert_bin_patch("completed");
+ apply_link_patches(
+ &mut effective,
+ &mut updates,
+ std::slice::from_ref(&completed),
+ false,
+ )
+ .unwrap();
+ apply_link_patches(&mut effective, &mut updates, &[completed], false).unwrap();
+
+ let source = remove_bin_patch("in-progress");
+ apply_link_patches(
+ &mut effective,
+ &mut updates,
+ std::slice::from_ref(&source),
+ false,
+ )
+ .unwrap();
+ apply_link_patches(&mut effective, &mut updates, &[source], false).unwrap();
+
+ let CacheValue::Object(grouped) = &effective[&parent].fields["groupSoup"] else {
+ panic!()
+ };
+ let CacheValue::List(bins) = &grouped["bins"] else {
+ panic!()
+ };
+ let CacheValue::Object(urgent) = &bins[0] else {
+ panic!()
+ };
+ assert_eq!(urgent["key"], CacheValue::String("urgent".into()));
+ assert_eq!(
+ urgent["totalCount"],
+ CacheValue::Number(CacheNumber::PosInt(1))
+ );
+ assert_eq!(urgent["nextCursor"], CacheValue::Null);
+ assert_eq!(
+ urgent["items"],
+ CacheValue::List(vec![CacheValue::Ref(EntityKey(
+ "GraphqlSoupItem:task-1".into()
+ ))])
+ );
+ let CacheValue::Object(source) = &bins[1] else {
+ panic!()
+ };
+ assert_eq!(
+ source["totalCount"],
+ CacheValue::Number(CacheNumber::PosInt(0))
+ );
+ assert_eq!(source["items"], CacheValue::List(vec![]));
+ let CacheValue::Object(completed) = &bins[2] else {
+ panic!()
+ };
+ assert_eq!(
+ completed["totalCount"],
+ CacheValue::Number(CacheNumber::PosInt(1))
+ );
+ assert_eq!(
+ completed["items"],
+ CacheValue::List(vec![CacheValue::Ref(EntityKey(
+ "GraphqlSoupItem:task-1".into()
+ ))])
+ );
+ assert_eq!(updates.len(), 1);
+ }
+
#[test]
fn strict_patch_set_has_no_partial_writes() {
let (parent, initial) = record();
diff --git a/crates/client/cache-core/src/query_inspection.rs b/crates/client/cache-core/src/query_inspection.rs
index 398a3985cc..7fb8349c52 100644
--- a/crates/client/cache-core/src/query_inspection.rs
+++ b/crates/client/cache-core/src/query_inspection.rs
@@ -203,7 +203,7 @@ fn resolve_record_owner<'records, 'selection>(
selections: &'selection [Selection],
path: &[String],
) -> Result<OwnerResolution<'records, 'selection>, QueryInspectionError> {
- let Some(record) = records.get(key.as_ref()) else {
+ let Some(record) = records.get(&key) else {
return Ok(OwnerResolution::NeedRecord(key));
};
let concrete = record.typename().unwrap_or(declared_type);
@@ -238,7 +238,7 @@ fn resolve_fields_owner<'records, 'selection>(
match value {
CacheValue::Ref(key) => resolve_record_owner(
records,
- key.borrow(),
+ key.borrowed(),
next_type,
&field.selection_set,
&path[1..],
diff --git a/crates/client/cache-core/src/store.rs b/crates/client/cache-core/src/store.rs
index eabd08beab..8cc65b46f3 100644
--- a/crates/client/cache-core/src/store.rs
+++ b/crates/client/cache-core/src/store.rs
@@ -23,7 +23,7 @@ pub trait Storage: MaybeSend {
/// Fetches records; result is aligned with `keys` (`None` = absent).
fn get_batch(
&self,
- keys: &[EntityKey<'static>],
+ keys: &[EntityKey<'_>],
) -> impl Future<Output = Result<Vec<Option<Record>>, Self::Error>> + MaybeSend;
/// Upserts records atomically (all-or-nothing per batch).
@@ -129,10 +129,7 @@ impl InMemoryStorage {
impl Storage for InMemoryStorage {
type Error = Infallible;
- async fn get_batch(
- &self,
- keys: &[EntityKey<'static>],
- ) -> Result<Vec<Option<Record>>, Self::Error> {
+ async fn get_batch(&self, keys: &[EntityKey<'_>]) -> Result<Vec<Option<Record>>, Self::Error> {
Ok(keys.iter().map(|k| self.records.get(k).cloned()).collect())
}
diff --git a/crates/client/cache-core/src/value.rs b/crates/client/cache-core/src/value.rs
index 35c5bdbf57..9a46a59475 100644
--- a/crates/client/cache-core/src/value.rs
+++ b/crates/client/cache-core/src/value.rs
@@ -2,7 +2,7 @@
//! values.
use serde::{Deserialize, Serialize};
-use std::borrow::{Borrow, Cow};
+use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;
@@ -36,7 +36,7 @@ impl<'a> EntityKey<'a> {
self.0 == ROOT_QUERY
}
- pub fn borrow(&self) -> EntityKey<'_> {
+ pub fn borrowed(&self) -> EntityKey<'_> {
EntityKey(Cow::Borrowed(self.0.as_ref()))
}
@@ -46,12 +46,6 @@ impl<'a> EntityKey<'a> {
}
}
-impl Borrow<str> for EntityKey<'_> {
- fn borrow(&self) -> &str {
- self.0.as_ref()
- }
-}
-
impl AsRef<str> for EntityKey<'_> {
fn as_ref(&self) -> &str {
self.0.as_ref()
diff --git a/crates/client/cache-core/tests/grouped_optimistic.rs b/crates/client/cache-core/tests/grouped_optimistic.rs
index cea0bf4ee5..f0b275fb23 100644
--- a/crates/client/cache-core/tests/grouped_optimistic.rs
+++ b/crates/client/cache-core/tests/grouped_optimistic.rs
@@ -39,6 +39,22 @@ query GroupSoup($input: GroupedSoupInput!) {
}
"#;
+const GROUP_MEMBERSHIP_QUERY: &str = r#"
+query GroupSoupMembership($input: GroupedSoupInput!) {
+ user {
+ id
+ groupSoup(input: $input) {
+ bins {
+ key
+ totalCount
+ nextCursor
+ items { __typename id }
+ }
+ }
+ }
+}
+"#;
+
const MUTATION: &str = r#"
mutation SetEntityProperty($input: SetEntityPropertyInput!) {
setEntityProperty(input: $input) {
@@ -122,10 +138,19 @@ fn mutation_response(option: &str) -> Json {
json!({ "setEntityProperty": property(option) })
}
+fn group_page_without_destination() -> Json {
+ let mut page = group_page();
+ page["user"]["groupSoup"]["bins"]
+ .as_array_mut()
+ .unwrap()
+ .truncate(1);
+ page
+}
+
fn patch(bin: &str, operation: LinkOperation) -> OptimisticLinkPatch {
OptimisticLinkPatch {
- query: GROUP_QUERY.into(),
- operation_name: Some("GroupSoup".into()),
+ query: GROUP_MEMBERSHIP_QUERY.into(),
+ operation_name: Some("GroupSoupMembership".into()),
variables_json: serde_json::to_string(&query_variables()).unwrap(),
path: vec![
LinkPathSegment::Field {
@@ -151,6 +176,63 @@ fn patch(bin: &str, operation: LinkOperation) -> OptimisticLinkPatch {
}
}
+fn remove_bin_patch(bin: &str) -> OptimisticLinkPatch {
+ OptimisticLinkPatch {
+ query: GROUP_MEMBERSHIP_QUERY.into(),
+ operation_name: Some("GroupSoupMembership".into()),
+ variables_json: serde_json::to_string(&query_variables()).unwrap(),
+ path: vec![
+ LinkPathSegment::Field {
+ field: "user".into(),
+ },
+ LinkPathSegment::Field {
+ field: "groupSoup".into(),
+ },
+ LinkPathSegment::Field {
+ field: "bins".into(),
+ },
+ ],
+ operation: LinkOperation::RemoveEmbeddedLink {
+ list_item: ListItemByScalar {
+ where_field: "key".into(),
+ equals: json!(bin),
+ },
+ link_field: "items".into(),
+ count_field: "totalCount".into(),
+ entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
+ },
+ }
+}
+
+fn upsert_bin_patch(bin: &str) -> OptimisticLinkPatch {
+ OptimisticLinkPatch {
+ query: GROUP_MEMBERSHIP_QUERY.into(),
+ operation_name: Some("GroupSoupMembership".into()),
+ variables_json: serde_json::to_string(&query_variables()).unwrap(),
+ path: vec![
+ LinkPathSegment::Field {
+ field: "user".into(),
+ },
+ LinkPathSegment::Field {
+ field: "groupSoup".into(),
+ },
+ LinkPathSegment::Field {
+ field: "bins".into(),
+ },
+ ],
+ operation: LinkOperation::UpsertEmbeddedLink {
+ list_item: ListItemByScalar {
+ where_field: "key".into(),
+ equals: json!(bin),
+ },
+ link_field: "items".into(),
+ count_field: "totalCount".into(),
+ entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
+ insert_fields: std::collections::HashMap::from([("nextCursor".into(), Json::Null)]),
+ },
+ }
+}
+
async fn read_group(engine: &mut Engine<InMemoryStorage>) -> Json {
match engine
.read_query(None, GROUP_QUERY, Some("GroupSoup"), &query_variables())
@@ -163,6 +245,10 @@ async fn read_group(engine: &mut Engine<InMemoryStorage>) -> Json {
}
async fn setup() -> Engine<InMemoryStorage> {
+ setup_with_page(group_page()).await
+}
+
+async fn setup_with_page(page: Json) -> Engine<InMemoryStorage> {
let mut engine = Engine::new(InMemoryStorage::new());
engine
.write_query(
@@ -170,7 +256,7 @@ async fn setup() -> Engine<InMemoryStorage> {
GROUP_QUERY,
Some("GroupSoup"),
&query_variables(),
- &group_page(),
+ &page,
None,
)
.await
@@ -200,18 +286,8 @@ fn cache_only_read_observes_move_and_rollback_restores_it() {
block_on(async {
let mut engine = setup().await;
let patches = [
- patch(
- "in-progress",
- LinkOperation::Remove {
- entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
- },
- ),
- patch(
- "completed",
- LinkOperation::PrependUnique {
- entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
- },
- ),
+ remove_bin_patch("in-progress"),
+ upsert_bin_patch("completed"),
];
let (transaction, _) = engine
.begin_optimistic_write(
@@ -233,9 +309,9 @@ fn cache_only_read_observes_move_and_rollback_restores_it() {
let bins = optimistic["user"]["groupSoup"]["bins"].as_array().unwrap();
assert!(bins[0]["items"].as_array().unwrap().is_empty());
assert_eq!(bins[1]["items"][0]["id"], json!("task-1"));
- // Server-owned pagination metadata is deliberately untouched.
- assert_eq!(bins[0]["totalCount"], json!(1));
- assert_eq!(bins[1]["totalCount"], json!(0));
+ assert_eq!(bins[0]["totalCount"], json!(0));
+ assert_eq!(bins[1]["totalCount"], json!(1));
+ // Server-owned pagination cursors are deliberately untouched.
assert_eq!(bins[1]["nextCursor"], json!("destination-cursor"));
// Durable recipes reconstruct the complete property + relation layer
@@ -252,6 +328,14 @@ fn cache_only_read_observes_move_and_rollback_restores_it() {
hydrated["user"]["groupSoup"]["bins"][1]["items"][0]["id"],
json!("task-1")
);
+ assert_eq!(
+ hydrated["user"]["groupSoup"]["bins"][0]["totalCount"],
+ json!(0)
+ );
+ assert_eq!(
+ hydrated["user"]["groupSoup"]["bins"][1]["totalCount"],
+ json!(1)
+ );
let claim = claim(&mut engine, transaction).await;
engine
@@ -269,6 +353,14 @@ fn cache_only_read_observes_move_and_rollback_restores_it() {
.unwrap()
.is_empty()
);
+ assert_eq!(
+ restored["user"]["groupSoup"]["bins"][0]["totalCount"],
+ json!(1)
+ );
+ assert_eq!(
+ restored["user"]["groupSoup"]["bins"][1]["totalCount"],
+ json!(0)
+ );
});
}
@@ -277,18 +369,8 @@ fn success_reapplies_recipe_and_returns_deduplicated_revalidation() {
block_on(async {
let mut engine = setup().await;
let patches = [
- patch(
- "in-progress",
- LinkOperation::Remove {
- entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
- },
- ),
- patch(
- "completed",
- LinkOperation::PrependUnique {
- entity_key: EntityKey("GraphqlSoupDocument:task-1".into()),
- },
- ),
+ remove_bin_patch("in-progress"),
+ upsert_bin_patch("completed"),
];
let (transaction, _) = engine
.begin_optimistic_write(
@@ -315,6 +397,7 @@ fn success_reapplies_recipe_and_returns_deduplicated_revalidation() {
"id": "task-2",
"properties": []
}]);
+ concurrent["user"]["groupSoup"]["bins"][1]["totalCount"] = json!(1);
engine
.write_query(
None,
@@ -349,6 +432,80 @@ fn success_reapplies_recipe_and_returns_deduplicated_revalidation() {
committed["user"]["groupSoup"]["bins"][1]["items"][1]["id"],
json!("task-2")
);
+ assert_eq!(
+ committed["user"]["groupSoup"]["bins"][0]["totalCount"],
+ json!(0)
+ );
+ assert_eq!(
+ committed["user"]["groupSoup"]["bins"][1]["totalCount"],
+ json!(2)
+ );
+ });
+}
+
+#[test]
+fn missing_destination_is_created_with_the_updated_item() {
+ block_on(async {
+ let mut engine = setup_with_page(group_page_without_destination()).await;
+ let patches = [
+ remove_bin_patch("in-progress"),
+ upsert_bin_patch("completed"),
+ ];
+ let (transaction, _) = engine
+ .begin_optimistic_write(
+ None,
+ BeginOptimisticWrite {
+ query: MUTATION,
+ operation_name: Some("SetEntityProperty"),
+ variables: &mutation_variables("completed"),
+ data: &mutation_response("completed"),
+ link_patches: &patches,
+ revalidations: &[],
+ created_at_ms: 0,
+ },
+ )
+ .await
+ .unwrap();
+
+ let optimistic = read_group(&mut engine).await;
+ let bins = optimistic["user"]["groupSoup"]["bins"].as_array().unwrap();
+ assert_eq!(bins.len(), 2);
+ assert_eq!(bins[0]["key"], json!("completed"));
+ assert_eq!(bins[0]["totalCount"], json!(1));
+ assert_eq!(bins[0]["nextCursor"], Json::Null);
+ assert_eq!(bins[0]["items"][0]["id"], json!("task-1"));
+ assert_eq!(
+ bins[0]["items"][0]["properties"][0]["value"]["optionIds"],
+ json!(["completed"])
+ );
+ assert!(bins[1]["items"].as_array().unwrap().is_empty());
+ assert_eq!(bins[1]["totalCount"], json!(0));
+
+ let claim = claim(&mut engine, transaction).await;
+ engine
+ .commit_optimistic_write(
+ transaction,
+ claim,
+ MUTATION,
+ Some("SetEntityProperty"),
+ &mutation_variables("completed"),
+ &mutation_response("completed"),
+ )
+ .await
+ .unwrap();
+ let committed = read_group(&mut engine).await;
+ assert_eq!(
+ committed["user"]["groupSoup"]["bins"][0]["items"][0]["id"],
+ json!("task-1")
+ );
+ assert_eq!(
+ committed["user"]["groupSoup"]["bins"][0]["totalCount"],
+ json!(1)
+ );
+ assert_eq!(
+ committed["user"]["groupSoup"]["bins"][1]["totalCount"],
+ json!(0)
+ );
});
}
diff --git a/crates/client/cache-core/tests/optimistic.rs b/crates/client/cache-core/tests/optimistic.rs
index 0463f95371..0d9f335c18 100644
--- a/crates/client/cache-core/tests/optimistic.rs
+++ b/crates/client/cache-core/tests/optimistic.rs
@@ -71,10 +71,7 @@ impl ClaimFailingStorage {
impl Storage for ClaimFailingStorage {
type Error = std::io::Error;
- async fn get_batch(
- &self,
- keys: &[EntityKey<'static>],
- ) -> Result<Vec<Option<Record>>, Self::Error> {
+ async fn get_batch(&self, keys: &[EntityKey<'_>]) -> Result<Vec<Option<Record>>, Self::Error> {
Ok(self.inner.get_batch(keys).await.unwrap())
}
diff --git a/crates/client/cache-core/tests/query_inspection.rs b/crates/client/cache-core/tests/query_inspection.rs
index 79c669872e..9d78b11062 100644
--- a/crates/client/cache-core/tests/query_inspection.rs
+++ b/crates/client/cache-core/tests/query_inspection.rs
@@ -88,10 +88,7 @@ struct OwnerOnlyStorage(InMemoryStorage);
impl Storage for OwnerOnlyStorage {
type Error = std::convert::Infallible;
- async fn get_batch(
- &self,
- keys: &[EntityKey<'static>],
- ) -> Result<Vec<Option<Record>>, Self::Error> {
+ async fn get_batch(&self, keys: &[EntityKey<'_>]) -> Result<Vec<Option<Record>>, Self::Error> {
assert!(
keys.iter()
.all(|key| key.is_root() || key.0 == "GraphqlUser:user-1"),
diff --git a/crates/client/cache-idb/src/idb_storage.rs b/crates/client/cache-idb/src/idb_storage.rs
index b5aec790c8..b631f50d32 100644
--- a/crates/client/cache-idb/src/idb_storage.rs
+++ b/crates/client/cache-idb/src/idb_storage.rs
@@ -109,10 +109,10 @@ impl IdbStorage {
// upgraded (delete/upgrade requests block while connections are
// open); without this, `destroy` from another tab can hang forever.
db.on_version_change(|event: web_sys::Event| {
- if let Some(target) = event.target() {
- if let Ok(db) = wasm_bindgen::JsCast::dyn_into::<web_sys::IdbDatabase>(target) {
- db.close();
- }
+ if let Some(target) = event.target()
+ && let Ok(db) = wasm_bindgen::JsCast::dyn_into::<web_sys::IdbDatabase>(target)
+ {
+ db.close();
}
});
let mut storage = IdbStorage { db };
@@ -190,10 +190,7 @@ impl IdbStorage {
impl Storage for IdbStorage {
type Error = IdbStorageError;
- async fn get_batch(
- &self,
- keys: &[EntityKey<'static>],
- ) -> Result<Vec<Option<Record>>, Self::Error> {
+ async fn get_batch(&self, keys: &[EntityKey<'_>]) -> Result<Vec<Option<Record>>, Self::Error> {
let tx = self
.db
.transaction(&[RECORDS_STORE], TransactionMode::ReadOnly)?;
diff --git a/crates/client/cache-sqlite/src/lib.rs b/crates/client/cache-sqlite/src/lib.rs
index 7ed5b9a815..eca2898c30 100644
--- a/crates/client/cache-sqlite/src/lib.rs
+++ b/crates/client/cache-sqlite/src/lib.rs
@@ -224,10 +224,7 @@ fn claim_is_current(
impl Storage for SqliteStorage {
type Error = SqliteStorageError;
- async fn get_batch(
- &self,
- keys: &[EntityKey<'static>],
- ) -> Result<Vec<Option<Record>>, Self::Error> {
+ async fn get_batch(&self, keys: &[EntityKey<'_>]) -> Result<Vec<Option<Record>>, Self::Error> {
let conn = self.conn();
let mut stmt = conn.prepare_cached("SELECT value FROM records WHERE key = ?1")?;
let mut out = Vec::with_capacity(keys.len());