Skip to content

Commit

Permalink
Merge pull request #2169 from graphcommerce-org/fix/GCOM-1315-cli-mig…
Browse files Browse the repository at this point in the history
…ration

Fix/gcom 1315 cli migration
  • Loading branch information
paales authored Jan 23, 2024
2 parents afc6c5c + 1ea4c90 commit 3cd54d7
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-socks-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphcommerce/next-config": patch
"@graphcommerce/hygraph-cli": patch
---

Solve issue where a Hygraph DynamicRow conditions were missing fields in the OrCondition
4 changes: 4 additions & 0 deletions docs/framework/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ The Google Tagmanager ID to be used on the site.

This value is required even if you are configuring different values for each locale.

#### hygraphManagementApi: string

Hygraph Management API. **Only used for migrations.**

#### hygraphProjectId: string

Hygraph Project ID. **Only used for migrations.**
Expand Down
16 changes: 10 additions & 6 deletions docs/hygraph/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ The following steps are needed to utilize this tool:
- Update public content views
- Delete public content views
- Can see schema view
5. Add this new token to your env file: `GC_HYGRAPH_WRITE_ACCESS_TOKEN=""`
5. Add this new token to your env file:
`GC_HYGRAPH_WRITE_ACCESS_TOKEN="{YOUR_WRITE_ACCESS_TOKEN}"`
2. Add the Content API key to your env file like this:
1. Open your Hygraph project. Go to: Project settings -> API Access
2. Copy the Content API URL and add to your env file:
`GC_HYGRAPH_WRITE_ACCESS_ENDPOINT=""`
`GC_HYGRAPH_WRITE_ACCESS_ENDPOINT="{YOUR_WRITE_ACCESS_ENDPOINT}"`
3. Add your hygraphProjectId to your env file like this:
1. Copy the project ID from the URL when logged in
`https://app.hygraph.com/PROJECT_ID_IS_HERE/master`
2. Add the project ID to your env file: `GC_HYGRAPH_PROJECT_ID=””`
4. Run `yarn graphcommerce hygraph-migrate`
5. Select the migration you want to run and press enter.
6. The migrations should now be applied, check your Hygraph Schema if changes
2. Add the project ID to your env file:
`GC_HYGRAPH_PROJECT_ID=”{YOUR_PROJECT_ID}”`
4. Add your hygraphManagementApi to your env file like this:
`GC_HYGRAPH_MANAGEMENT_API="{YOUR_MANAGEMENT_API}"`
5. Run `yarn graphcommerce hygraph-migrate`
6. Select the migration you want to run and press enter.
7. The migrations should now be applied, check your Hygraph Schema if changes
are made.
5 changes: 5 additions & 0 deletions packages/hygraph-cli/Config.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ extend input GraphCommerceConfig {
Hygraph Project ID. **Only used for migrations.**
"""
hygraphProjectId: String

"""
Hygraph Management API. **Only used for migrations.**
"""
hygraphManagementApi: String
}
7 changes: 7 additions & 0 deletions packages/hygraph-cli/dist/migrations/graphcommerce6to7.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ const graphcommerce6to7 = async (schema) => {
enumerationApiId: 'RowColumnOneVariants',
parentApiId: 'RowColumnOne',
}, 'RowColumnOne', 'model');
(0, migrationAction_1.migrationAction)(schema, 'componentUnionField', 'create', {
displayName: 'Conditions',
apiId: 'conditions',
parentApiId: 'ConditionOr',
componentApiIds: ['ConditionText', 'ConditionNumber'],
isList: true,
}, 'ConditionOr', 'component');
return migrationAction_1.client.run(true);
};
exports.graphcommerce6to7 = graphcommerce6to7;
91 changes: 56 additions & 35 deletions packages/hygraph-cli/dist/migrations/graphcommerce7to8.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
"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',
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.graphcommerce7to8 = void 0
const management_sdk_1 = require('@hygraph/management-sdk')
const migrationAction_1 = require('../migrationAction')
const graphcommerce7to8 = 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,
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;
},
parentApiId: 'DynamicRow',
},
'DynamicRow',
'model',
)
;(0, migrationAction_1.migrationAction)(
schema,
'componentUnionField',
'create',
{
displayName: 'Conditions',
apiId: 'conditions',
parentApiId: 'ConditionOr',
componentApiIds: ['ConditionText', 'ConditionNumber'],
isList: true,
},
'ConditionOr',
'component',
)
return migrationAction_1.client.run(true)
}
exports.graphcommerce7to8 = graphcommerce7to8
6 changes: 3 additions & 3 deletions packages/hygraph-cli/dist/migrations/graphcommerce8to9.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.graphcommerce7_unknown_to_8 = void 0;
exports.graphcommerce8to9 = void 0;
const migrationAction_1 = require("../migrationAction");
const graphcommerce7_unknown_to_8 = async (schema) => {
const graphcommerce8to9 = async (schema) => {
if (!migrationAction_1.client) {
return 0;
}
Expand All @@ -19,4 +19,4 @@ const graphcommerce7_unknown_to_8 = async (schema) => {
}
return migrationAction_1.client.run(true);
};
exports.graphcommerce7_unknown_to_8 = graphcommerce7_unknown_to_8;
exports.graphcommerce8to9 = graphcommerce8to9;
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 @@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./graphcommerce5to6"), exports);
__exportStar(require("./graphcommerce6to7"), exports);
__exportStar(require("./graphcommerce7to8"), exports);
__exportStar(require("./graphcommerce8to9"), exports);
9 changes: 6 additions & 3 deletions packages/hygraph-cli/dist/readSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ const client_1 = require("@apollo/client");
const fetch_1 = require("@whatwg-node/fetch");
const readSchema = async (config) => {
if (!config.hygraphProjectId) {
throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file');
throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file.');
}
if (!config.hygraphWriteAccessToken) {
throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file');
throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file.');
}
const projectId = config.hygraphProjectId;
if (!config.hygraphManagementApi) {
throw new Error('Please provide GC_HYGRAPH_MANAGEMENT_API in your env file.');
}
const hygraphClient = new client_1.ApolloClient({
link: new client_1.HttpLink({
uri: 'https://management.hygraph.com/graphql',
uri: config.hygraphManagementApi,
fetch: fetch_1.fetch,
headers: { Authorization: `Bearer ${config.hygraphWriteAccessToken}` },
}),
Expand Down
15 changes: 15 additions & 0 deletions packages/hygraph-cli/src/migrations/graphcommerce6to7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,20 @@ export const graphcommerce6to7 = async (schema: Schema) => {
'model',
)

migrationAction(
schema,
'componentUnionField',
'create',
{
displayName: 'Conditions',
apiId: 'conditions',
parentApiId: 'ConditionOr',
componentApiIds: ['ConditionText', 'ConditionNumber'],
isList: true,
},
'ConditionOr',
'component',
)

return client.run(true)
}
17 changes: 16 additions & 1 deletion packages/hygraph-cli/src/migrations/graphcommerce7to8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { VisibilityTypes } from '@hygraph/management-sdk'
import { migrationAction, client } from '../migrationAction'
import { Schema } from '../types'

export const graphcommerce7to7_1 = async (schema: Schema) => {
export const graphcommerce7to8 = async (schema: Schema) => {
if (!client) {
return 0
}
Expand Down Expand Up @@ -41,5 +41,20 @@ export const graphcommerce7to7_1 = async (schema: Schema) => {
'model',
)

migrationAction(
schema,
'componentUnionField',
'create',
{
displayName: 'Conditions',
apiId: 'conditions',
parentApiId: 'ConditionOr',
componentApiIds: ['ConditionText', 'ConditionNumber'],
isList: true,
},
'ConditionOr',
'component',
)

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

export const graphcommerce7_unknown_to_8 = async (schema: Schema) => {
export const graphcommerce8to9 = async (schema: Schema) => {
if (!client) {
return 0
}
Expand Down
1 change: 1 addition & 0 deletions packages/hygraph-cli/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './graphcommerce5to6'
export * from './graphcommerce6to7'
export * from './graphcommerce7to8'
export * from './graphcommerce8to9'
10 changes: 7 additions & 3 deletions packages/hygraph-cli/src/readSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import { fetch } from '@whatwg-node/fetch'

export const readSchema = async (config: GraphCommerceConfig) => {
if (!config.hygraphProjectId) {
throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file')
throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file.')
}

if (!config.hygraphWriteAccessToken) {
throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file')
throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file.')
}

const projectId = config.hygraphProjectId

if (!config.hygraphManagementApi) {
throw new Error('Please provide GC_HYGRAPH_MANAGEMENT_API in your env file.')
}

const hygraphClient = new ApolloClient({
link: new HttpLink({
uri: 'https://management.hygraph.com/graphql',
uri: config.hygraphManagementApi,
fetch,
headers: { Authorization: `Bearer ${config.hygraphWriteAccessToken}` },
}),
Expand Down
1 change: 1 addition & 0 deletions packagesDev/next-config/dist/generated/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function GraphCommerceConfigSchema() {
googleRecaptchaKey: _zod.z.string().nullish(),
googleTagmanagerId: _zod.z.string().nullish(),
hygraphEndpoint: _zod.z.string().min(1),
hygraphManagementApi: _zod.z.string().nullish(),
hygraphProjectId: _zod.z.string().nullish(),
hygraphWriteAccessEndpoint: _zod.z.string().nullish(),
hygraphWriteAccessToken: _zod.z.string().nullish(),
Expand Down
3 changes: 3 additions & 0 deletions packagesDev/next-config/src/generated/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export type GraphCommerceConfig = {
* Project settings -> API Access -> High Performance Read-only Content API
*/
hygraphEndpoint: Scalars['String']['input'];
/** Hygraph Management API. **Only used for migrations.** */
hygraphManagementApi?: InputMaybe<Scalars['String']['input']>;
/** Hygraph Project ID. **Only used for migrations.** */
hygraphProjectId?: InputMaybe<Scalars['String']['input']>;
/**
Expand Down Expand Up @@ -448,6 +450,7 @@ export function GraphCommerceConfigSchema(): z.ZodObject<Properties<GraphCommerc
googleRecaptchaKey: z.string().nullish(),
googleTagmanagerId: z.string().nullish(),
hygraphEndpoint: z.string().min(1),
hygraphManagementApi: z.string().nullish(),
hygraphProjectId: z.string().nullish(),
hygraphWriteAccessEndpoint: z.string().nullish(),
hygraphWriteAccessToken: z.string().nullish(),
Expand Down

0 comments on commit 3cd54d7

Please sign in to comment.