Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple dynamic rows (GCOM-1307) #2139

Merged
merged 8 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/hygraph-cli/dist/migrations/graphcommerce7to8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.graphcommerce7to7_1 = void 0;
const management_sdk_1 = require("@hygraph/management-sdk");
const migrationAction_1 = require("../migrationAction");
const graphcommerce7to7_1 = async (schema) => {
if (!migrationAction_1.client) {
return 0;
}
const hasRow = schema.models
.find((m) => m.apiId === 'DynamicRow')
?.fields.some((f) => f.apiId === 'row');
if (hasRow) {
(0, migrationAction_1.migrationAction)(schema, 'unionField', 'update', {
apiId: 'row',
displayName: 'Row Deprecated',
parentApiId: 'DynamicRow',
description: 'This field is deprecated. Use Rows instead.',
});
}
(0, migrationAction_1.migrationAction)(schema, 'unionField', 'create', {
displayName: 'Rows',
apiId: 'rows',
isList: true,
reverseField: {
modelApiIds: ['RowQuote', 'RowLinks', 'RowColumnOne'],
apiId: 'dynamicRows',
displayName: 'Dynamic Rows',
visibility: management_sdk_1.VisibilityTypes.Hidden,
isList: true,
},
parentApiId: 'DynamicRow',
}, 'DynamicRow', 'model');
return migrationAction_1.client.run(true);
};
exports.graphcommerce7to7_1 = graphcommerce7to7_1;
22 changes: 22 additions & 0 deletions packages/hygraph-cli/dist/migrations/graphcommerce8to9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.graphcommerce7_unknown_to_8 = void 0;
const migrationAction_1 = require("../migrationAction");
const graphcommerce7_unknown_to_8 = async (schema) => {
if (!migrationAction_1.client) {
return 0;
}
// This migration is for GC 8.0 and is not yet exported as a usable migration.
// Removes the deprecated 'Row' field which was deprecated in [email protected]
const hasRow = schema.models
.find((m) => m.apiId === 'DynamicRow')
?.fields.some((f) => f.apiId === 'row');
if (hasRow) {
(0, migrationAction_1.migrationAction)(schema, 'simpleField', 'delete', {
apiId: 'row',
parentApiId: 'DynamicRow',
});
}
return migrationAction_1.client.run(true);
};
exports.graphcommerce7_unknown_to_8 = graphcommerce7_unknown_to_8;
1 change: 1 addition & 0 deletions packages/hygraph-cli/dist/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./graphcommerce5to6"), exports);
__exportStar(require("./graphcommerce6to7"), exports);
__exportStar(require("./graphcommerce7to8"), exports);
45 changes: 45 additions & 0 deletions packages/hygraph-cli/src/migrations/graphcommerce7to8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { VisibilityTypes } from '@hygraph/management-sdk'
import { migrationAction, client } from '../migrationAction'
import { Schema } from '../types'

export const graphcommerce7to7_1 = async (schema: Schema) => {
if (!client) {
return 0
}

const hasRow = schema.models
.find((m) => m.apiId === 'DynamicRow')
?.fields.some((f) => f.apiId === 'row')

if (hasRow) {
migrationAction(schema, 'unionField', 'update', {
apiId: 'row',
displayName: 'Row Deprecated',
parentApiId: 'DynamicRow',
description: 'This field is deprecated. Use Rows instead.',
})
}

migrationAction(
schema,
'unionField',
'create',
{
displayName: 'Rows',
apiId: 'rows',
isList: true,
reverseField: {
modelApiIds: ['RowQuote', 'RowLinks', 'RowColumnOne'],
apiId: 'dynamicRows',
displayName: 'Dynamic Rows',
visibility: VisibilityTypes.Hidden,
isList: true,
},
parentApiId: 'DynamicRow',
},
'DynamicRow',
'model',
)

return client.run(true)
}
24 changes: 24 additions & 0 deletions packages/hygraph-cli/src/migrations/graphcommerce8to9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { migrationAction, client } from '../migrationAction'
import { Schema } from '../types'

