Share GraphQL Fields Across Queries with Named Fragments
Use named GraphQL fragments to centralize repeated field sets, reduce copy-paste errors, and verify schema changes propagate across operations.
01 May 2026, 22:30 UTC

Problem: the same user fields appear in every operation
A frontend team queries the logged-in user in a dashboard, includes the same fields in a settings mutation response, and asks for them again in a notification feed. Each operation lists id, displayName, avatarUrl, and email individually. When the schema renames displayName to name, a developer updates the dashboard but misses the mutation. The app renders a blank name in settings, and the bug only appears after deployment.
Thesis: named GraphQL fragments centralize field selections
A fragment is a named, reusable set of fields tied to a specific type. Defining UserSummary once means any query or mutation can spread it with the ... operator. Changing the fragment updates every operation that references it, which reduces copy-paste errors and makes schema evolution safer.
Define a fragment for the User type
fragment UserSummary on User {
id
displayName
avatarUrl
email
}
Spread the fragment in a query
query Dashboard {
viewer {
...UserSummary
role
}
}
Reuse the fragment in a mutation response
mutation UpdateEmail($email: String!) {
updateUser(email: $email) {
...UserSummary
}
}
Both operations now receive exactly the same user field set. If the team adds phone to UserSummary, both the dashboard and the mutation response start returning phone without further edits.
Conditional selection with @include and @skip
Fragments work with directives, but the server must honor the client context. This example requests the email field only when includeEmail is true:
query Viewer($includeEmail: Boolean!) {
viewer {
...UserSummary @include(if: $includeEmail)
role
}
}
If the server ignores the variable, the client may receive fields it did not expect or omit fields it requires. Test directive behavior against your specific GraphQL server before relying on it in production.
Trade-off: fragment size and discoverability
Large, vaguely named fragments make queries harder to read and can cause over-fetching. Keep fragments small, name them after the data they describe (for example, UserSummary rather than Stuff), and avoid nesting fragments inside fragments unless the composition genuinely simplifies the operation. A fragment that grows beyond a handful of fields is a signal to split it or reconsider the query design.
Verify that a fragment change propagates
- Run the Dashboard query against your endpoint using GraphiQL, Insomnia, or curl.
- Confirm the response includes id, displayName, avatarUrl, and email under viewer.
- Add phone to the UserSummary fragment definition.
- Run the Dashboard query and the UpdateEmail mutation again.
- Check that phone appears in both responses.
Example curl request (run in a terminal with network access to the GraphQL server):
curl -X POST "$GRAPHQL_ENDPOINT" \
-H "Authorization: Bearer $AUTH_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query":"query Dashboard { viewer { ...UserSummary role } } fragment UserSummary on User { id displayName avatarUrl email }"}'
Replace $GRAPHQL_ENDPOINT and $AUTH_TOKEN with your own values. The request is read-only for the query shown; if you test the mutation, it will change data, so use a non-production environment. Expected check: the JSON response contains a viewer object with the fragment fields and no validation errors.
Actionable closing
Start by extracting one repeated field set into a fragment. Run the operation before and after the change to confirm the response shape is identical. Then add a field to the fragment and verify it appears everywhere the fragment is spread. Fragments are supported by any spec-compliant GraphQL server, so this pattern works across clients and languages without new dependencies.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.