GraphQL Fragment Generator
Paste your GraphQL SDL schema and instantly generate reusable named fragments for every type, interface, and union.
Supports nested type expansion, __typename,
custom fragment name prefixes, and union inline-fragment spreading.
Everything runs in your browser — no server, no dependencies.
Frequently Asked Questions
What is a GraphQL fragment and why should I use one?▼
A GraphQL fragment is a reusable selection set that you define once and spread across multiple queries or mutations with the `...FragmentName` syntax. Fragments keep your queries DRY: when your User type changes, you update one fragment instead of a dozen query strings. They also make it easier to co-locate data requirements with UI components — a pattern strongly recommended by Apollo, urql, and Relay.
What is the difference between named fragments and inline fragments?▼
Named fragments (`fragment UserFields on User { ... }`) are defined separately and can be reused across many operations. Inline fragments (`... on User { ... }`) are written directly inside a query and are not reusable. Inline fragments are mainly used for type-narrowing inside union and interface fields, while named fragments are the go-to choice for code reuse and component-driven data fetching.
When should I use the 'Expand nested types' option?▼
Enable it when you want fully self-contained fragments that resolve nested object fields immediately. This generates a separate fragment for each nested type (e.g. `AddressFields`) and spreads it inside the parent fragment. Disable it when you prefer shallow fragments and want to explicitly choose which nested fields to include per query — the generator will leave a comment placeholder instead so you know which fields require expansion.
Why add __typename to every fragment?▼
__typename tells Apollo Client (and other normalized caches) exactly which type each object is, which is required for correct cache normalization and for type-safe union/interface handling. It is also required if you use Apollo's `possibleTypes` or Relay's store-based fragments. Enabling this option is a good default for any Apollo or Relay project. For urql with the document cache (non-normalized), __typename is less critical.
Can I use these fragments with Apollo Client, urql, and Relay?▼
Yes. The generated fragments are standard GraphQL SDL and work with any GraphQL client. With Apollo Client you import the fragment from a `.gql` / `.graphql` file and spread it inside your query document. With urql you do the same via `gql` tagged templates. Relay requires fragments to be co-located with components and uses its own compiler, but the fragment SDL itself is identical — you just wrap it in Relay's `graphql` tag and call `useFragment`.
Related Products
on GumroadAPI Testing Cheatsheet Pack
Quick-reference cheatsheets for REST, GraphQL, and WebSocket testing. Includes Postman collections, shell scripts for smoke tests and load tests, and JWT decoder.
Buy on Gumroad →DevToolkit Starter Kit
A ready-to-deploy developer tools website with 12 built-in tools. Astro + Preact + Tailwind. Deploy to Cloudflare Pages for free. Built-in SEO, dark mode, and ad-ready layout.
Buy on Gumroad →TypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →You Might Also Need
GraphQL Query Builder
Build GraphQL queries visually from your schema SDL. Select root fields, pick sub-fields, add arguments, and generate query/mutation/subscription strings instantly. Supports nested types.
Open free tool →
OpenAPI Validator
Paste your OpenAPI 3.x spec (YAML or JSON) to validate structure, find missing required fields, detect duplicate paths, check for missing operationIds, and get errors and warnings with path references.
Open free tool →
JSON Schema Form Builder
Paste a JSON Schema and render a live HTML form with real-time validation. Split view editor. Supports type, required, enum, format, constraints.
Open free tool →
More Free Tools
Related Articles
GraphQL Federation vs REST Microservices: Which Architecture in 2026?
Compare GraphQL Federation vs REST microservices — Apollo Federation, schema stitching, performance, team scaling, migration paths, and when to choose each in 2026.
tRPC vs GraphQL: End-to-End Type Safety in 2026
A comprehensive comparison of tRPC and GraphQL in 2026 — developer experience, type safety, performance, schema definition, client libraries, real-time, and when to choose each for your fullstack TypeScript application.
tRPC vs GraphQL vs REST: Type-Safe APIs in 2026
Compare tRPC, GraphQL, and REST for building type-safe APIs in 2026. Covers DX, performance, bundle size, schema management, and when to use each approach.
Zod Schema Validation: The Complete Guide for TypeScript Developers
Master Zod schema validation in TypeScript. Learn how to define schemas, validate API responses, parse form data, infer types, compose schemas, and handle errors — with 10+ real-world code examples.
Base64 Encode/Decode Explained: When and How to Use It
Understand when to use Base64 encoding and when to avoid it. This guide explains the Base64 algorithm, practical use cases, code examples in JavaScript, Python, Go, and more.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →