Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unknown-union-variant-fail-soft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fetchium": patch
---

Handle unknown union variants instead of silently dropping the whole entity update. An optional union field now degrades to `undefined`; a non-optional one surfaces an `UnknownUnionVariantError` (visible, not a swallowed warn) and applies no partial state. Initial load and live updates behave the same.
9 changes: 8 additions & 1 deletion packages/fetchium/src/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { GcManager, NoOpGcManager, GcKeyType } from './GcManager.js';
import { DEFAULT_GC_TIME } from './stores/shared.js';
import { Query, QueryDefinition } from './query.js';
import { ParseContext, parseEntities, parseEntity, type ParseResult } from './parseEntities.js';
import { UnknownUnionVariantError } from './errors.js';
import { applyEntityRefs, type ApplyResult } from './applyEntities.js';
import { ValidatorDef } from './typeDefs.js';
import { ConstraintMatcher, EVENT_SOURCE_FIELD } from './ConstraintMatcher.js';
Expand Down Expand Up @@ -391,7 +392,13 @@ export class QueryClient {
const parsedData = parseEntity(data, mergedDef as unknown as EntityDef, parseCtx);
applyEntityRefs(parseCtx, parsedData, true);
} catch (e) {
this.context.log?.warn?.('Failed to apply mutation event', e);
// Unknown required-union variant: surface as an error (not a silent warn)
// so the dropped update is visible. Optional unions degrade during parse.
if (e instanceof UnknownUnionVariantError) {
this.context.log?.error?.('Mutation event dropped: unknown union variant; update not applied', e);
} else {
this.context.log?.warn?.('Failed to apply mutation event', e);
}
if (existing === undefined) {
const created = this.entityMap.getEntity(key);
if (created !== undefined) created.evict();
Expand Down
228 changes: 228 additions & 0 deletions packages/fetchium/src/__tests__/unknown-union-variant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import { describe, it, expect, afterEach } from 'vitest';
import { SyncQueryStore, MemoryPersistentStore } from '../stores/sync.js';
import { QueryClient } from '../QueryClient.js';
import { t } from '../typeDefs.js';
import { Entity } from '../proxy.js';
import { fetchQuery } from '../query.js';
import { testWithClient, sleep } from './utils.js';
import { TopicQuery } from '../topic/TopicQuery.js';
import { TopicQueryAdapter } from '../topic/TopicQueryAdapter.js';
import type { MutationEvent } from '../types.js';

/**
* Unknown union variant: optional field degrades to undefined (apply the rest),
* non-optional surfaces an error without applying partial state. Same on initial
* load and live updates.
*/

class Stream {
private sub: { onData: (d: unknown) => void; onEvent: (e: MutationEvent) => void } | undefined;
private buffered: unknown[] = [];
subscribe(cb: { onData: (d: unknown) => void; onEvent: (e: MutationEvent) => void }) {
this.sub = cb;
const pending = this.buffered;
this.buffered = [];
for (const d of pending) cb.onData(d);
return () => {
this.sub = undefined;
};
}
pushData(data: unknown) {
if (this.sub) this.sub.onData(data);
else this.buffered.push(data);
}
pushEvent(event: MutationEvent) {
this.sub?.onEvent(event);
}
}

class MockAdapter extends TopicQueryAdapter {
stream: Stream;
private unsubs = new Map<string, () => void>();
constructor(stream: Stream) {
super();
this.stream = stream;
}
subscribe(topic: string): void {
this.unsubs.set(
topic,
this.stream.subscribe({
onData: data => this.fulfillTopic(topic, data),
onEvent: event => this.sendMutationEvent(event),
}),
);
}
unsubscribe(topic: string): void {
this.unsubs.get(topic)?.();
this.unsubs.delete(topic);
this.clearTopic(topic);
}
}

const StatusActive = t.object({ __typename: t.typename('StatusActive'), label: t.string });
const StatusClosed = t.object({ __typename: t.typename('StatusClosed'), closedAt: t.string });

class CardReq extends Entity {
__typename = t.typename('CardReq');
id = t.id;
disabled = t.boolean;
status = t.union(StatusActive, StatusClosed); // non-optional union
}

class CardOpt extends Entity {
__typename = t.typename('CardOpt');
id = t.id;
disabled = t.boolean;
status = t.optional(t.union(StatusActive, StatusClosed)); // optional union
}

class GetCardReq extends TopicQuery {
static override adapter = MockAdapter;
topic = 'card:req';
result = t.entity(CardReq);
}

class GetCardOpt extends TopicQuery {
static override adapter = MockAdapter;
topic = 'card:opt';
result = t.entity(CardOpt);
}

class GetCardReqLoad extends TopicQuery {
static override adapter = MockAdapter;
topic = 'card:reqload';
result = t.entity(CardReq);
// retry off so a parse-failed load rejects immediately, not after the backoff.
override getConfig() {
return { ...super.getConfig(), retry: false as const };
}
}

function makeClient(stream: Stream) {
const logs = { error: [] as unknown[][], warn: [] as unknown[][] };
const client = new QueryClient({
store: new SyncQueryStore(new MemoryPersistentStore()),
adapters: [new MockAdapter(stream)],
log: {
error: (m: string, e?: unknown) => logs.error.push([m, e]),
warn: (m: string, e?: unknown) => logs.warn.push([m, e]),
},
} as any);
return { client, logs };
}

async function pushEventOutsideReactiveContext(stream: Stream, event: MutationEvent): Promise<void> {
await new Promise<void>(resolve => {
setTimeout(() => {
stream.pushEvent(event);
resolve();
}, 0);
});
await sleep(10);
}

const unknownVariantUpdate = (typename: string) => ({
type: 'update' as const,
typename,
id: '1',
data: { __typename: typename, id: '1', disabled: true, status: { __typename: 'StatusVoided', voidedAt: 'now' } },
});

describe('unknown union variant in a mutation event', () => {
let client: QueryClient;
afterEach(() => client?.destroy());

it('non-optional union: does not apply partial state and surfaces an error', async () => {
const stream = new Stream();
const made = makeClient(stream);
client = made.client;

await testWithClient(client, async () => {
const relay = fetchQuery(GetCardReq);
stream.pushData({
__typename: 'CardReq',
id: '1',
disabled: false,
status: { __typename: 'StatusActive', label: 'Open' },
});
await relay;
expect((relay.value as any).disabled).toBe(false);

await pushEventOutsideReactiveContext(stream, unknownVariantUpdate('CardReq'));

const v: any = relay.value;
// No partial state: sibling change not applied, but the drop is surfaced.
expect(v.disabled).toBe(false);
expect(v.status.__typename).toBe('StatusActive');
expect(made.logs.error.length).toBe(1);
expect(String(made.logs.error[0][0])).toMatch(/unknown union variant/i);
});
});

it('optional union: degrades the field to undefined and applies the rest', async () => {
const stream = new Stream();
const made = makeClient(stream);
client = made.client;

await testWithClient(client, async () => {
const relay = fetchQuery(GetCardOpt);
stream.pushData({
__typename: 'CardOpt',
id: '1',
disabled: false,
status: { __typename: 'StatusActive', label: 'Open' },
});
await relay;
expect((relay.value as any).status.__typename).toBe('StatusActive');

await pushEventOutsideReactiveContext(stream, unknownVariantUpdate('CardOpt'));

const v: any = relay.value;
// Sibling applies; the unknown-variant union degrades to undefined, no error.
expect(v.disabled).toBe(true);
expect(v.status).toBeUndefined();
expect(made.logs.error.length).toBe(0);
});
});

it('non-optional union on initial load: rejects the query, surfacing the error (no degrade, no partial state)', async () => {
const stream = new Stream();
const made = makeClient(stream);
client = made.client;

await testWithClient(client, async () => {
const relay = fetchQuery(GetCardReqLoad);
stream.pushData({
__typename: 'CardReq',
id: '1',
disabled: true,
status: { __typename: 'StatusVoided', voidedAt: 'now' },
});
await expect(relay).rejects.toThrow(/Unknown typename 'StatusVoided'/);
expect(relay.isRejected).toBe(true);
});
});

it('optional union on initial load: unknown variant degrades to undefined, rest applies (parity with updates)', async () => {
const stream = new Stream();
const made = makeClient(stream);
client = made.client;

await testWithClient(client, async () => {
const relay = fetchQuery(GetCardOpt);
// First (and only) snapshot already carries an unknown variant.
stream.pushData({
__typename: 'CardOpt',
id: '1',
disabled: true,
status: { __typename: 'StatusVoided', voidedAt: 'now' },
});
await relay;

const v: any = relay.value;
expect(v.disabled).toBe(true);
expect(v.status).toBeUndefined();
expect(made.logs.error.length).toBe(0);
});
});
});
10 changes: 10 additions & 0 deletions packages/fetchium/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ export function typeToString(type: InternalObjectFieldTypeDef): string {
return 'unknown';
}

/** Union payload's `__typename` matched no known variant. Typed so callers can degrade optional fields to `undefined` and surface required ones. */
export class UnknownUnionVariantError extends Error {
readonly typename: string;
constructor(typename: string, path?: string) {
super(`Unknown typename '${typename}' in union${path ? ` at ${path}` : ''}`);
this.name = 'UnknownUnionVariantError';
this.typename = typename;
}
}

export function typeError(path: string, expectedType: InternalObjectFieldTypeDef, value: unknown): Error {
if (IS_DEV) {
return new TypeError(
Expand Down
19 changes: 16 additions & 3 deletions packages/fetchium/src/parseEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { hashValue } from 'signalium/utils';
import type { QueryClient, PreloadedEntityMap } from './QueryClient.js';
import { CaseInsensitiveSet, FormattedValue, FORMAT_MASK_SHIFT, ValidatorDef } from './typeDefs.js';
import { typeError } from './errors.js';
import { typeError, UnknownUnionVariantError } from './errors.js';
import {
ARRAY_KEY,
ArrayDef,
Expand Down Expand Up @@ -257,7 +257,20 @@ function parseData(value: unknown, typeDef: TypeDef | ComplexTypeDef, ctx: Parse
}

if ((propMask & Mask.UNION) !== 0) {
return parseUnionData(valueType, value as Record<string, unknown> | unknown[], def as UnionDef, ctx, path);
try {
return parseUnionData(valueType, value as Record<string, unknown> | unknown[], def as UnionDef, ctx, path);
} catch (e) {
// Unknown variant: degrade an optional field to undefined; rethrow a
// required one so the caller surfaces it instead of dropping silently.
if (e instanceof UnknownUnionVariantError && (propMask & Mask.UNDEFINED) !== 0) {
ctx.warn('Unknown union variant for optional field, defaulting to undefined', {
typename: e.typename,
path,
});
return undefined;
}
throw e;
}
}

if (valueType === Mask.ARRAY) {
Expand Down Expand Up @@ -313,7 +326,7 @@ function parseUnionData(
const matchingDef = unionDef.shape![typename];

if (matchingDef === undefined || typeof matchingDef === 'number') {
throw new Error(`Unknown typename '${typename}' in union`);
throw new UnknownUnionVariantError(typename, path);
}

if (matchingDef.mask & Mask.ENTITY && ctx.queryClient !== undefined) {
Expand Down
Loading