@graphcommerce/hygraph-cli 8.1.0-canary.9 → 9.0.0-canary.101

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +187 -6
  2. package/Config.graphqls +6 -10
  3. package/dist/UpsertClient.js +69 -0
  4. package/dist/index.js +2 -2
  5. package/dist/migrateHygraphCli.js +111 -0
  6. package/dist/migrationActionFactory.js +133 -0
  7. package/dist/migrations/graphcommerce5to6.js +12 -14
  8. package/dist/migrations/graphcommerce6to7.js +25 -27
  9. package/dist/migrations/graphcommerce7to8.js +7 -9
  10. package/dist/migrations/graphcommerce8to9.js +54 -8
  11. package/dist/readSchema.js +10 -21
  12. package/dist/utils/getConfig.js +19 -0
  13. package/dist/utils/getEndpointUrl.js +43 -0
  14. package/dist/utils/getManagementClient.js +15 -0
  15. package/dist/{log-functions.js → utils/graphCommerceLog.js} +1 -1
  16. package/package.json +8 -8
  17. package/src/UpsertClient.ts +99 -0
  18. package/src/index.ts +1 -1
  19. package/src/migrateHygraphCli.ts +113 -0
  20. package/src/migrationActionFactory.ts +230 -0
  21. package/src/migrations/graphcommerce5to6.ts +4 -6
  22. package/src/migrations/graphcommerce6to7.ts +4 -6
  23. package/src/migrations/graphcommerce7to8.ts +4 -6
  24. package/src/migrations/graphcommerce8to9.ts +79 -8
  25. package/src/readSchema.ts +24 -29
  26. package/src/types.ts +6 -1
  27. package/src/utils/getConfig.ts +33 -0
  28. package/src/utils/getEndpointUrl.ts +65 -0
  29. package/src/utils/getManagementClient.ts +15 -0
  30. package/src/{log-functions.ts → utils/graphCommerceLog.ts} +1 -1
  31. package/dist/client.js +0 -21
  32. package/dist/migrateHygraph.js +0 -96
  33. package/dist/migrationAction.js +0 -133
  34. package/src/client.ts +0 -25
  35. package/src/migrateHygraph.ts +0 -86
  36. package/src/migrationAction.ts +0 -223
@@ -2,13 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.graphcommerce6to7 = void 0;
4
4
  const management_sdk_1 = require("@hygraph/management-sdk");
