fragment.graphql
GraphQL fragment files contain reusable field selections that can be shared across multiple queries.
File Location
Section titled “File Location”Shared Fragments
Section titled “Shared Fragments”src/graphql/fragments/{name}.fragment.graphqlsrc/graphql/fragments/{name}.fragment.cms.graphqlShared fragments are located in the central src/graphql/fragments/ directory.
Examples:
src/graphql/fragments/imageProps.fragment.graphqlsrc/graphql/fragments/articleListItem.fragment.cms.graphqlsrc/graphql/fragments/authorBio.fragment.graphqlsrc/graphql/fragments/placeCard.fragment.graphqlComponent-Specific Fragments
Section titled “Component-Specific Fragments”In rare cases, component-specific fragments can be colocated:
src/components/{feature}/graphql/{name}.fragment.graphqlFragment Naming Pattern
Section titled “Fragment Naming Pattern”fragment {EntityName}{Purpose} on {Type} { # fields}Examples:
fragment ImageProperties on Image { }fragment ArticleListItem on Article { }fragment PlaceCard on Place { }fragment AuthorBio on Author { }Typical Structure
Section titled “Typical Structure”Simple Fragment
Section titled “Simple Fragment”fragment ImageProperties on Image { url width height description # Used as alt text title}Complex Fragment with Nested Fragments
Section titled “Complex Fragment with Nested Fragments”#import "#graphql/fragments/imageProps.fragment.graphql"
fragment ArticleListItem on Article { title slug excerpt publishDate
# Nested fragment featuredImage { ...ImageProperties }
# Author info author { firstName lastName slug image { ...ImageProperties } }
# Metadata metadata { category tags readTime }}Fragment with Inline Fragments
Section titled “Fragment with Inline Fragments”fragment ContentBlock on ContentBlockUnion { __typename
... on ContentText { text format }
... on ContentImage { image { ...ImageProperties } caption }
... on ContentVideo { videoUrl thumbnail { ...ImageProperties } }}Fragment Imports
Section titled “Fragment Imports”Use path aliases for fragment imports:
#import "#graphql/fragments/imageProps.fragment.graphql"#import "#graphql/fragments/authorBio.fragment.graphql"
fragment ArticleDetail on Article { title featuredImage { ...ImageProperties } author { ...AuthorBio }}Fragment Usage in Queries
Section titled “Fragment Usage in Queries”#import "#graphql/fragments/articleListItem.fragment.graphql"
query ArticlesPage { articleCollection(limit: 20) { items { ...ArticleListItem } }
featuredArticles: articleCollection( where: { featured: true } limit: 3 ) { items { ...ArticleListItem } }}Common Fragment Patterns
Section titled “Common Fragment Patterns”Image Fragment
Section titled “Image Fragment”fragment ImageProperties on Image { url width height description # alt text title contentType}Link/CTA Fragment
Section titled “Link/CTA Fragment”fragment CTAProperties on CTA { text url variant # primary, secondary, text external openInNewTab}Author Fragment
Section titled “Author Fragment”#import "#graphql/fragments/imageProps.fragment.graphql"
fragment AuthorBio on Author { firstName lastName slug bio image { ...ImageProperties } socialMedia { twitter instagram }}Card/List Item Fragments
Section titled “Card/List Item Fragments”#import "#graphql/fragments/imageProps.fragment.graphql"
fragment ArticleCard on Article { title slug excerpt publishDate category image { ...ImageProperties } author { firstName lastName }}
fragment PlaceCard on Place { name slug description location { city country } image { ...ImageProperties } rating}Type Generation
Section titled “Type Generation”Fragments generate TypeScript types:
fragment ArticleListItem on Article { title slug}Generates:
// Generated typeexport type ArticleListItemFragment = { __typename?: 'Article'; title: string; slug: string;};
// Usage in codeimport type { ArticleListItemFragment } from "#graphql/generated/contentful/schema";
function renderArticle(article: ArticleListItemFragment) { return `<h2>${article.title}</h2>`;}Related
Section titled “Related”- GraphQL Naming - Fragment naming conventions
- page.query.graphql - Using fragments in queries
- File Organization - Where to place fragments
- GraphQL Query Patterns - Fragment composition and best practices