Skip to Content
Grist Widgets

useGristSchema

Reference for the hook that snapshots a Grist document's schema, and optionally its data.

Builds a JSON snapshot of the current Grist document — schema, optional samples, optional full data. Useful for:

  • Passing context to LLMs (system prompts, function definitions).
  • Generating type definitions for a known document.
  • Test fixtures that mirror a real document.
import { useGristSchema } from "grist-widget-sdk"

Signature

function useGristSchema(options?: UseGristSchemaOptions): UseGristSchemaResult

UseGristSchemaOptions

type UseGristSchemaOptions = {
  // What to put in the replica
  replicaRowMode?: "schema-only" | "schema+samples" | "schema+data" // default "schema+samples"
  sampleRowLimit?: number              // default 10
  source?: string                      // recorded into document.source
  excludeSystemTableData?: boolean     // default true
  tableId?: string                     // the table `table` and `tableJson` read

  // Columns left out
  excludeId?: boolean                  // default true
  excludeGristHelpers?: boolean        // default true
  excludeLoggingColumns?: boolean      // default true
  excludeManualSort?: boolean          // default false
  extraExcludeColumn?: (ctx: GristColumnExcludeContext) => boolean
}

The access level and columns are the widget's declaration (defineGristWidget()): the hook reads Grist through the provider mountGristWidget() renders, so it is called inside it.

UseGristSchemaResult

type UseGristSchemaResult = {
  document: GristReplicaDocument | null   // typed object form
  documentJson: string | null             // JSON.stringified version
  loading: boolean
  error: string | null
  reload: () => Promise<void>
}

The hook performs the read once at mount and on reload(). It does not subscribe to row changes — call reload() when you need a fresh snapshot.

Common patterns

Feed an LLM

const { documentJson } = useGristSchema({
  replicaRowMode: "schema+samples",
  sampleRowLimit: 3,
})

const systemPrompt = `
You are an assistant helping the user query their Grist document.
Schema (with sample rows):
${documentJson ?? "<loading>"}
`

Generate types

import type { GristReplicaDocument } from "grist-widget-sdk"

const { document } = useGristSchema()
// document.tables.Tasks.columns is { Title: { type: "Text" }, ... }

Reuse across tests

Save useGristSchema({ replicaRowMode: "schema+data" }).getDocumentJson() from a real document as a .replica.json fixture, then feed it to createGristEmulator({ document }) in tests.

Pairing with <GristBoundary>

The hook returns loading: true until the SDK is ready and the schema fetch resolves. Render a skeleton:

const { document, loading } = useGristSchema()
if (loading) return <Skeleton />

Pitfalls

  • Don't request replicaRowMode: "schema+data" on huge documents — you'll send the whole thing across the iframe boundary.
  • Declare requiredAccess: "full" in defineGristWidget() to read tables other than the one the widget is placed on.
  • The schema does not reflect access-controlled column hiding — it shows everything the widget can see.

On this page