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.

fix(graphql): don't throw when soup items are missing on document open (#6514)

dev-rb3a2758b3158d
diff --git a/apps/web/src/features/activity/queries/entity-query.test.ts b/apps/web/src/features/activity/queries/entity-query.test.ts
index 46e06e0639..1982009e0f 100644
--- a/apps/web/src/features/activity/queries/entity-query.test.ts
+++ b/apps/web/src/features/activity/queries/entity-query.test.ts
@@ -8,6 +8,11 @@ describe('selectEntityActivity', () => {
expect(selectEntityActivity(soupPage([]), 'doc-1')).toEqual({
kind: 'entity-missing',
});
+ expect(
+ selectEntityActivity({ user: { id: 'user-1' } } as never, 'doc-1')
+ ).toEqual({
+ kind: 'entity-missing',
+ });
});
it('returns found when the entity is present, even with no events', () => {
diff --git a/apps/web/src/features/activity/queries/select-entity-activity.ts b/apps/web/src/features/activity/queries/select-entity-activity.ts
index a13d853a69..c1891c47fb 100644
--- a/apps/web/src/features/activity/queries/select-entity-activity.ts
+++ b/apps/web/src/features/activity/queries/select-entity-activity.ts
@@ -10,7 +10,7 @@ export function selectEntityActivity(
data: EntityActivityQuery,
entityId: string
): EntityActivityResult {
- const item = data.user.soup.items.find((entry) => entry.id === entityId);
+ const item = data.user?.soup?.items?.find((entry) => entry.id === entityId);
if (!item) return { kind: 'entity-missing' };
return {
kind: 'found',
diff --git a/apps/web/src/lib/queries/preview/graphql.ts b/apps/web/src/lib/queries/preview/graphql.ts
index dffb08f906..f6665fc9f2 100644
--- a/apps/web/src/lib/queries/preview/graphql.ts
+++ b/apps/web/src/lib/queries/preview/graphql.ts
@@ -277,8 +277,12 @@ function previewFromQuery(
data: ItemPreviewQuery | ItemPreviewsQuery,
item: ItemEntity
): PreviewItem | undefined {
+ // Cache-and-network can emit a user envelope before this soup selection
+ // is present. Treat that as unloaded so REST fallback can take over.
+ const items = data.user?.soup?.items;
+ if (!items) return undefined;
const recordKey = normalizedRecordKey(item);
- const record = data.user.soup.items.find(
+ const record = items.find(
(record) => `${record.__typename}:${record.id}` === recordKey
);
return record ? graphqlRecordToPreview(record) : undefined;
diff --git a/apps/web/src/lib/queries/preview/tests/graphql.test.ts b/apps/web/src/lib/queries/preview/tests/graphql.test.ts
index 29aad90820..821b09d054 100644
--- a/apps/web/src/lib/queries/preview/tests/graphql.test.ts
+++ b/apps/web/src/lib/queries/preview/tests/graphql.test.ts
@@ -20,6 +20,8 @@ vi.mock('@app/lib/urql-solid', () => ({
createUrqlQuery: createUrqlQueryMock,
}));
+vi.mock('@service-storage/client', () => ({ DEFAULT_ITEM_TYPE: 'document' }));
+
vi.mock('@service-storage/graphql-soup', () => ({
getGraphqlSoupCacheHost: getGraphqlSoupCacheHostMock,
getGraphqlSoupClient: getGraphqlSoupClientMock,
@@ -195,6 +197,44 @@ describe('GraphQL item previews', () => {
}
});
+ it('treats a user envelope without soup items as unloaded, not a throw', async () => {
+ const [result, setResult] = createStore({
+ data: { user: { id: 'viewer' } } as ItemPreviewQuery,
+ error: null,
+ isError: false,
+ isFetched: false,
+ isFetching: true,
+ isLoading: true,
+ isEnabled: true,
+ stale: false,
+ refetch: vi.fn(async () => undefined),
+ });
+ createUrqlQueryMock.mockReturnValue(result);
+
+ const { query, dispose } = createRoot((dispose) => ({
+ query: createGraphqlItemPreviewQuery(
+ () => ({ id: 'doc-1', type: 'document' }),
+ () => true
+ ),
+ dispose,
+ }));
+
+ try {
+ await vi.advanceTimersByTimeAsync(30);
+ expect(query.data()).toBeUndefined();
+ expect(query.shouldFallback()).toBe(false);
+ setResult({
+ isFetched: true,
+ isFetching: false,
+ isLoading: false,
+ });
+ expect(query.data()).toBeUndefined();
+ expect(query.shouldFallback()).toBe(true);
+ } finally {
+ dispose();
+ }
+ });
+
it('isolates missing records and partial errors within a heterogeneous batch', async () => {
const [result] = createStore({
data: {
diff --git a/apps/web/src/lib/queries/properties/graphql/entity.test.ts b/apps/web/src/lib/queries/properties/graphql/entity.test.ts
index 7ff8e20add..435b3da90d 100644
--- a/apps/web/src/lib/queries/properties/graphql/entity.test.ts
+++ b/apps/web/src/lib/queries/properties/graphql/entity.test.ts
@@ -170,6 +170,12 @@ describe('mapGraphqlEntityProperties', () => {
it('retains the not-yet-loaded distinction', () => {
expect(mapGraphqlEntityProperties(undefined, 'entity-1')).toBeUndefined();
+ expect(
+ mapGraphqlEntityProperties(
+ { user: { id: 'user-1' } } as never,
+ 'entity-1'
+ )
+ ).toBeUndefined();
expect(mapGraphqlPropertiesMock).not.toHaveBeenCalled();
});
});
diff --git a/apps/web/src/lib/queries/properties/graphql/entity.ts b/apps/web/src/lib/queries/properties/graphql/entity.ts
index b00c172158..27a97cb890 100644
--- a/apps/web/src/lib/queries/properties/graphql/entity.ts
+++ b/apps/web/src/lib/queries/properties/graphql/entity.ts
@@ -496,8 +496,8 @@ export function mapGraphqlEntityProperties(
entityId: string
): SoupProperty[] | undefined {
if (!data) return undefined;
- const item = data.user.soup.items.find(
- (candidate) => candidate.id === entityId
- );
+ const items = data.user?.soup?.items;
+ if (!items) return undefined;
+ const item = items.find((candidate) => candidate.id === entityId);
return mapGraphqlProperties(item?.properties ?? []);
}