export const graphcommerce7_unknown_to_8 = async (schema: Schema) => {
if (!client) {
return 0
}

// This migration is for GC 8.0 and is not yet exported as a usable migration.

// Removes the deprecated 'Row' field which was deprecated in [email protected]
const hasRow = schema.models
.find((m) => m.apiId === 'DynamicRow')
?.fields.some((f) => f.apiId === 'row')

if (hasRow) {
migrationAction(schema, 'simpleField', 'delete', {
apiId: 'row',
parentApiId: 'DynamicRow',
})
}

return client.run(true)
}
1 change: 1 addition & 0 deletions packages/hygraph-cli/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './graphcommerce5to6'
export * from './graphcommerce6to7'
export * from './graphcommerce7to8'
11 changes: 11 additions & 0 deletions packages/hygraph-dynamic-rows/graphql/DynamicRow.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ fragment DynamicRow on DynamicRow @injectable {
...RowQuote
...RowLinks
}

rows {
__typename
... on Node {
id
}

...RowColumnOne
...RowQuote
...RowLinks
}
}
18 changes: 10 additions & 8 deletions packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,24 @@ export async function hygraphDynamicRows(
const [pageResult, dynamicResult] = await Promise.all([pageQuery, dynamicRows])

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

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

const rowsToMerge = rows.length > 0 ? rows : [row]

if (!target) {
if (placement === 'BEFORE') content.unshift(row)
else content.push(row)
if (placement === 'BEFORE') content.unshift(...rowsToMerge)
else content.push(...rowsToMerge)
return
}

const targetIdx = content.findIndex((c) => c.id === target.id)
if (placement === 'BEFORE') content.splice(targetIdx, 0, row)
if (placement === 'AFTER') content.splice(targetIdx + 1, 0, row)
if (placement === 'REPLACE') content.splice(targetIdx, 1, row)
if (placement === 'BEFORE') content.splice(targetIdx, 0, ...rowsToMerge)
if (placement === 'AFTER') content.splice(targetIdx + 1, 0, ...rowsToMerge)
if (placement === 'REPLACE') content.splice(targetIdx, 1, ...rowsToMerge)
})

if (!content.length) return pageResult
Expand Down
24 changes: 11 additions & 13 deletions packagesDev/next-config/dist/withGraphCommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,17 @@ function withGraphCommerce(nextConfig, cwd) {
},
redirects: async () => {
const redirects = (await nextConfig.redirects?.()) ?? [];
if (!graphcommerceConfig.legacyProductRoute) {
const destination = `${graphcommerceConfig.productRoute ?? '/p/'}:url*`;
redirects.push(...[
{ source: '/product/bundle/:url*', destination, permanent: true },
{ source: '/product/configurable/:url*', destination, permanent: true },
{ source: '/product/downloadable/:url*', destination, permanent: true },
{ source: '/product/grouped/:url*', destination, permanent: true },
{ source: '/product/virtual/:url*', destination, permanent: true },
{ source: '/customer/account', destination: '/account', permanent: true },
]);
if (destination !== '/product/:url*')
redirects.push({ source: '/product/:url*', destination, permanent: true });
}
const destination = `${graphcommerceConfig.productRoute ?? '/p/'}:url*`;
redirects.push(...[
{ source: '/product/bundle/:url*', destination, permanent: true },
{ source: '/product/configurable/:url*', destination, permanent: true },
{ source: '/product/downloadable/:url*', destination, permanent: true },
{ source: '/product/grouped/:url*', destination, permanent: true },
{ source: '/product/virtual/:url*', destination, permanent: true },
{ source: '/customer/account', destination: '/account', permanent: true },
]);
if (destination !== '/product/:url*')
redirects.push({ source: '/product/:url*', destination, permanent: true });
return redirects;
},
rewrites: async () => {
Expand Down
Loading