Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.
Draft
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
26 changes: 20 additions & 6 deletions packages/fabrix/src/renderers/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FabrixContextType } from "@context";
import { Value } from "@fetcher";
import { TableComponentEntry } from "@registry";
import { createElement } from "react";
import { z } from "zod";
import { RootField, SubFields, ViewFields, getSubFields } from "./fields";
import { Loader } from "./shared";
import { buildTypenameExtractor } from "./typename";
Expand All @@ -24,18 +25,31 @@ export const getTableMode = (fields: ViewFields) => {
return keyField.field.getName() == "collection" ? "standard" : "relay";
};

const collectionValidator = z.object({
collection: z.array(z.record(z.unknown())),
});
const relayValidator = z.object({
edges: z.array(
z.object({
node: z.record(z.unknown()),
}),
),
});

export const getTableValues = (
rootValue: Value,
mode: TableMode,
): Record<string, unknown>[] => {
const value = rootValue as Record<string, unknown>;
switch (mode) {
case "standard":
return value.collection as Record<string, unknown>[];
case "relay":
return (value.edges as { node: Record<string, unknown> }[]).map(
({ node }) => node,
);
case "standard": {
const r = collectionValidator.safeParse(value);
return r.success ? r.data.collection : [];
}
case "relay": {
const r = relayValidator.safeParse(value);
return r.success ? r.data.edges.map((e) => e.node) : [];
}
}
};

Expand Down