tangrams
v0.15.0
Published
Code generation for the TanStack ecosystem
Maintainers
Readme
tangrams
Code generation for the TanStack ecosystem
[!WARNING] This project is in alpha and under active development. APIs may change without notice.
Overview
tangrams is a comprehensive code generation tool for the TanStack ecosystem. It takes your GraphQL or OpenAPI schema and generates fully typed, ready-to-use artifacts for TanStack libraries.
Features
- TanStack Query - Generate type-safe
queryOptions,infiniteQueryOptions, andmutationOptions - TanStack Form - Generate type-safe
formOptionswith validation schemas - TanStack DB - Generate
queryCollectionOptionswith auto-detected CRUD operations
Supported Data Sources
- GraphQL - Via introspection endpoint or local SDL files
- OpenAPI - Via spec file (local or remote URL)
Installation
# bun
bun add -D tangrams
# npm
npm install -D tangrams
# pnpm
pnpm add -D tangramsPeer Dependencies
Install dependencies based on what you're generating:
# TanStack Query (generates includes "query")
bun add @tanstack/react-query
# TanStack Form (generates includes "form")
bun add @tanstack/react-form
# TanStack DB (generates includes "db")
bun add @tanstack/react-db @tanstack/query-db-collection @tanstack/react-query
# Validation library (choose one - zod is the default)
bun add zod # Default validator
# bun add valibot # Lightweight alternative
# bun add arktype # Type-first validation
# bun add effect # Effect ecosystem
# GraphQL sources
bun add graphql-request
# OpenAPI sources
bun add @better-fetch/fetchQuick Start
Initialize configuration
bunx tangrams initConfigure your source in
tangrams.config.ts:import { defineConfig } from "tangrams"; export default defineConfig({ sources: [ { name: "api", type: "graphql", schema: { url: "http://localhost:4000/graphql" }, documents: "./src/graphql/**/*.graphql", generates: ["query", "form"], }, ], });Generate code
bunx tangrams generateUse the generated code
import { useQuery, useMutation } from "@tanstack/react-query" import { getUserQueryOptions, createUserMutationOptions } from "./tangrams/api/query/operations" function UserProfile({ userId }: { userId: string }) { const { data } = useQuery(getUserQueryOptions({ id: userId })) const { mutate } = useMutation(createUserMutationOptions()) return <div>{data?.user?.name}</div> }
Vite Plugin
For Vite projects, tangrams provides a plugin that integrates code generation into your dev server and build process. The plugin reads configuration from your tangrams.config.ts file and provides automatic file watching and cleanup.
// vite.config.ts
import { defineConfig } from "vite"
import { tangrams } from "tangrams/vite"
export default defineConfig({
plugins: [
tangrams(), // Loads tangrams.config.ts automatically
],
})Plugin Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| configFile | string | - | Path to config file (auto-discovers tangrams.config.ts by default) |
| force | boolean | false | Force regenerate all files |
| watch | boolean | true | Watch for changes in dev mode |
| clean | boolean | true | Remove stale directories |
Custom Config Path
tangrams({ configFile: "./config/tangrams.config.ts" })Documentation
For comprehensive documentation, configuration reference, and usage examples, visit the Tangrams Documentation.
- Getting Started - Installation, configuration reference, and all available options
- TanStack Query - Generate
queryOptions,infiniteQueryOptionswhen applicable, andmutationOptions - TanStack Form - Generate
formOptionswith schema validation - TanStack DB - Generate collections with local-first data sync
CLI Reference
tangrams init
Initialize a new tangrams.config.ts file.
tangrams init [options]
Options:
-f, --force Overwrite existing config filetangrams generate
Generate TypeScript code from your configured sources.
tangrams generate [options]
Options:
-c, --config <path> Path to config file
-f, --force Force regeneration of all files including client
-w, --watch Watch for file changes and regenerate
--clean Remove stale source directories from previous generations
-y, --yes Skip confirmation prompts (use with --clean)
--env-file <path> Path to env file (can be specified multiple times)
--no-dotenv Disable automatic .env file loadingCleanup Mode
When using --clean, tangrams will detect and remove stale source directories from previous generations. This is useful when you rename or remove sources from your configuration.
# Remove stale artifacts (prompts for confirmation)
tangrams generate --clean
# Remove stale artifacts without prompting
tangrams generate --clean --yesIf tangrams detects that a source was renamed (same schema/spec, different name), it will automatically copy the client.ts file to the new source directory before removing the old one. This preserves any customizations you've made to the client.
Watch Mode
When using --watch, tangrams will:
- Watch your config file, GraphQL documents, and OpenAPI specs for changes
- Cache schemas between file changes for faster rebuilds
- Continue watching even if generation fails
Interactive commands in watch mode:
- Press
rto force a full refresh (re-fetches all schemas) - Press
qto quit
Roadmap
- Multi-Framework Code Generation
- TanStack DB - Electic Collections
- TanStack DB - LocalStorage Collections
- TBD
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © hobbescodes
