Skip to content

fragment.graphql

GraphQL fragment files contain reusable field selections that can be shared across multiple queries.

src/graphql/fragments/{name}.fragment.graphql
src/graphql/fragments/{name}.fragment.cms.graphql

Shared fragments are located in the central src/graphql/fragments/ directory.

Examples:

src/graphql/fragments/imageProps.fragment.graphql
src/graphql/fragments/editorialAssemblyListItem.fragment.graphql
src/graphql/fragments/editorialAssemblyDetail.fragment.graphql
src/graphql/fragments/poiListItem.fragment.graphql

In rare cases, component-specific fragments can be colocated:

src/components/{feature}/graphql/{name}.fragment.graphql
fragment {EntityName}{Purpose} on {Type} {
# fields
}

Examples:

fragment ImageProperties on Image { }
fragment EditorialAssemblyListItem on EditorialAssembly { }
fragment EditorialAssemblyDetail on EditorialAssemblyExtended { }
fragment PoiListItem on Poi { }
src/graphql/fragments/imageProps.fragment.graphql
fragment ImageProperties on Image {
url
width
height
description # Used as alt text
title
}
src/graphql/fragments/editorialAssemblyListItem.fragment.graphql
#import "#graphql/fragments/imageProps.fragment.graphql"
fragment EditorialAssemblyListItem on EditorialAssembly {
esid
title
slug
publishedDate
excerpt
readTime
featuredImage {
...ImageProperties
}
authors {
firstName
lastName
slug
image {
...ImageProperties
}
}
}
src/graphql/fragments/contentBlock.fragment.graphql
fragment ContentBlock on ContentBlockUnion {
__typename
... on ContentText {
text
format
}
... on ContentImage {
image {
...ImageProperties
}
caption
}
... on ContentVideo {
videoUrl
thumbnail {
...ImageProperties
}
}
}

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
}
}
src/pages/articles/_graphql/page.query.api.graphql
#import "#graphql/fragments/editorialAssemblyListItem.fragment.graphql"
query ArticlesPage {
editorialAssemblies(page: { size: 20 }) {
items {
...EditorialAssemblyListItem
}
}
}
src/pages/articles/[slug]/_graphql/page.query.api.graphql
#import "#graphql/fragments/editorialAssemblyDetail.fragment.graphql"
query ArticlePage($slug: String!) {
editorialAssembly(slug: $slug) {
...EditorialAssemblyDetail
}
}
fragment ImageProperties on Image {
url
width
height
description # alt text
title
contentType
}
fragment CTAProperties on CTA {
text
url
variant # primary, secondary, text
external
openInNewTab
}
#import "#graphql/fragments/imageProps.fragment.graphql"
fragment AuthorBio on Author {
firstName
lastName
slug
bio
image {
...ImageProperties
}
socialMedia {
twitter
instagram
}
}
src/graphql/fragments/editorialAssemblyListItem.fragment.graphql
#import "#graphql/fragments/imageProps.fragment.graphql"
fragment EditorialAssemblyListItem on EditorialAssembly {
title
slug
excerpt
date
featuredImage {
...ImageProperties
}
authors {
firstName
lastName
slug
}
tags {
slug
title
}
}
src/graphql/fragments/editorialAssemblyDetail.fragment.graphql
#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
}
}
}
#import "#graphql/fragments/imageProps.fragment.graphql"
fragment PlaceCard on Place {
name
slug
description
location {
city
country
}
image {
...ImageProperties
}
rating
}

Fragments generate TypeScript types:

fragment EditorialAssemblyListItem on EditorialAssembly {
title
slug
excerpt
date
}

Generates:

// Generated type
export type EditorialAssemblyListItemFragment = {
__typename?: 'EditorialAssembly';
title: string;
slug: string;
excerpt: string | null;
date: string;
};
// Usage in code
import type { EditorialAssemblyListItemFragment } from "#graphql/generated/rakiura/schema";
// Adapter pattern: map Rakiura types to existing UI component interfaces
function mapEditorialAssemblyToArticle(
assembly: EditorialAssemblyListItemFragment
): Article {
return {
title: assembly.title,
slug: assembly.slug,
excerpt: assembly.excerpt ?? "",
date: assembly.publishedDate,
};
}