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/editorialAssemblyListItem.fragment.graphqlsrc/graphql/fragments/editorialAssemblyDetail.fragment.graphqlsrc/graphql/fragments/poiListItem.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 EditorialAssemblyListItem on EditorialAssembly { }fragment EditorialAssemblyDetail on EditorialAssemblyExtended { }fragment PoiListItem on Poi { }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 EditorialAssemblyListItem on EditorialAssembly { esid title slug publishedDate excerpt readTime
featuredImage { ...ImageProperties }
authors { firstName lastName slug image { ...ImageProperties } }}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”List Page (Rakiura)
Section titled “List Page (Rakiura)”#import "#graphql/fragments/editorialAssemblyListItem.fragment.graphql"
query ArticlesPage { editorialAssemblies(page: { size: 20 }) { items { ...EditorialAssemblyListItem } }}Detail Page (Rakiura)
Section titled “Detail Page (Rakiura)”#import "#graphql/fragments/editorialAssemblyDetail.fragment.graphql"
query ArticlePage($slug: String!) { editorialAssembly(slug: $slug) { ...EditorialAssemblyDetail }}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 }}Editorial Assembly Fragments (Rakiura)
Section titled “Editorial Assembly Fragments (Rakiura)”#import "#graphql/fragments/imageProps.fragment.graphql"
fragment EditorialAssemblyListItem on EditorialAssembly { title slug excerpt date featuredImage { ...ImageProperties } authors { firstName lastName slug } tags { slug title }}#import "#graphql/fragments/imageProps.fragment.graphql"
fragment EditorialAssemblyDetail on EditorialAssemblyExtended { title slug excerpt date featuredImage { ...ImageProperties } authors { firstName lastName slug bio image { ...ImageProperties } } content readTime tags { slug title } meta { type { slug title } }}Card/List Item Fragments
Section titled “Card/List Item Fragments”#import "#graphql/fragments/imageProps.fragment.graphql"
fragment PlaceCard on Place { name slug description location { city country } image { ...ImageProperties } rating}Type Generation
Section titled “Type Generation”Fragments generate TypeScript types:
fragment EditorialAssemblyListItem on EditorialAssembly { title slug excerpt date}Generates:
// Generated typeexport type EditorialAssemblyListItemFragment = { __typename?: 'EditorialAssembly'; title: string; slug: string; excerpt: string | null; date: string;};
// Usage in codeimport type { EditorialAssemblyListItemFragment } from "#graphql/generated/rakiura/schema";
// Adapter pattern: map Rakiura types to existing UI component interfacesfunction mapEditorialAssemblyToArticle( assembly: EditorialAssemblyListItemFragment): Article { return { title: assembly.title, slug: assembly.slug, excerpt: assembly.excerpt ?? "", date: assembly.publishedDate, };}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