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/union-field-variant-replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fetchium': patch
---

Fix the runtime merge so a discriminated union field is replaced when its variant changes. Previously the merge recursed into the union's typename-keyed shape, which matches no real field, so a changed variant left the old variant's fields in place and the new variant's fields never landed. Union fields are now replaced wholesale.
60 changes: 60 additions & 0 deletions packages/fetchium/src/__tests__/branch-fixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,66 @@ describe('Branch Fixes', () => {
});
});

it('should replace a discriminated union field when its variant changes', async () => {
const { client, mockFetch } = getClient();

const QueuedState = t.object({
__typename: t.typename('QueuedState'),
queuedAt: t.string,
});
const RunningState = t.object({
__typename: t.typename('RunningState'),
progress: t.number,
});

class Job extends Entity {
__typename = t.typename('Job');
id = t.id;
state = t.union(QueuedState, RunningState);
}

mockFetch.get('/jobs/[id]', {
job: {
__typename: 'Job',
id: '1',
state: { __typename: 'QueuedState', queuedAt: '2026-06-10T00:00:00Z' },
},
});

await testWithClient(client, async () => {
class GetJob extends RESTQuery {
params = { id: t.id };
path = `/jobs/${this.params.id}`;
result = { job: t.entity(Job) };
}

const relay = fetchQuery(GetJob, { id: '1' });
const result = await relay;

expect((result.job.state as any).__typename).toBe('QueuedState');
expect((result.job.state as any).queuedAt).toBe('2026-06-10T00:00:00Z');

// Variant flips: the job starts running. The new state has a different
// __typename and a disjoint field set.
mockFetch.get('/jobs/[id]', {
job: {
__typename: 'Job',
id: '1',
state: { __typename: 'RunningState', progress: 42 },
},
});

const result2 = await relay.value!.__refetch();

// The union field must be replaced wholesale, not merged: the new
// variant's discriminator and fields are present, and no field from
// the old variant survives.
expect((result2.job.state as any).__typename).toBe('RunningState');
expect((result2.job.state as any).progress).toBe(42);
expect('queuedAt' in (result2.job.state as any)).toBe(false);
});
});

it('should handle nullable nested object with null value', async () => {
const { client, mockFetch } = getClient();
class Profile extends Entity {
Expand Down
8 changes: 7 additions & 1 deletion packages/fetchium/src/applyEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { QueryClient } from './QueryClient.js';
import type { EntityInstance } from './EntityInstance.js';
import type { ParseContext, ParsedEntity } from './parseEntities.js';
import { FormattedValue, ValidatorDef } from './typeDefs.js';
import { Mask } from './types.js';
import { createLiveCollection, LiveCollectionBinding } from './LiveCollection.js';
import { PROXY_ID } from './proxyId.js';

Expand Down Expand Up @@ -206,7 +207,12 @@ function mergeFields(
} else {
const newVal = data[fieldKey];
const oldVal = existingData[fieldKey];
if (isPlainObject(newVal) && isPlainObject(oldVal)) {
// Replace a union wholesale instead of merging by field, otherwise a
// changed variant keeps the old variant's fields. Unlike entity unions,
// there is no partial-update path to preserve here: a partial variant
// payload fails validation, so every update carries the full variant.
const isUnion = propShape instanceof ValidatorDef && (propShape.mask & Mask.UNION) !== 0;
if (!isUnion && isPlainObject(newVal) && isPlainObject(oldVal)) {
const nestedShape =
propShape instanceof ValidatorDef && propShape.shape !== undefined
? (propShape.shape as Record<string, unknown>)
Expand Down
Loading