5
- const migrationAction_1 = require("../migrationAction");
6
- const graphcommerce6to7 = async (schema) => {
7
- if (!migrationAction_1.client) {
8
- return 0;
9
- }
5
+ const migrationActionFactory_1 = require("../migrationActionFactory");
6
+ const graphcommerce6to7 = async (schema, client) => {
7
+ const { migrationAction } = (0, migrationActionFactory_1.migrationActionFactory)(schema, client);
10
8
  // ? ENUMERATIONS
11
- (0, migrationAction_1.migrationAction)(schema, 'enumeration', 'create', {
9
+ migrationAction(schema, 'enumeration', 'create', {
12
10
  displayName: 'Row Column One Variants',
13
11
  apiId: 'RowColumnOneVariants',
14
12
  values: [
@@ -16,7 +14,7 @@ const graphcommerce6to7 = async (schema) => {
16
14
  { displayName: 'Message', apiId: 'Message' },
17
15
  ],
18
16
  });
19
- (0, migrationAction_1.migrationAction)(schema, 'enumeration', 'create', {
17
+ migrationAction(schema, 'enumeration', 'create', {
20
18
  displayName: 'Dynamic Row Condition Number Operator',
21
19
  apiId: 'DynamicRowConditionNumberOperator',
22
20
  values: [
@@ -25,7 +23,7 @@ const graphcommerce6to7 = async (schema) => {
25
23
  { displayName: 'Equal to', apiId: 'EQUAL' },
26
24
  ],
27
25
  });
28
- (0, migrationAction_1.migrationAction)(schema, 'enumeration', 'create', {
26
+ migrationAction(schema, 'enumeration', 'create', {
29
27
  displayName: 'Dynamic Row Placement',
30
28
  apiId: 'DynamicRowPlacement',
31
29
  values: [
@@ -35,36 +33,36 @@ const graphcommerce6to7 = async (schema) => {
35
33
  ],
36
34
  });
37
35
  // ? COMPONENTS
38
- (0, migrationAction_1.migrationAction)(schema, 'component', 'create', {
36
+ migrationAction(schema, 'component', 'create', {
39
37
  displayName: 'Text',
40
38
  apiId: 'ConditionText',
41
39
  apiIdPlural: 'ConditionTexts',
42
40
  });
43
- (0, migrationAction_1.migrationAction)(schema, 'component', 'create', {
41
+ migrationAction(schema, 'component', 'create', {
44
42
  displayName: 'Number',
45
43
  apiId: 'ConditionNumber',
46
44
  apiIdPlural: 'ConditionNumbers',
47
45
  });
48
- (0, migrationAction_1.migrationAction)(schema, 'component', 'create', {
46
+ migrationAction(schema, 'component', 'create', {
49
47
  displayName: 'AND',
50
48
  apiId: 'ConditionAnd',
51
49
  apiIdPlural: 'ConditionAnds',
52
50
  description: 'All of these conditions must match',
53
51
  });
54
- (0, migrationAction_1.migrationAction)(schema, 'component', 'create', {
52
+ migrationAction(schema, 'component', 'create', {
55
53
  displayName: 'OR',
56
54
  apiId: 'ConditionOr',
57
55
  apiIdPlural: 'ConditionOrs',
58
56
  description: 'One of these conditions must match',
59
57
  });
60
- (0, migrationAction_1.migrationAction)(schema, 'componentUnionField', 'create', {
58
+ migrationAction(schema, 'componentUnionField', 'create', {
61
59
  displayName: 'Conditions',
62
60
  apiId: 'conditions',
63
61
  parentApiId: 'ConditionAnd',
64
62
  componentApiIds: ['ConditionOr', 'ConditionText', 'ConditionNumber'],
65
63
  isList: true,
66
64
  }, 'ConditionAnd', 'component');
67
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
65
+ migrationAction(schema, 'simpleField', 'create', {
68
66
  displayName: 'Property',
69
67
  apiId: 'property',
70
68
  type: management_sdk_1.SimpleFieldType.String,
@@ -81,14 +79,14 @@ const graphcommerce6to7 = async (schema) => {
81
79
  },
82
80
  },
83
81
  }, 'ConditionText', 'component');
84
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
82
+ migrationAction(schema, 'simpleField', 'create', {
85
83
  displayName: 'Value',
86
84
  apiId: 'value',
87
85
  type: management_sdk_1.SimpleFieldType.String,
88
86
  parentApiId: 'ConditionText',
89
87
  isRequired: true,
90
88
  }, 'ConditionText', 'component');
91
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
89
+ migrationAction(schema, 'simpleField', 'create', {
92
90
  displayName: 'Property',
93
91
  apiId: 'property',
94
92
  type: management_sdk_1.SimpleFieldType.String,
@@ -104,14 +102,14 @@ const graphcommerce6to7 = async (schema) => {
104
102
  },
105
103
  },
106
104
  }, 'ConditionNumber', 'component');
107
- (0, migrationAction_1.migrationAction)(schema, 'enumerableField', 'create', {
105
+ migrationAction(schema, 'enumerableField', 'create', {
108
106
  displayName: 'Operator',
109
107
  apiId: 'operator',
110
108
  parentApiId: 'ConditionNumber',
111
109
  enumerationApiId: 'DynamicRowConditionNumberOperator',
112
110
  isRequired: true,
113
111
  }, 'ConditionNumber', 'component');
114
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
112
+ migrationAction(schema, 'simpleField', 'create', {
115
113
  displayName: 'Value',
116
114
  apiId: 'value',
117
115
  type: management_sdk_1.SimpleFieldType.Float,
@@ -119,13 +117,13 @@ const graphcommerce6to7 = async (schema) => {
119
117
  isRequired: true,
120
118
  }, 'ConditionNumber', 'component');
121
119
  // ? MODEL
122
- (0, migrationAction_1.migrationAction)(schema, 'model', 'create', {
120
+ migrationAction(schema, 'model', 'create', {
123
121
  displayName: 'Dynamic Row',
124
122
  apiId: 'DynamicRow',
125
123
  apiIdPlural: 'DynamicRows',
126
124
  description: 'Dynamic rows allow you to add specific Row models to pages based on the properties of the page',
127
125
  });
128
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
126
+ migrationAction(schema, 'simpleField', 'create', {
129
127
  displayName: 'Internal name',
130
128
  apiId: 'internalName',
131
129
  description: 'Only used for internal reference',
@@ -135,7 +133,7 @@ const graphcommerce6to7 = async (schema) => {
135
133
  isUnique: true,
136
134
  modelApiId: 'DynamicRow',
137
135
  }, 'DynamicRow', 'model');
138
- (0, migrationAction_1.migrationAction)(schema, 'unionField', 'create', {
136
+ migrationAction(schema, 'unionField', 'create', {
139
137
  displayName: 'Row',
140
138
  apiId: 'row',
141
139
  reverseField: {
@@ -147,7 +145,7 @@ const graphcommerce6to7 = async (schema) => {
147
145
  },
148
146
  parentApiId: 'DynamicRow',
149
147
  }, 'DynamicRow', 'model');
150
- (0, migrationAction_1.migrationAction)(schema, 'enumerableField', 'create', {
148
+ migrationAction(schema, 'enumerableField', 'create', {
151
149
  displayName: 'Placement',
152
150
  apiId: 'placement',
153
151
  parentApiId: 'DynamicRow',
@@ -155,7 +153,7 @@ const graphcommerce6to7 = async (schema) => {
155
153
  description: 'Where will the row be placed relative to the target',
156
154
  isRequired: true,
157
155
  }, 'DynamicRow', 'model');
158
- (0, migrationAction_1.migrationAction)(schema, 'unionField', 'create', {
156
+ migrationAction(schema, 'unionField', 'create', {
159
157
  displayName: 'Placement target',
160
158
  apiId: 'target',
161
159
  description: 'Optional: When the target is left blank it will place the Dynamic Row on the start or end.',
@@ -181,7 +179,7 @@ const graphcommerce6to7 = async (schema) => {
181
179
  },
182
180
  parentApiId: 'DynamicRow',
183
181
  }, 'DynamicRow', 'model');
184
- (0, migrationAction_1.migrationAction)(schema, 'componentUnionField', 'create', {
182
+ migrationAction(schema, 'componentUnionField', 'create', {
185
183
  displayName: 'Conditions (OR)',
186
184
  apiId: 'conditions',
187
185
  parentApiId: 'DynamicRow',
@@ -190,19 +188,19 @@ const graphcommerce6to7 = async (schema) => {
190
188
  isList: true,
191
189
  }, 'DynamicRow', 'model');
192
190
  // ? Row Column One
193
- (0, migrationAction_1.migrationAction)(schema, 'enumerableField', 'create', {
191
+ migrationAction(schema, 'enumerableField', 'create', {
194
192
  displayName: 'Variant',
195
193
  apiId: 'rowColumnOneVariant',
196
194
  enumerationApiId: 'RowColumnOneVariants',
197
195
  parentApiId: 'RowColumnOne',
198
196
  }, 'RowColumnOne', 'model');
199
- (0, migrationAction_1.migrationAction)(schema, 'componentUnionField', 'create', {
197
+ migrationAction(schema, 'componentUnionField', 'create', {
200
198
  displayName: 'Conditions',
201
199
  apiId: 'conditions',
202
200
  parentApiId: 'ConditionOr',
203
201
  componentApiIds: ['ConditionText', 'ConditionNumber'],
204
202
  isList: true,
205
203
  }, 'ConditionOr', 'component');
206
- return migrationAction_1.client.run(true);
204
+ return client.run(true);
207
205
  };
208
206
  exports.graphcommerce6to7 = graphcommerce6to7;
@@ -2,23 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.graphcommerce7to8 = void 0;
4
4
  const management_sdk_1 = require("@hygraph/management-sdk");
5
- const migrationAction_1 = require("../migrationAction");
6
- const graphcommerce7to8 = async (schema) => {
7
- if (!migrationAction_1.client) {
8
- return 0;
9
- }
5
+ const migrationActionFactory_1 = require("../migrationActionFactory");
6
+ const graphcommerce7to8 = async (schema, client) => {
7
+ const { migrationAction } = (0, migrationActionFactory_1.migrationActionFactory)(schema, client);
10
8
  const hasRow = schema.models
11
9
  .find((m) => m.apiId === 'DynamicRow')
12
10
  ?.fields.some((f) => f.apiId === 'row');
13
11
  if (hasRow) {
14
- (0, migrationAction_1.migrationAction)(schema, 'unionField', 'update', {
12
+ migrationAction(schema, 'unionField', 'update', {
15
13
  apiId: 'row',
16
14
  displayName: 'Row Deprecated',
17
15
  parentApiId: 'DynamicRow',
18
16
  description: 'This field is deprecated. Use Rows instead.',
19
17
  });
20
18
  }
21
- (0, migrationAction_1.migrationAction)(schema, 'unionField', 'create', {
19
+ migrationAction(schema, 'unionField', 'create', {
22
20
  displayName: 'Rows',
23
21
  apiId: 'rows',
24
22
  isList: true,
@@ -31,13 +29,13 @@ const graphcommerce7to8 = async (schema) => {
31
29
  },
32
30
  parentApiId: 'DynamicRow',
33
31
  }, 'DynamicRow', 'model');
34
- (0, migrationAction_1.migrationAction)(schema, 'componentUnionField', 'create', {
32
+ migrationAction(schema, 'componentUnionField', 'create', {
35
33
  displayName: 'Conditions',
36
34
  apiId: 'conditions',
37
35
  parentApiId: 'ConditionOr',
38
36
  componentApiIds: ['ConditionText', 'ConditionNumber'],
39
37
  isList: true,
40
38
  }, 'ConditionOr', 'component');
41
- return migrationAction_1.client.run(true);
39
+ return client.run(true);
42
40
  };
43
41
  exports.graphcommerce7to8 = graphcommerce7to8;
@@ -1,22 +1,68 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.graphcommerce8to9 = void 0;
4
- const migrationAction_1 = require("../migrationAction");
5
- const graphcommerce8to9 = async (schema) => {
6
- if (!migrationAction_1.client) {
7
- return 0;
8
- }
9
- // This migration is for GC 8.0 and is not yet exported as a usable migration.
4
+ const management_sdk_1 = require("@hygraph/management-sdk");
5
+ const migrationActionFactory_1 = require("../migrationActionFactory");
6
+ const graphcommerce8to9 = async (schema, client) => {
7
+ const { migrationAction } = (0, migrationActionFactory_1.migrationActionFactory)(schema, client);
10
8
  // Removes the deprecated 'Row' field which was deprecated in GC@7.1
11
9
  const hasRow = schema.models
12
10
  .find((m) => m.apiId === 'DynamicRow')
13
11
  ?.fields.some((f) => f.apiId === 'row');
14
12
  if (hasRow) {
15
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'delete', {
13
+ migrationAction(schema, 'simpleField', 'delete', {
16
14
  apiId: 'row',
17
15
  parentApiId: 'DynamicRow',
18
16
  });
19
17
  }
20
- return migrationAction_1.client.run(true);
18
+ const hasRowCategory = schema.models.some((m) => m.apiId === 'RowCategory');
19
+ //
20
+ if (!hasRowCategory) {
21
+ migrationAction(schema, 'model', 'create', {
22
+ apiId: 'RowCategory',
23
+ displayName: 'Row Category',
24
+ apiIdPlural: 'RowProductLists',
25
+ description: 'A model that displays a category',
26
+ });
27
+ migrationAction(schema, 'simpleField', 'create', {
28
+ position: 1,
29
+ type: management_sdk_1.SimpleFieldType.String,
30
+ formConfig: { renderer: 'GCMS_SLUG', config: { isLowercase: true } },
31
+ validations: {
32
+ String: {
33
+ matches: {
34
+ regex: '^[a-z0-9]+(?:[-/][a-z0-9]+)*$',
35
+ errorMessage: 'The category URL must be a valid slug',
36
+ },
37
+ },
38
+ },
39
+ parentApiId: 'RowCategory',
40
+ displayName: 'Category URL',
41
+ apiId: 'categoryUrl',
42
+ description: 'The URL of the category, may include slashes',
43
+ isTitle: true,
44
+ isLocalized: true,
45
+ isRequired: true,
46
+ visibility: management_sdk_1.VisibilityTypes.ReadWrite,
47
+ }, 'RowCategory', 'model');
48
+ migrationAction(schema, 'enumeration', 'create', {
49
+ displayName: 'Row Category Variant',
50
+ apiId: 'RowCategoryVariant',
51
+ values: [
52
+ { displayName: 'Backstory', apiId: 'Backstory' },
53
+ { displayName: 'Grid', apiId: 'Grid' },
54
+ { displayName: 'Swipeable', apiId: 'Swipeable' },
55
+ ],
56
+ });
57
+ migrationAction(schema, 'enumerableField', 'create', {
58
+ displayName: 'Variant',
59
+ apiId: 'variant',
60
+ parentApiId: 'RowCategory',
61
+ enumerationApiId: 'RowCategoryVariant',
62
+ description: 'As what variant wil the RowCategory be displayed',
63
+ isRequired: true,
64
+ }, 'RowCategory', 'model');
65
+ }
66
+ return client.run(true);
21
67
  };
22
68
  exports.graphcommerce8to9 = graphcommerce8to9;
@@ -2,33 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readSchema = void 0;
4
4
  const client_1 = require("@apollo/client");
5
- const fetch_1 = require("@whatwg-node/fetch");
6
- const readSchema = async (config) => {
7
- if (!config.hygraphProjectId) {
8
- throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file.');
9
- }
10
- if (!config.hygraphWriteAccessToken) {
11
- throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file.');
12
- }
13
- const projectId = config.hygraphProjectId;
14
- if (!config.hygraphManagementApi) {
15
- throw new Error('Please provide GC_HYGRAPH_MANAGEMENT_API in your env file.');
16
- }
17
- const hygraphClient = new client_1.ApolloClient({
18
- link: new client_1.HttpLink({
19
- uri: config.hygraphManagementApi,
20
- fetch: fetch_1.fetch,
21
- headers: { Authorization: `Bearer ${config.hygraphWriteAccessToken}` },
22
- }),
23
- cache: new client_1.InMemoryCache(),
24
- });
25
- const { data } = await hygraphClient.query({
5
+ const readSchema = async (managementClient, projectId) => {
6
+ const { data } = await managementClient.query({
26
7
  query: (0, client_1.gql) `
27
8
  query getSchema($projectId: ID!) {
28
9
  viewer {
29
10
  project(id: $projectId) {
30
11
  environment(name: "master") {
31
12
  contentModel {
13
+ locales {
14
+ id
15
+ apiId
16
+ }
17
+ stages {
18
+ id
19
+ apiId
20
+ }
32
21
  models {
33
22
  apiId
34
23
  apiIdPlural
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConfig = getConfig;
4
+ function getConfig(config) {
5
+ let { hygraphProjectId: projectId, hygraphWriteAccessToken: authToken, hygraphManagementApi: uri, hygraphEndpoint, } = config;
6
+ if (!authToken) {
7
+ throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file.');
8
+ }
9
+ if (!projectId) {
10
+ projectId = new URL(hygraphEndpoint).pathname.split('/')?.[1];
11
+ }
12
+ if (!uri) {
13
+ const endpoint = new URL(hygraphEndpoint);
14
+ endpoint.hostname = `management-${endpoint.hostname}`.replace('.cdn', '');
15
+ endpoint.pathname = 'graphql';
16
+ uri = endpoint.toString();
17
+ }
18
+ return { projectId, authToken, uri };
19
+ }
@@ -0,0 +1,43 @@
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.getEnvironment = getEnvironment;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ async function getEnvironment(client, config) {
9
+ const endpoints = await client.query({
10
+ query: (0, graphql_tag_1.default) `
11
+ query Environments($projectId: ID!) {
12
+ viewer {
13
+ id
14
+ project(id: $projectId) {
15
+ environments {
16
+ name
17
+ endpoint
18
+ migrations {
19
+ name
20
+ status
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+ `,
27
+ variables: { projectId: config.projectId },
28
+ errorPolicy: 'all',
29
+ });
30
+ if (endpoints.errors) {
31
+ const isBadInput = endpoints.errors.some((e) => e.extensions?.code === 'BAD_USER_INPUT');
32
+ if (isBadInput) {
33
+ throw Error(`
34
+ Could not find environment for projectId ${config.projectId}.
35
+ Please check your GC_HYGRAPH_PROJECT_ID in your env file.
36
+ `);
37
+ }
38
+ throw new Error(`An error occurred: ${endpoints.errors.map((e) => e.message).join('\n')}`);
39
+ }
40
+ const environment = endpoints.data.viewer.project.environments.find((env) => env.name === 'master') ??
41
+ endpoints.data.viewer.project.environments?.[0];
42
+ return environment;
43
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getManagementClient = getManagementClient;
4
+ const client_1 = require("@apollo/client");
5
+ function getManagementClient(config) {
6
+ const { authToken: accessToken, uri } = config;
7
+ return new client_1.ApolloClient({
8
+ link: new client_1.HttpLink({
9
+ uri,
10
+ fetch,
11
+ headers: { Authorization: `Bearer ${accessToken}` },
12
+ }),
13
+ cache: new client_1.InMemoryCache(),
14
+ });
15
+ }
@@ -10,6 +10,6 @@ const graphcommerceLog = (message, type) => {
10
10
  info: '\x1b[36m\x1b[1m%s\x1b[0m',
11
11
  };
12
12
  // eslint-disable-next-line no-console
13
- console.log(type ? color[type] : '', `[GraphCommerce]: ${message}`);
13
+ console.log(type ? color[type] : '', `${message}`);
14
14
  };
15
15
  exports.graphcommerceLog = graphcommerceLog;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/hygraph-cli",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.9",
5
+ "version": "9.0.0-canary.101",
6
6
  "scripts": {
7
7
  "dev": "tsc --preserveWatchOutput --watch",
8
8
  "build": "tsc",
@@ -12,23 +12,23 @@
12
12
  "main": "dist/index.js",
13
13
  "types": "src/index.ts",
14
14
  "dependencies": {
15
- "@hygraph/management-sdk": "1.2.4",
16
- "@whatwg-node/fetch": "^0.9.15",
15
+ "@hygraph/management-sdk": "1.2.5",
16
+ "@whatwg-node/fetch": "^0.9.21",
17
17
  "graphql-tag": "^2.12.6",
18
18
  "prompts": "^2.4.2"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "@apollo/client": "^3",
22
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.9",
23
- "@graphcommerce/next-config": "^8.1.0-canary.9",
24
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.9",
25
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.9",
22
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.101",
23
+ "@graphcommerce/next-config": "^9.0.0-canary.101",
24
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.101",
25
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.101",
26
26
  "dotenv": "^16.1.4",
27
27
  "graphql": "^16.7.1"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/prompts": "^2.4.9",
31
- "typescript": "5.3.3"
31
+ "typescript": "5.6.2"
32
32
  },
33
33
  "sideEffects": false,
34
34
  "prettier": "@graphcommerce/prettier-config-pwa",
@@ -0,0 +1,99 @@
1
+ import {
2
+ BatchMigrationCreateComponentFieldInput,
3
+ BatchMigrationCreateComponentInput,
4
+ BatchMigrationCreateComponentUnionFieldInput,
5
+ BatchMigrationCreateEnumerableFieldInput,
6
+ BatchMigrationCreateEnumerationInput,
7
+ BatchMigrationCreateLocaleInput,
8
+ BatchMigrationCreateModelInput,
9
+ BatchMigrationCreateRelationalFieldInput,
10
+ BatchMigrationCreateSimpleFieldInput,
11
+ BatchMigrationCreateStageInput,
12
+ BatchMigrationCreateUnionFieldInput,
13
+ Client,
14
+ } from '@hygraph/management-sdk'
15
+ import { Schema } from './types'
16
+
17
+ interface MigrationParams {
18
+ name?: string
19
+ endpoint: string
20
+ authToken: string
21
+ }
22
+
23
+ export class UpsertClient extends Client {
24
+ constructor(
25
+ params: MigrationParams,
26
+ private schema: Schema,
27
+ ) {
28
+ super(params)
29
+ }
30
+
31
+ upsertModel(data: BatchMigrationCreateModelInput) {
32
+ const exists = this.schema.models.some((m) => m.apiId === data.apiId)
33
+ return exists ? this.createModel(data) : this.updateModel(data)
34
+ }
35
+
36
+ upsertComponent(data: BatchMigrationCreateComponentInput) {
37
+ const exists = this.schema.models.some((m) => m.apiId === data.apiId)
38
+ return exists ? this.createComponent(data) : this.updateComponent(data)
39
+ }
40
+
41
+ upsertSimpleField(data: BatchMigrationCreateSimpleFieldInput) {
42
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
43
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
44
+ return exists
45
+ ? this.createSimpleField(data)
46
+ : this.updateSimpleField({ ...data, embeddableModels: undefined })
47
+ }
48
+
49
+ // upsertRemoteField(data: BatchMigrationCreateRemoteFieldInput) {
50
+ // const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
51
+ // const exists = model?.fields.some((f) => f.apiId === data.apiId)
52
+ // return exists ? this.createRemoteField(data) : this.updateRemoteField(data)
53
+ // }
54
+
55
+ upsertRelationalField(data: BatchMigrationCreateRelationalFieldInput) {
56
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
57
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
58
+ return exists ? this.createRelationalField(data) : this.updateRelationalField(data)
59
+ }
60
+
61
+ upsertUnionField(data: BatchMigrationCreateUnionFieldInput) {
62
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
63
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
64
+ return exists ? this.createUnionField(data) : this.updateUnionField(data)
65
+ }
66
+
67
+ upsertComponentField(data: BatchMigrationCreateComponentFieldInput) {
68
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
69
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
70
+ return exists ? this.createComponentField(data) : this.updateComponentField(data)
71
+ }
72
+
73
+ upsertComponentUnionField(data: BatchMigrationCreateComponentUnionFieldInput) {
74
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
75
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
76
+ return exists ? this.createComponentUnionField(data) : this.updateComponentUnionField(data)
77
+ }
78
+
79
+ upsertEnumeration(data: BatchMigrationCreateEnumerationInput) {
80
+ const exists = this.schema.enumerations.some((e) => e.apiId === data.apiId)
81
+ return exists ? this.createEnumeration(data) : this.updateEnumeration(data)
82
+ }
83
+
84
+ upsertEnumerableField(data: BatchMigrationCreateEnumerableFieldInput) {
85
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
86
+ const exists = model?.fields.some((f) => f.apiId === data.apiId)
87
+ return exists ? this.createEnumerableField(data) : this.updateEnumerableField(data)
88
+ }
89
+
90
+ upsertStage(data: BatchMigrationCreateStageInput) {
91
+ const exists = this.schema.stages.some((m) => m.apiId === data.apiId)
92
+ return exists ? this.createStage(data) : this.updateStage(data)
93
+ }
94
+
95
+ upsertLocale(data: BatchMigrationCreateLocaleInput) {
96
+ const exists = this.schema.locales.some((m) => m.apiId === data.apiId)
97
+ return exists ? this.createLocale(data) : this.updateLocale(data)
98
+ }
99
+ }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { migrateHygraph } from './migrateHygraph'
1
+ export { migrateHygraphCli as migrateHygraph } from './migrateHygraphCli'