transform.ts
The transform.ts file contains functions that transform raw GraphQL data into UI-ready formats.
File Location
Section titled “File Location”Page-Level Transform Files
Section titled “Page-Level Transform Files”src/pages/{routeName}/_graphql/transform.tsExamples:
src/pages/articles/_graphql/transform.tssrc/pages/places/_graphql/transform.tssrc/pages/articles/[slug]/_graphql/transform.tsComponent-Level Transform Files
Section titled “Component-Level Transform Files”src/components/{feature}/graphql/transform.tssrc/pages/{route}/_components/{feature}/transform.tsExamples:
src/components/relatedArticles/graphql/transform.tssrc/pages/articles/_components/sidebar/transform.tsFunction Naming Pattern
Section titled “Function Naming Pattern”transform{Entity}Data()transform{Source}To{Target}()Examples:
export function transformArticleData(raw: ArticleQuery): TransformedArticle {}export function transformHeroData(raw: HeroFromQuery): HeroData {}export function transformContentBlocks(raw: ComponentsCollection): ContentBlock[] {}Typical Structure
Section titled “Typical Structure”// Importsimport type { ArticlePageQuery } from "#graphql/generated/contentful/schema";import type { TransformedArticle } from "./transform.types";
// Main transform functionexport function transformArticleData( data: ArticlePageQuery): TransformedArticle { const article = data.internalPageCollection?.items[0];
return { title: article?.title ?? "", slug: article?.slug ?? "", content: transformContent(article?.content), hero: transformHero(article?.hero), metadata: transformMetadata(article?.metadata), };}
// Helper transform functionsfunction transformHero(hero: HeroFromQuery | null | undefined): HeroData { if (!hero) { return { title: "", image: null }; }
return { title: hero.title ?? "", image: hero.image ? transformImage(hero.image) : null, cta: hero.cta ? transformCTA(hero.cta) : null, };}Common Patterns
Section titled “Common Patterns”Nullish Coalescing
Section titled “Nullish Coalescing”title: article?.title ?? "",count: article?.viewCount ?? 0,tags: article?.tags ?? [],Optional Chaining
Section titled “Optional Chaining”const title = data.internalPageCollection?.items[0]?.title ?? "";Array Transformations
Section titled “Array Transformations”const articles = data.articleCollection?.items .filter((item): item is NonNullable<typeof item> => item !== null) .map(transformArticleListItem) ?? [];Union Type Handling
Section titled “Union Type Handling”function transformComponent(component: ComponentUnion): ContentBlock { switch (component.__typename) { case "ComponentHero": return transformHeroBlock(component); case "ComponentText": return transformTextBlock(component); default: return { type: "unknown" }; }}Related
Section titled “Related”- transform.types.ts - Transform type definitions
- api.ts - API functions that call transforms
- Function Naming - Function naming conventions
- GraphQL Query Patterns - Transform patterns and best practices