@graphcommerce/hygraph-cli 9.0.0-canary.72 → 9.0.0-canary.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/Config.graphqls +6 -10
- package/dist/UpsertClient.js +69 -0
- package/dist/index.js +2 -2
- package/dist/migrateHygraphCli.js +111 -0
- package/dist/migrationActionFactory.js +133 -0
- package/dist/migrations/graphcommerce5to6.js +12 -14
- package/dist/migrations/graphcommerce6to7.js +25 -27
- package/dist/migrations/graphcommerce7to8.js +7 -9
- package/dist/migrations/graphcommerce8to9.js +54 -8
- package/dist/readSchema.js +10 -21
- package/dist/utils/getConfig.js +19 -0
- package/dist/utils/getEndpointUrl.js +43 -0
- package/dist/utils/getManagementClient.js +15 -0
- package/dist/{log-functions.js → utils/graphCommerceLog.js} +1 -1
- package/package.json +5 -5
- package/src/UpsertClient.ts +99 -0
- package/src/index.ts +1 -1
- package/src/migrateHygraphCli.ts +113 -0
- package/src/migrationActionFactory.ts +230 -0
- package/src/migrations/graphcommerce5to6.ts +4 -6
- package/src/migrations/graphcommerce6to7.ts +4 -6
- package/src/migrations/graphcommerce7to8.ts +4 -6
- package/src/migrations/graphcommerce8to9.ts +79 -8
- package/src/readSchema.ts +24 -29
- package/src/types.ts +6 -1
- package/src/utils/getConfig.ts +33 -0
- package/src/utils/getEndpointUrl.ts +65 -0
- package/src/utils/getManagementClient.ts +15 -0
- package/src/{log-functions.ts → utils/graphCommerceLog.ts} +1 -1
- package/dist/client.js +0 -21
- package/dist/migrateHygraph.js +0 -95
- package/dist/migrationAction.js +0 -133
- package/src/client.ts +0 -25
- package/src/migrateHygraph.ts +0 -86
- package/src/migrationAction.ts +0 -223
package/dist/migrateHygraph.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.migrateHygraph = migrateHygraph;
|
|
30
|
-
const fs_1 = __importDefault(require("fs"));
|
|
31
|
-
const next_config_1 = require("@graphcommerce/next-config");
|
|
32
|
-
const dotenv_1 = __importDefault(require("dotenv"));
|
|
33
|
-
const prompts_1 = __importDefault(require("prompts"));
|
|
34
|
-
const log_functions_1 = require("./log-functions");
|
|
35
|
-
const migrations = __importStar(require("./migrations"));
|
|
36
|
-
const readSchema_1 = require("./readSchema");
|
|
37
|
-
dotenv_1.default.config();
|
|
38
|
-
async function migrateHygraph() {
|
|
39
|
-
const config = (0, next_config_1.loadConfig)(process.cwd());
|
|
40
|
-
/**
|
|
41
|
-
* Extracting the current GC version. Are we gonna use the current version to determine which
|
|
42
|
-
* scripts should be runned? Or do we let the user pick the migration from a list? 🤔
|
|
43
|
-
*/
|
|
44
|
-
const packageJson = fs_1.default.readFileSync('package.json', 'utf8');
|
|
45
|
-
const packageData = JSON.parse(packageJson);
|
|
46
|
-
const graphcommerceVersion = packageData.dependencies['@graphcommerce/next-ui'];
|
|
47
|
-
(0, log_functions_1.graphcommerceLog)(`Graphcommerce version: ${graphcommerceVersion}`, 'info');
|
|
48
|
-
// Extract the currently existing models, components and enumerations from the Hygraph schema.
|
|
49
|
-
const schemaViewer = await (0, readSchema_1.readSchema)(config);
|
|
50
|
-
const schema = schemaViewer.viewer.project.environment.contentModel;
|
|
51
|
-
// A list of possible migrations
|
|
52
|
-
const possibleMigrations = Object.entries(migrations);
|
|
53
|
-
// Here we setup the list we ask the user to choose from
|
|
54
|
-
const selectMigrationInput = {
|
|
55
|
-
type: 'select',
|
|
56
|
-
name: 'selectedMigration',
|
|
57
|
-
message: '\x1b[36m\x1b[1m[GraphCommerce]: Select migration',
|
|
58
|
-
choices: [],
|
|
59
|
-
};
|
|
60
|
-
for (const [name, migration] of possibleMigrations) {
|
|
61
|
-
if (Array.isArray(selectMigrationInput.choices)) {
|
|
62
|
-
selectMigrationInput?.choices?.push({ title: name, value: { name, migration } });
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// Here we ask the user to choose a migration from a list of possible migrations
|
|
66
|
-
try {
|
|
67
|
-
(0, log_functions_1.graphcommerceLog)('Available migrations: ', 'info');
|
|
68
|
-
const selectMigrationOutput = await (0, prompts_1.default)(selectMigrationInput);
|
|
69
|
-
const { migration, name } = selectMigrationOutput.selectedMigration;
|
|
70
|
-
(0, log_functions_1.graphcommerceLog)(`You have selected the ${selectMigrationOutput.selectedMigration.name} migration`, 'info');
|
|
71
|
-
try {
|
|
72
|
-
// Here we try to run the migration
|
|
73
|
-
// eslint-disable-next-line no-await-in-loop
|
|
74
|
-
const result = await migration(schema);
|
|
75
|
-
(0, log_functions_1.graphcommerceLog)(`Migration result: ${JSON.stringify(result)}`, 'info');
|
|
76
|
-
if (!result) {
|
|
77
|
-
throw new Error('[GraphCommerce]: No migration client found. Please make sure your GC_HYGRAPH_WRITE_ACCESS_ENDPOINT and GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file are correct.');
|
|
78
|
-
}
|
|
79
|
-
if (result.status !== 'SUCCESS') {
|
|
80
|
-
throw new Error(`[GraphCommerce]: Migration not successful: ${result.status} ${name}:\n${result.errors}`);
|
|
81
|
-
}
|
|
82
|
-
(0, log_functions_1.graphcommerceLog)(`Migration successful: ${name}`, 'info');
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
if (err instanceof Error) {
|
|
86
|
-
const garbledErrorIndex = err.message.indexOf(': {"');
|
|
87
|
-
const msg = garbledErrorIndex > 0 ? err.message.slice(0, garbledErrorIndex) : err.message;
|
|
88
|
-
(0, log_functions_1.graphcommerceLog)(`${msg}`, 'error');
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
(0, log_functions_1.graphcommerceLog)(`[GraphCommerce]: An error occurred: ${error}`, 'error');
|
|
94
|
-
}
|
|
95
|
-
}
|
package/dist/migrationAction.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.migrationAction = exports.client = void 0;
|
|
7
|
-
const next_config_1 = require("@graphcommerce/next-config");
|
|
8
|
-
const dotenv_1 = __importDefault(require("dotenv"));
|
|
9
|
-
const client_1 = require("./client");
|
|
10
|
-
const log_functions_1 = require("./log-functions");
|
|
11
|
-
dotenv_1.default.config();
|
|
12
|
-
const config = (0, next_config_1.loadConfig)(process.cwd());
|
|
13
|
-
exports.client = (0, client_1.initClient)(config, undefined);
|
|
14
|
-
/**
|
|
15
|
-
* This constant is used to assign the right action of the management SDK to the migratioAction
|
|
16
|
-
* function
|
|
17
|
-
*/
|
|
18
|
-
const actionMap = exports.client
|
|
19
|
-
? {
|
|
20
|
-
create: {
|
|
21
|
-
model: (innerprops) => exports.client.createModel(innerprops),
|
|
22
|
-
component: (innerprops) => exports.client.createComponent(innerprops),
|
|
23
|
-
enumeration: (innerprops) => exports.client.createEnumeration(innerprops),
|
|
24
|
-
simpleField: (innerprops) => exports.client.createSimpleField(innerprops),
|
|
25
|
-
enumerableField: (innerprops) => exports.client.createEnumerableField(innerprops),
|
|
26
|
-
componentField: (innerprops) => exports.client.createComponentField(innerprops),
|
|
27
|
-
relationalField: (innerprops) => exports.client.createRelationalField(innerprops),
|
|
28
|
-
unionField: (innerprops) => exports.client.createUnionField(innerprops),
|
|
29
|
-
componentUnionField: (innerprops) => exports.client.createComponentUnionField(innerprops),
|
|
30
|
-
},
|
|
31
|
-
update: {
|
|
32
|
-
model: (innerprops) => exports.client.updateModel(innerprops),
|
|
33
|
-
component: (innerprops) => exports.client.updateComponent(innerprops),
|
|
34
|
-
enumeration: (innerprops) => exports.client.updateEnumeration(innerprops),
|
|
35
|
-
simpleField: (innerprops) => exports.client.updateSimpleField(innerprops),
|
|
36
|
-
enumerableField: (innerprops) => exports.client.updateEnumerableField(innerprops),
|
|
37
|
-
componentField: (innerprops) => exports.client.updateComponentField(innerprops),
|
|
38
|
-
relationalField: (innerprops) => exports.client.updateRelationalField(innerprops),
|
|
39
|
-
unionField: (innerprops) => exports.client.updateUnionField(innerprops),
|
|
40
|
-
componentUnionField: (innerprops) => exports.client.updateComponentUnionField(innerprops),
|
|
41
|
-
},
|
|
42
|
-
delete: {
|
|
43
|
-
model: (innerprops) => exports.client.deleteModel(innerprops),
|
|
44
|
-
component: (innerprops) => exports.client.deleteComponent(innerprops),
|
|
45
|
-
enumeration: (innerprops) => exports.client.deleteEnumeration(innerprops),
|
|
46
|
-
simpleField: (innerprops) => exports.client.deleteField(innerprops),
|
|
47
|
-
enumerableField: (innerprops) => exports.client.deleteField(innerprops),
|
|
48
|
-
componentField: (innerprops) => exports.client.deleteField(innerprops),
|
|
49
|
-
relationalField: (innerprops) => exports.client.deleteField(innerprops),
|
|
50
|
-
unionField: (innerprops) => exports.client.deleteField(innerprops),
|
|
51
|
-
componentUnionField: (innerprops) => exports.client.deleteField(innerprops),
|
|
52
|
-
},
|
|
53
|
-
}
|
|
54
|
-
: undefined;
|
|
55
|
-
/**
|
|
56
|
-
* This function is our variation on the client.migrationAction functions from the hygraph
|
|
57
|
-
* management sdk.
|
|
58
|
-
*
|
|
59
|
-
* MigrationAction() is better suited because it is a single function for all actions. More
|
|
60
|
-
* importantly, if the action fails, because a field with the apiID already exists for instance. it
|
|
61
|
-
* will skip this action but still continue the whole migration, while the management sdk function
|
|
62
|
-
* will bail.
|
|
63
|
-
*
|
|
64
|
-
* It takes the schema as argument, which is always the same.
|
|
65
|
-
*
|
|
66
|
-
* Then it takes the type of schema entity you want to do an action upon, and the action you want to
|
|
67
|
-
* do.
|
|
68
|
-
*
|
|
69
|
-
* The fourth arguments are the props that belong to the action you want to do. For instance, if you
|
|
70
|
-
* want to create a model, you need to pass the props that belong to a model.
|
|
71
|
-
*
|
|
72
|
-
* The last two arguments are optional. If you want to create a field, you need to pass the apiId of
|
|
73
|
-
* the model or component you want to create the field on. If you want to create a field on a
|
|
74
|
-
* component, you also need to pass the parentType, which is either 'model' or 'component'.
|
|
75
|
-
*/
|
|
76
|
-
const migrationAction = (schema, type, action, props, parentApiId, parentType) => {
|
|
77
|
-
/**
|
|
78
|
-
* Check if the entity already exists.
|
|
79
|
-
* If an update or deletion is made, it does not matter if the entity already exists
|
|
80
|
-
*/
|
|
81
|
-
const alreadyExists = () => {
|
|
82
|
-
if (action !== 'create') {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
switch (type) {
|
|
86
|
-
case 'model':
|
|
87
|
-
return schema.models.some((model) => model.apiId === props.apiId);
|
|
88
|
-
case 'component':
|
|
89
|
-
return schema.components.some((component) => component.apiId === props.apiId);
|
|
90
|
-
case 'enumeration':
|
|
91
|
-
return schema.enumerations.some((enumeration) => enumeration.apiId === props.apiId);
|
|
92
|
-
case 'simpleField':
|
|
93
|
-
case 'enumerableField':
|
|
94
|
-
case 'relationalField':
|
|
95
|
-
case 'unionField':
|
|
96
|
-
case 'componentUnionField': {
|
|
97
|
-
let parent;
|
|
98
|
-
switch (parentType) {
|
|
99
|
-
case 'model': {
|
|
100
|
-
parent = schema.models.find((model) => model.apiId === parentApiId);
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
case 'component': {
|
|
104
|
-
parent = schema.components.find((component) => component.apiId === parentApiId);
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
default:
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
return parent?.fields.some((field) => field.apiId === props.apiId);
|
|
111
|
-
}
|
|
112
|
-
default: {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
const actionFunc = actionMap && actionMap[action] && actionMap[action][type];
|
|
118
|
-
if (!alreadyExists()) {
|
|
119
|
-
if (actionFunc) {
|
|
120
|
-
(0, log_functions_1.graphcommerceLog)(`${(0, log_functions_1.capitalize)(action)} ${type} with apiId ${props.apiId}...`);
|
|
121
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
122
|
-
// @ts-ignore | This error is a loss on typescript autocomplete, but the function is called correctly
|
|
123
|
-
actionFunc(props);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
(0, log_functions_1.graphcommerceLog)(`Action ${action} is not supported for ${type}`, 'error');
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
(0, log_functions_1.graphcommerceLog)(`${(0, log_functions_1.capitalize)(type)} with apiId ${props.apiId} on ${parentApiId} already exists`, 'warning');
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
exports.migrationAction = migrationAction;
|
package/src/client.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { GraphCommerceConfig } from '@graphcommerce/next-config'
|
|
2
|
-
import { Client } from '@hygraph/management-sdk'
|
|
3
|
-
import { graphcommerceLog } from './log-functions'
|
|
4
|
-
|
|
5
|
-
export const initClient = (config: GraphCommerceConfig, name: string | undefined) => {
|
|
6
|
-
if (!config.hygraphWriteAccessEndpoint) {
|
|
7
|
-
graphcommerceLog(
|
|
8
|
-
'Please provide hygraphWriteAccessEndpoint in your config or GC_HYGRAPH_WRITE_ACCESS_ENDPOINT in your env file',
|
|
9
|
-
'error',
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
return 0
|
|
13
|
-
}
|
|
14
|
-
if (!config.hygraphWriteAccessToken) {
|
|
15
|
-
graphcommerceLog('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file')
|
|
16
|
-
|
|
17
|
-
return 0
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return new Client({
|
|
21
|
-
authToken: config.hygraphWriteAccessToken,
|
|
22
|
-
endpoint: config.hygraphWriteAccessEndpoint,
|
|
23
|
-
name,
|
|
24
|
-
})
|
|
25
|
-
}
|
package/src/migrateHygraph.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import { loadConfig } from '@graphcommerce/next-config'
|
|
3
|
-
import type { MigrationInfo } from '@hygraph/management-sdk/dist/src/ManagementAPIClient'
|
|
4
|
-
import dotenv from 'dotenv'
|
|
5
|
-
import prompts, { PromptObject } from 'prompts'
|
|
6
|
-
import { graphcommerceLog } from './log-functions'
|
|
7
|
-
import * as migrations from './migrations'
|
|
8
|
-
import { readSchema } from './readSchema'
|
|
9
|
-
import { Schema } from './types'
|
|
10
|
-
|
|
11
|
-
dotenv.config()
|
|
12
|
-
|
|
13
|
-
export async function migrateHygraph() {
|
|
14
|
-
const config = loadConfig(process.cwd())
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Extracting the current GC version. Are we gonna use the current version to determine which
|
|
18
|
-
* scripts should be runned? Or do we let the user pick the migration from a list? 🤔
|
|
19
|
-
*/
|
|
20
|
-
const packageJson = fs.readFileSync('package.json', 'utf8')
|
|
21
|
-
const packageData = JSON.parse(packageJson)
|
|
22
|
-
const graphcommerceVersion = packageData.dependencies['@graphcommerce/next-ui']
|
|
23
|
-
|
|
24
|
-
graphcommerceLog(`Graphcommerce version: ${graphcommerceVersion}`, 'info')
|
|
25
|
-
|
|
26
|
-
// Extract the currently existing models, components and enumerations from the Hygraph schema.
|
|
27
|
-
const schemaViewer = await readSchema(config)
|
|
28
|
-
const schema: Schema = schemaViewer.viewer.project.environment.contentModel
|
|
29
|
-
|
|
30
|
-
// A list of possible migrations
|
|
31
|
-
const possibleMigrations: [string, (schema: Schema) => Promise<0 | MigrationInfo>][] =
|
|
32
|
-
Object.entries(migrations)
|
|
33
|
-
|
|
34
|
-
// Here we setup the list we ask the user to choose from
|
|
35
|
-
const selectMigrationInput: PromptObject<string> | PromptObject<string>[] = {
|
|
36
|
-
type: 'select',
|
|
37
|
-
name: 'selectedMigration',
|
|
38
|
-
message: '\x1b[36m\x1b[1m[GraphCommerce]: Select migration',
|
|
39
|
-
choices: [],
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (const [name, migration] of possibleMigrations) {
|
|
43
|
-
if (Array.isArray(selectMigrationInput.choices)) {
|
|
44
|
-
selectMigrationInput?.choices?.push({ title: name, value: { name, migration } })
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Here we ask the user to choose a migration from a list of possible migrations
|
|
49
|
-
try {
|
|
50
|
-
graphcommerceLog('Available migrations: ', 'info')
|
|
51
|
-
const selectMigrationOutput = await prompts(selectMigrationInput)
|
|
52
|
-
const { migration, name } = selectMigrationOutput.selectedMigration
|
|
53
|
-
graphcommerceLog(
|
|
54
|
-
`You have selected the ${selectMigrationOutput.selectedMigration.name} migration`,
|
|
55
|
-
'info',
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
// Here we try to run the migration
|
|
60
|
-
// eslint-disable-next-line no-await-in-loop
|
|
61
|
-
const result = await migration(schema)
|
|
62
|
-
|
|
63
|
-
graphcommerceLog(`Migration result: ${JSON.stringify(result)}`, 'info')
|
|
64
|
-
if (!result) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
'[GraphCommerce]: No migration client found. Please make sure your GC_HYGRAPH_WRITE_ACCESS_ENDPOINT and GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file are correct.',
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
if (result.status !== 'SUCCESS') {
|
|
70
|
-
throw new Error(
|
|
71
|
-
`[GraphCommerce]: Migration not successful: ${result.status} ${name}:\n${result.errors}`,
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
graphcommerceLog(`Migration successful: ${name}`, 'info')
|
|
76
|
-
} catch (err) {
|
|
77
|
-
if (err instanceof Error) {
|
|
78
|
-
const garbledErrorIndex = err.message.indexOf(': {"')
|
|
79
|
-
const msg = garbledErrorIndex > 0 ? err.message.slice(0, garbledErrorIndex) : err.message
|
|
80
|
-
graphcommerceLog(`${msg}`, 'error')
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} catch (error) {
|
|
84
|
-
graphcommerceLog(`[GraphCommerce]: An error occurred: ${error}`, 'error')
|
|
85
|
-
}
|
|
86
|
-
}
|
package/src/migrationAction.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import { loadConfig } from '@graphcommerce/next-config'
|
|
2
|
-
import {
|
|
3
|
-
BatchMigrationCreateModelInput,
|
|
4
|
-
BatchMigrationCreateComponentInput,
|
|
5
|
-
BatchMigrationCreateEnumerationInput,
|
|
6
|
-
BatchMigrationCreateSimpleFieldInput,
|
|
7
|
-
BatchMigrationCreateEnumerableFieldInput,
|
|
8
|
-
BatchMigrationCreateRelationalFieldInput,
|
|
9
|
-
BatchMigrationCreateUnionFieldInput,
|
|
10
|
-
BatchMigrationCreateComponentFieldInput,
|
|
11
|
-
BatchMigrationDeleteComponentInput,
|
|
12
|
-
BatchMigrationDeleteEnumerationInput,
|
|
13
|
-
BatchMigrationDeleteModelInput,
|
|
14
|
-
BatchMigrationUpdateComponentFieldInput,
|
|
15
|
-
BatchMigrationUpdateComponentInput,
|
|
16
|
-
BatchMigrationUpdateEnumerableFieldInput,
|
|
17
|
-
BatchMigrationUpdateEnumerationInput,
|
|
18
|
-
BatchMigrationUpdateModelInput,
|
|
19
|
-
BatchMigrationUpdateRelationalFieldInput,
|
|
20
|
-
BatchMigrationUpdateSimpleFieldInput,
|
|
21
|
-
BatchMigrationUpdateUnionFieldInput,
|
|
22
|
-
BatchMigrationDeleteFieldInput,
|
|
23
|
-
BatchMigrationCreateComponentUnionFieldInput,
|
|
24
|
-
BatchMigrationUpdateComponentUnionFieldInput,
|
|
25
|
-
} from '@hygraph/management-sdk'
|
|
26
|
-
import dotenv from 'dotenv'
|
|
27
|
-
import { initClient } from './client'
|
|
28
|
-
import { graphcommerceLog, capitalize } from './log-functions'
|
|
29
|
-
import { Schema } from './types'
|
|
30
|
-
|
|
31
|
-
dotenv.config()
|
|
32
|
-
const config = loadConfig(process.cwd())
|
|
33
|
-
|
|
34
|
-
export const client = initClient(config, undefined)
|
|
35
|
-
|
|
36
|
-
type AllActions =
|
|
37
|
-
| BatchMigrationCreateModelInput
|
|
38
|
-
| BatchMigrationUpdateModelInput
|
|
39
|
-
| BatchMigrationDeleteModelInput
|
|
40
|
-
| BatchMigrationCreateComponentInput
|
|
41
|
-
| BatchMigrationUpdateComponentInput
|
|
42
|
-
| BatchMigrationDeleteComponentInput
|
|
43
|
-
| BatchMigrationCreateEnumerationInput
|
|
44
|
-
| BatchMigrationUpdateEnumerationInput
|
|
45
|
-
| BatchMigrationDeleteEnumerationInput
|
|
46
|
-
| BatchMigrationCreateSimpleFieldInput // type: SimpleFieldType
|
|
47
|
-
| BatchMigrationUpdateSimpleFieldInput
|
|
48
|
-
| BatchMigrationCreateEnumerableFieldInput
|
|
49
|
-
| BatchMigrationUpdateEnumerableFieldInput
|
|
50
|
-
| BatchMigrationCreateRelationalFieldInput // type: RelationalFieldType
|
|
51
|
-
| BatchMigrationUpdateRelationalFieldInput
|
|
52
|
-
| BatchMigrationCreateUnionFieldInput
|
|
53
|
-
| BatchMigrationUpdateUnionFieldInput
|
|
54
|
-
| BatchMigrationCreateComponentFieldInput
|
|
55
|
-
| BatchMigrationUpdateComponentFieldInput
|
|
56
|
-
| BatchMigrationDeleteFieldInput
|
|
57
|
-
| BatchMigrationCreateComponentUnionFieldInput
|
|
58
|
-
| BatchMigrationUpdateComponentUnionFieldInput
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* This constant is used to assign the right action of the management SDK to the migratioAction
|
|
62
|
-
* function
|
|
63
|
-
*/
|
|
64
|
-
const actionMap = client
|
|
65
|
-
? {
|
|
66
|
-
create: {
|
|
67
|
-
model: (innerprops: BatchMigrationCreateModelInput) => client.createModel(innerprops),
|
|
68
|
-
component: (innerprops: BatchMigrationCreateComponentInput) =>
|
|
69
|
-
client.createComponent(innerprops),
|
|
70
|
-
enumeration: (innerprops: BatchMigrationCreateEnumerationInput) =>
|
|
71
|
-
client.createEnumeration(innerprops),
|
|
72
|
-
simpleField: (innerprops: BatchMigrationCreateSimpleFieldInput) =>
|
|
73
|
-
client.createSimpleField(innerprops),
|
|
74
|
-
enumerableField: (innerprops: BatchMigrationCreateEnumerableFieldInput) =>
|
|
75
|
-
client.createEnumerableField(innerprops),
|
|
76
|
-
componentField: (innerprops: BatchMigrationCreateComponentFieldInput) =>
|
|
77
|
-
client.createComponentField(innerprops),
|
|
78
|
-
relationalField: (innerprops: BatchMigrationCreateRelationalFieldInput) =>
|
|
79
|
-
client.createRelationalField(innerprops),
|
|
80
|
-
unionField: (innerprops: BatchMigrationCreateUnionFieldInput) =>
|
|
81
|
-
client.createUnionField(innerprops),
|
|
82
|
-
componentUnionField: (innerprops: BatchMigrationCreateComponentUnionFieldInput) =>
|
|
83
|
-
client.createComponentUnionField(innerprops),
|
|
84
|
-
},
|
|
85
|
-
update: {
|
|
86
|
-
model: (innerprops: BatchMigrationUpdateModelInput) => client.updateModel(innerprops),
|
|
87
|
-
component: (innerprops: BatchMigrationUpdateComponentInput) =>
|
|
88
|
-
client.updateComponent(innerprops),
|
|
89
|
-
enumeration: (innerprops: BatchMigrationUpdateEnumerationInput) =>
|
|
90
|
-
client.updateEnumeration(innerprops),
|
|
91
|
-
simpleField: (innerprops: BatchMigrationUpdateSimpleFieldInput) =>
|
|
92
|
-
client.updateSimpleField(innerprops),
|
|
93
|
-
enumerableField: (innerprops: BatchMigrationUpdateEnumerableFieldInput) =>
|
|
94
|
-
client.updateEnumerableField(innerprops),
|
|
95
|
-
componentField: (innerprops: BatchMigrationUpdateComponentFieldInput) =>
|
|
96
|
-
client.updateComponentField(innerprops),
|
|
97
|
-
relationalField: (innerprops: BatchMigrationUpdateRelationalFieldInput) =>
|
|
98
|
-
client.updateRelationalField(innerprops),
|
|
99
|
-
unionField: (innerprops: BatchMigrationUpdateUnionFieldInput) =>
|
|
100
|
-
client.updateUnionField(innerprops),
|
|
101
|
-
componentUnionField: (innerprops: BatchMigrationUpdateComponentUnionFieldInput) =>
|
|
102
|
-
client.updateComponentUnionField(innerprops),
|
|
103
|
-
},
|
|
104
|
-
delete: {
|
|
105
|
-
model: (innerprops: BatchMigrationDeleteModelInput) => client.deleteModel(innerprops),
|
|
106
|
-
component: (innerprops: BatchMigrationDeleteComponentInput) =>
|
|
107
|
-
client.deleteComponent(innerprops),
|
|
108
|
-
enumeration: (innerprops: BatchMigrationDeleteEnumerationInput) =>
|
|
109
|
-
client.deleteEnumeration(innerprops),
|
|
110
|
-
simpleField: (innerprops: BatchMigrationDeleteFieldInput) => client.deleteField(innerprops),
|
|
111
|
-
enumerableField: (innerprops: BatchMigrationDeleteFieldInput) =>
|
|
112
|
-
client.deleteField(innerprops),
|
|
113
|
-
componentField: (innerprops: BatchMigrationDeleteFieldInput) =>
|
|
114
|
-
client.deleteField(innerprops),
|
|
115
|
-
relationalField: (innerprops: BatchMigrationDeleteFieldInput) =>
|
|
116
|
-
client.deleteField(innerprops),
|
|
117
|
-
unionField: (innerprops: BatchMigrationDeleteFieldInput) => client.deleteField(innerprops),
|
|
118
|
-
componentUnionField: (innerprops: BatchMigrationDeleteFieldInput) =>
|
|
119
|
-
client.deleteField(innerprops),
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
: undefined
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* This function is our variation on the client.migrationAction functions from the hygraph
|
|
126
|
-
* management sdk.
|
|
127
|
-
*
|
|
128
|
-
* MigrationAction() is better suited because it is a single function for all actions. More
|
|
129
|
-
* importantly, if the action fails, because a field with the apiID already exists for instance. it
|
|
130
|
-
* will skip this action but still continue the whole migration, while the management sdk function
|
|
131
|
-
* will bail.
|
|
132
|
-
*
|
|
133
|
-
* It takes the schema as argument, which is always the same.
|
|
134
|
-
*
|
|
135
|
-
* Then it takes the type of schema entity you want to do an action upon, and the action you want to
|
|
136
|
-
* do.
|
|
137
|
-
*
|
|
138
|
-
* The fourth arguments are the props that belong to the action you want to do. For instance, if you
|
|
139
|
-
* want to create a model, you need to pass the props that belong to a model.
|
|
140
|
-
*
|
|
141
|
-
* The last two arguments are optional. If you want to create a field, you need to pass the apiId of
|
|
142
|
-
* the model or component you want to create the field on. If you want to create a field on a
|
|
143
|
-
* component, you also need to pass the parentType, which is either 'model' or 'component'.
|
|
144
|
-
*/
|
|
145
|
-
export const migrationAction = (
|
|
146
|
-
schema: Schema,
|
|
147
|
-
type:
|
|
148
|
-
| 'model'
|
|
149
|
-
| 'component'
|
|
150
|
-
| 'enumeration'
|
|
151
|
-
| 'simpleField'
|
|
152
|
-
| 'componentField'
|
|
153
|
-
| 'enumerableField'
|
|
154
|
-
| 'relationalField'
|
|
155
|
-
| 'unionField'
|
|
156
|
-
| 'componentUnionField',
|
|
157
|
-
action: 'create' | 'update' | 'delete',
|
|
158
|
-
props: AllActions,
|
|
159
|
-
parentApiId?: string,
|
|
160
|
-
parentType?: 'model' | 'component' | 'enumeration',
|
|
161
|
-
) => {
|
|
162
|
-
/**
|
|
163
|
-
* Check if the entity already exists.
|
|
164
|
-
* If an update or deletion is made, it does not matter if the entity already exists
|
|
165
|
-
*/
|
|
166
|
-
const alreadyExists = () => {
|
|
167
|
-
if (action !== 'create') {
|
|
168
|
-
return false
|
|
169
|
-
}
|
|
170
|
-
switch (type) {
|
|
171
|
-
case 'model':
|
|
172
|
-
return schema.models.some((model) => model.apiId === props.apiId)
|
|
173
|
-
|
|
174
|
-
case 'component':
|
|
175
|
-
return schema.components.some((component) => component.apiId === props.apiId)
|
|
176
|
-
|
|
177
|
-
case 'enumeration':
|
|
178
|
-
return schema.enumerations.some((enumeration) => enumeration.apiId === props.apiId)
|
|
179
|
-
|
|
180
|
-
case 'simpleField':
|
|
181
|
-
case 'enumerableField':
|
|
182
|
-
case 'relationalField':
|
|
183
|
-
case 'unionField':
|
|
184
|
-
case 'componentUnionField': {
|
|
185
|
-
let parent
|
|
186
|
-
switch (parentType) {
|
|
187
|
-
case 'model': {
|
|
188
|
-
parent = schema.models.find((model) => model.apiId === parentApiId)
|
|
189
|
-
break
|
|
190
|
-
}
|
|
191
|
-
case 'component': {
|
|
192
|
-
parent = schema.components.find((component) => component.apiId === parentApiId)
|
|
193
|
-
break
|
|
194
|
-
}
|
|
195
|
-
default:
|
|
196
|
-
return false
|
|
197
|
-
}
|
|
198
|
-
return parent?.fields.some((field) => field.apiId === props.apiId)
|
|
199
|
-
}
|
|
200
|
-
default: {
|
|
201
|
-
return false
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const actionFunc = actionMap && actionMap[action] && actionMap[action][type]
|
|
207
|
-
|
|
208
|
-
if (!alreadyExists()) {
|
|
209
|
-
if (actionFunc) {
|
|
210
|
-
graphcommerceLog(`${capitalize(action)} ${type} with apiId ${props.apiId}...`)
|
|
211
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
212
|
-
// @ts-ignore | This error is a loss on typescript autocomplete, but the function is called correctly
|
|
213
|
-
actionFunc(props)
|
|
214
|
-
} else {
|
|
215
|
-
graphcommerceLog(`Action ${action} is not supported for ${type}`, 'error')
|
|
216
|
-
}
|
|
217
|
-
} else {
|
|
218
|
-
graphcommerceLog(
|
|
219
|
-
`${capitalize(type)} with apiId ${props.apiId} on ${parentApiId} already exists`,
|
|
220
|
-
'warning',
|
|
221
|
-
)
|
|
222
|
-
}
|
|
223
|
-
}
|