Skip to content

Commit

Permalink
Merge pull request #2182 from graphcommerce-org/fix/dynamic-rows
Browse files Browse the repository at this point in the history
Dynamic rows would break page rendering if there was a dynamic row but no page returned
  • Loading branch information
paales authored Jan 29, 2024
2 parents 2b5c442 + 1d0980b commit 0a1ff0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-weeks-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/hygraph-dynamic-rows": patch
---

Dynamic rows would break page rendering if there was a dynamic row but no page returned
33 changes: 24 additions & 9 deletions packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* - Boven de product description zetten? in rowrenderer zetten
* - Hoe gaan we dit optioneel maken?
* - Hoe gaan we dit upgradebaar maken? management sdk
*/

import { HygraphPagesQuery } from '@graphcommerce/graphcms-ui'
import { ApolloClient, NormalizedCacheObject } from '@graphcommerce/graphql'
import {
Expand Down Expand Up @@ -85,6 +79,8 @@ function matchCondition(
return false
}

type Page = HygraphPagesQuery['pages'][number]

/**
* Fetch the page content for the given urls.
*
Expand Down Expand Up @@ -119,14 +115,17 @@ export async function hygraphDynamicRows(

const [pageResult, dynamicResult] = await Promise.all([pageQuery, dynamicRows])

const page = pageResult.data.pages[0] as Page | undefined

// Create a copy of the content array.
const content = pageResult.data.pages[0]?.content ?? []
const content = page?.content ?? []

dynamicResult?.data.dynamicRows.forEach((dynamicRow) => {
const { placement, target, rows, row } = dynamicRow
if (!rows && !row) return

const rowsToMerge = rows.length > 0 ? rows : [row]
const rowsToMerge = rows
if (row && rows.length === 0) rowsToMerge.push(row)

if (!target) {
if (placement === 'BEFORE') content.unshift(...rowsToMerge)
Expand All @@ -142,6 +141,22 @@ export async function hygraphDynamicRows(

if (!content.length) return pageResult

const dynamicPage: Page = {
id: 'dynamic-page',
__typename: 'Page',
metaRobots: 'INDEX_FOLLOW',
metaTitle: '',
metaDescription: '',
url: '',
content: [],
relatedPages: [],
}

// Return the merged page result.
return { data: { ...pageResult.data, pages: [{ ...pageResult.data.pages[0], content }] } }
return {
data: {
...pageResult.data,
pages: [{ ...dynamicPage, ...page, content }],
},
}
}

0 comments on commit 0a1ff0d

Please sign in to comment.