Skip to main content

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.

✓ Pure client-side SDL parsing ✓ Object types, interfaces & unions ✓ Works with Apollo, urql & Relay

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`.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

GraphQL Query Builder OpenAPI Validator JSON Schema Form Builder GraphQL Schema Validator v2 GraphQL Query Formatter JSON Schema to Zod Converter API Response Mock Generator API Response Time Tester JSON Schema Generator JSON-LD Schema Generator Zod Schema Generator OpenAPI Schema Validator Prisma Schema Visualizer OpenAPI Mock Generator Prisma Schema Builder LLM JSON Schema Builder OpenAI Function Calling Generator API Test Case Generator Database Schema Diff API Request Builder API Mock Server Config Generator Drizzle Schema Generator GraphQL Client Configurator API Rate Limit Calculator API Response Formatter & Analyzer HTTP API Tester SQL ERD Diagram Generator LLM Cost Calculator AI Prompt Cost Calculator REST API Request Builder Database Schema Visualizer Mock Service Worker (MSW) API Mocking Guide LLM Prompt Cost Calculator SQL to TypeScript Types API Response Mocker AI Rate Limit Calculator PlanetScale Branch Manager API Key Generator AI Doc Generator — Pro Tool Code Tools HTTP Methods Reference HTTP Code Snippet Generator HTTP Status Code Reference HTTP Status Code Reference JSON Diff Viewer JSON Diff Rate Limiter Tester API Changelog Generator HTTP Mock Response Builder Rate Limit Calculator CORS Policy Builder Grafana Dashboard JSON Validator HTTP Status Code Lookup HTTP Status Code Lookup Vue Component Generator HTTP Status Code Reference LLM Context Window Calculator LLM Token Counter Prompt Token Estimator YAML Validator Pro Playwright E2E Testing Guide Pulumi Modern IaC Guide MCP Endpoint Tester HTML5 Canvas API Playground Token Context Calculator Token Counter & Cost Estimator AI API Rate Limit Calculator

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →