On this page
Fragments
Reuse a group of selected fields across operations.
Keep a fragment beside an operation
Use an in-file fragment when one operation owns the selection.
lib/graphql/viewer.graphql
query Viewer {
viewer {
...UserIdentity
}
}
fragment UserIdentity on User {
id
login
}
The generated response model includes the fragment fields and fragment output follows the <FragmentName>Mixin pattern, such as UserIdentityMixin.
Share fragments through globs
Global fragments are available to every mapping. Mapping-level fragments are available only to their schema/operation pair.
build.yaml
options:
fragments_glob: lib/graphql/common/*.fragment.graphql
schema_mapping:
- schema: schema.graphql
queries_glob: lib/graphql/account/viewer.graphql
fragments_glob: lib/graphql/account/fragments/*.graphql
Keep schema-specific fragments inside their mapping.
Documents include transitive dependencies
If an operation references UserIdentity, and that fragment references AvatarFields, both definitions enter the operation DocumentNode. Unrelated configured fragments do not.
lib/graphql/common/user.fragment.graphql
fragment UserIdentity on User {
id
login
...AvatarFields
}
fragment AvatarFields on User {
avatarUrl
}
Resolve missing fragments
A spread must resolve from one of three places:
- the operation file;
- the mapping’s
fragments_glob; - the top-level
fragments_glob.
The fragment file must also be included by the build target’s sources. Otherwise generation raises MissingFragmentException with a message beginning Can't find the "FragmentName" in "ClassName".
Fragment-only files belong in fragments_glob, not queries_glob; a matched query file with no operation raises MissingOperationException.
Continue with GraphQL features for abstract types and scalars, or consult troubleshooting for exact failure recovery.