GraphQL Fragments: Why are they useful?

Imagine you have a page that displays several graphs. Instead of writing a separate GraphQL query for each graph, you typically write a single parent query and pass the entire result down to the child components. Each graph then picks out the data it needs.

This setup works well when everything lives on the same page. But things get tricky when you try to render a graph in isolation—like in Storybook, unit tests, or even on a different page. Suddenly, you’re forced to mock or pass down the entire parent query result, even if the graph only uses a small slice of it.

There are a couple of ways to solve this:

1. Use a Mapper Function

You can define a function that extracts only the relevant data from the parent query result and passes it into the graph.

Downsides:

2. Use GraphQL Fragments - The Better Approach

Instead, you can define a fragment that represents the exact data each graph needs. That fragment can then be included in any parent query.

Benefits:

Fragments make your components more portable, testable, and decoupled from the specifics of any one page. They’re a simple but powerful tool for scaling your GraphQL codebase cleanly.