@graphcommerce/hygraph-cli 7.0.0-canary.21 → 7.0.0

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.
@@ -0,0 +1,61 @@
1
+ import { ApolloClient, InMemoryCache, gql, HttpLink } from '@apollo/client'
2
+ import { GraphCommerceConfig } from '@graphcommerce/next-config'
3
+ import { fetch } from '@whatwg-node/fetch'
4
+
5
+ export const readSchema = async (config: GraphCommerceConfig) => {
6
+ if (!config.hygraphProjectId) {
7
+ throw new Error('Please provide GC_HYGRAPH_PROJECT_ID in your env file')
8
+ }
9
+
10
+ if (!config.hygraphWriteAccessToken) {
11
+ throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file')
12
+ }
13
+
14
+ const projectId = config.hygraphProjectId
15
+
16
+ const hygraphClient = new ApolloClient({
17
+ link: new HttpLink({
18
+ uri: 'https://management.hygraph.com/graphql',
19
+ fetch,
20
+ headers: { Authorization: `Bearer ${config.hygraphWriteAccessToken}` },
21
+ }),
22
+ cache: new InMemoryCache(),
23
+ })
24
+
25
+ const { data } = await hygraphClient.query({
26
+ query: gql`
27
+ query getSchema($projectId: ID!) {
28
+ viewer {
29
+ project(id: $projectId) {
30
+ environment(name: "master") {
31
+ contentModel {
32
+ models {
33
+ apiId
34
+ apiIdPlural
35
+ fields {
36
+ apiId
37
+ }
38
+ }
39
+ components {
40
+ apiId
41
+ apiIdPlural
42
+ fields {
43
+ apiId
44
+ }
45
+ }
46
+ enumerations {
47
+ apiId
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ `,
55
+ variables: {
56
+ projectId,
57
+ },
58
+ })
59
+
60
+ return data
61
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  // eslint-disable-next-line import/no-extraneous-dependencies
2
- import { MigrationInfo } from '@hygraph/management-sdk/dist/ManagementAPIClient'
2
+ import type { Model, Component, Enumeration } from '@hygraph/management-sdk'
3
+ import type { MigrationInfo } from '@hygraph/management-sdk/dist/src/ManagementAPIClient'
3
4
 
4
5
  export type Migration = (name: string | undefined) => Promise<MigrationInfo>
6
+
7
+ export type Schema = {
8
+ models: Model[]
9
+ components: Component[]
10
+ enumerations: Enumeration[]
11
+ }
@@ -1,229 +0,0 @@
1
- import { loadConfig } from '@graphcommerce/next-config'
2
- import { Client, SimpleFieldType, VisibilityTypes } from '@hygraph/management-sdk'
3
- import dotenv from 'dotenv'
4
-
5
- dotenv.config()
6
-
7
- export const dynamicRow = async (name: string | undefined) => {
8
- const config = loadConfig(process.cwd())
9
-
10
- if (!config.hygraphWriteAccessEndpoint) {
11
- throw new Error(
12
- 'Please provide hygraphWriteAccessEndpoint in your config or GC_HYGRAPH_WRITE_ACCESS_ENDPOINT in your env',
13
- )
14
- }
15
- if (!config.hygraphWriteAccessToken) {
16
- throw new Error('Please provide GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env')
17
- }
18
-
19
- const client = new Client({
20
- authToken: config.hygraphWriteAccessToken,
21
- endpoint: config.hygraphWriteAccessEndpoint,
22
- name,
23
- })
24
-
25
- // ? ENUMERATIONS
26
- client.createEnumeration({
27
- displayName: 'Dynamic Row Condition Number Operator',
28
- apiId: 'DynamicRowConditionNumberOperator',
29
- values: [
30
- { displayName: 'Greater than or equal to', apiId: 'GTE' },
31
- { displayName: 'Less than or equal to', apiId: 'LTE' },
32
- { displayName: 'Equal to', apiId: 'EQUAL' },
33
- ],
34
- })
35
-
36
- client.createEnumeration({
37
- displayName: 'Dynamic Row Placement',
38
- apiId: 'DynamicRowPlacement',
39
- values: [
40
- { displayName: 'Before', apiId: 'BEFORE' },
41
- { displayName: 'After', apiId: 'AFTER' },
42
- { displayName: 'Replace', apiId: 'REPLACE' },
43
- ],
44
- })
45
-
46
- // ? COMPONENTS
47
- client.createComponent({
48
- displayName: 'Text',
49
- apiId: 'ConditionText',
50
- apiIdPlural: 'ConditionTexts',
51
- })
52
-
53
- client.createComponent({
54
- displayName: 'Number',
55
- apiId: 'ConditionNumber',
56
- apiIdPlural: 'ConditionNumbers',
57
- })
58
-
59
- client.createComponent({
60
- displayName: 'AND',
61
- apiId: 'ConditionAnd',
62
- apiIdPlural: 'ConditionAnds',
63
- description: 'All of these conditions must match',
64
- })
65
-
66
- client.createComponent({
67
- displayName: 'OR',
68
- apiId: 'ConditionOr',
69
- apiIdPlural: 'ConditionOrs',
70
- description: 'One of these conditions must match',
71
- })
72
-
73
- client.createComponentUnionField({
74
- displayName: 'Conditions',
75
- apiId: 'conditions',
76
- parentApiId: 'ConditionAnd',
77
- componentApiIds: ['ConditionOr', 'ConditionText', 'ConditionNumber'],
78
- isList: true,
79
- })
80
-
81
- client.createComponentUnionField({
82
- displayName: 'Conditions',
83
- apiId: 'conditions',
84
- parentApiId: 'ConditionOr',
85
- componentApiIds: ['ConditionText', 'ConditionNumber'],
86
- isList: true,
87
- })
88
-
89
- client.createSimpleField({
90
- displayName: 'Property',
91
- apiId: 'property',
92
- type: SimpleFieldType.String,
93
- parentApiId: 'ConditionText',
94
- description:
95
- 'Path to the value of the object being evaluated.\n\nFor products: url_key, category, sku',
96
- isRequired: true,
97
- validations: {
98
- String: {
99
- matches: {
100
- flags: ['i', 's'],
101
- regex: '^[a-z0-9-_.]+$',
102
- errorMessage: 'Only letters, numbers, dashes (-), underscores (_) or dots allowed (.)',
103
- },
104
- },
105
- },
106
- })
107
-
108
- client.createSimpleField({
109
- displayName: 'Value',
110
- apiId: 'value',
111
- type: SimpleFieldType.String,
112
- parentApiId: 'ConditionText',
113
- isRequired: true,
114
- })
115
-
116
- client.createSimpleField({
117
- displayName: 'Property',
118
- apiId: 'property',
119
- type: SimpleFieldType.String,
120
- parentApiId: 'ConditionNumber',
121
- isRequired: true,
122
- validations: {
123
- String: {
124
- matches: {
125
- flags: ['i', 's'],
126
- regex: '^[a-z0-9-_.]+$',
127
- errorMessage: 'Only letters, numbers, dashes (-), underscores (_) or dots allowed (.)',
128
- },
129
- },
130
- },
131
- })
132
-
133
- client.createEnumerableField({
134
- displayName: 'Operator',
135
- apiId: 'operator',
136
- parentApiId: 'ConditionNumber',
137
- enumerationApiId: 'DynamicRowConditionNumberOperator',
138
- isRequired: true,
139
- })
140
-
141
- client.createSimpleField({
142
- displayName: 'Value',
143
- apiId: 'value',
144
- type: SimpleFieldType.Float,
145
- parentApiId: 'ConditionNumber',
146
- isRequired: true,
147
- })
148
-
149
- // ? MODEL
150
- client.createModel({
151
- apiId: 'DynamicRow',
152
- apiIdPlural: 'DynamicRows',
153
- displayName: 'Dynamic Row',
154
- description:
155
- 'Dynamic rows allow you to add specific Row models to pages based on the properties of the page',
156
- })
157
-
158
- client.createSimpleField({
159
- displayName: 'Internal name',
160
- apiId: 'internalName',
161
- description: 'Only used for internal reference',
162
- type: SimpleFieldType.String,
163
- isTitle: true,
164
- isRequired: true,
165
- isUnique: true,
166
- modelApiId: 'DynamicRow',
167
- })
168
-
169
- client.createUnionField({
170
- displayName: 'Row',
171
- apiId: 'row',
172
- reverseField: {
173
- modelApiIds: ['RowQuote', 'RowLinks', 'RowColumnOne'],
174
- apiId: 'dynamicRow',
175
- displayName: 'DynamicRows',
176
- visibility: VisibilityTypes.Hidden,
177
- isList: true,
178
- },
179
- parentApiId: 'DynamicRow',
180
- })
181
-
182
- client.createEnumerableField({
183
- displayName: 'Placement',
184
- apiId: 'placement',
185
- parentApiId: 'DynamicRow',
186
- enumerationApiId: 'DynamicRowPlacement',
187
- description: 'Where will the row be placed relative to the target',
188
- isRequired: true,
189
- })
190
-
191
- client.createUnionField({
192
- displayName: 'Placement target',
193
- apiId: 'target',
194
- description:
195
- 'Optional: When the target is left blank it will place the Dynamic Row on the start or end.',
196
- reverseField: {
197
- modelApiIds: [
198
- 'RowQuote',
199
- 'RowLinks',
200
- 'RowColumnOne',
201
- 'RowColumnTwo',
202
- 'RowColumnThree',
203
- 'RowServiceOptions',
204
- 'RowContentLinks',
205
- 'RowButtonLinkList',
206
- 'RowProduct',
207
- 'RowSpecialBanner',
208
- 'RowHeroBanner',
209
- 'RowBlogContent',
210
- ],
211
- apiId: 'dynamicRowsTarget',
212
- displayName: 'DynamicRowsTarget',
213
- visibility: VisibilityTypes.Hidden,
214
- isList: true,
215
- },
216
- parentApiId: 'DynamicRow',
217
- })
218
-
219
- client.createComponentUnionField({
220
- displayName: 'Conditions (OR)',
221
- apiId: 'conditions',
222
- parentApiId: 'DynamicRow',
223
- description: 'One of these conditions must match',
224
- componentApiIds: ['ConditionAnd', 'ConditionText', 'ConditionNumber'],
225
- isList: true,
226
- })
227
-
228
- return client.run(true)
229
- }