@@ -8,21 +8,35 @@ import {
8
8
ParsedAppSyncModelConfig ,
9
9
RawAppSyncModelConfig ,
10
10
CodeGenEnum ,
11
+ CodeGenUnion ,
12
+ CodeGenInterface ,
11
13
} from './appsync-visitor' ;
12
14
import { METADATA_SCALAR_MAP } from '../scalars' ;
13
15
export type JSONSchema = {
14
16
models : JSONSchemaModels ;
15
17
enums : JSONSchemaEnums ;
16
18
nonModels : JSONSchemaTypes ;
19
+ interfaces : JSONSchemaInterfaces ;
20
+ unions : JSONSchemaUnions ;
17
21
version : string ;
18
22
codegenVersion : string ;
19
23
} ;
20
24
export type JSONSchemaModels = Record < string , JSONSchemaModel > ;
21
25
export type JSONSchemaTypes = Record < string , JSONSchemaNonModel > ;
26
+ export type JSONSchemaInterfaces = Record < string , JSONSchemaInterface > ;
27
+ export type JSONSchemaUnions = Record < string , JSONSchemaUnion > ;
22
28
export type JSONSchemaNonModel = {
23
29
name : string ;
24
30
fields : JSONModelFields ;
25
31
} ;
32
+ export type JSONSchemaInterface = {
33
+ name : string ;
34
+ fields : JSONModelFields ;
35
+ } ;
36
+ export type JSONSchemaUnion = {
37
+ name : string ;
38
+ types : JSONModelFieldType [ ] ;
39
+ } ;
26
40
type JSONSchemaModel = {
27
41
name : string ;
28
42
attributes ?: JSONModelAttributes ;
@@ -58,7 +72,7 @@ type AssociationBelongsTo = AssociationBaseType & {
58
72
59
73
type AssociationType = AssociationHasMany | AssociationHasOne | AssociationBelongsTo ;
60
74
61
- type JSONModelFieldType = keyof typeof METADATA_SCALAR_MAP | { model : string } | { enum : string } | { nonModel : string } ;
75
+ type JSONModelFieldType = keyof typeof METADATA_SCALAR_MAP | { model : string } | { enum : string } | { nonModel : string } | { interface : string } | { union : string } ;
62
76
type JSONModelField = {
63
77
name : string ;
64
78
type : JSONModelFieldType ;
@@ -160,6 +174,8 @@ export class AppSyncJSONVisitor<
160
174
models : { } ,
161
175
enums : { } ,
162
176
nonModels : { } ,
177
+ interfaces : { } ,
178
+ unions : { } ,
163
179
// This is hard-coded for the schema version purpose instead of codegen version
164
180
// To avoid the failure of validation method checkCodegenSchema in JS Datastore
165
181
// The hard code is starting from amplify codegen major version 4
@@ -175,11 +191,19 @@ export class AppSyncJSONVisitor<
175
191
return { ...acc , [ nonModel . name ] : this . generateNonModelMetadata ( nonModel ) } ;
176
192
} , { } ) ;
177
193
194
+ const interfaces = Object . values ( this . getSelectedInterfaces ( ) ) . reduce ( ( acc , codegenInterface : CodeGenInterface ) => {
195
+ return { ...acc , [ codegenInterface . name ] : this . generateInterfaceMetadata ( codegenInterface ) } ;
196
+ } , { } ) ;
197
+
198
+ const unions = Object . values ( this . getSelectedUnions ( ) ) . reduce ( ( acc , union : CodeGenUnion ) => {
199
+ return { ...acc , [ union . name ] : this . generateUnionMetadata ( union ) } ;
200
+ } , { } ) ;
201
+
178
202
const enums = Object . values ( this . enumMap ) . reduce ( ( acc , enumObj ) => {
179
203
const enumV = this . generateEnumMetadata ( enumObj ) ;
180
204
return { ...acc , [ this . getEnumName ( enumObj ) ] : enumV } ;
181
205
} , { } ) ;
182
- return { ...result , models, nonModels : nonModels , enums } ;
206
+ return { ...result , models, nonModels : nonModels , enums, interfaces , unions } ;
183
207
}
184
208
185
209
private getFieldAssociation ( field : CodeGenField ) : AssociationType | void {
@@ -229,39 +253,58 @@ export class AppSyncJSONVisitor<
229
253
private generateNonModelMetadata ( nonModel : CodeGenModel ) : JSONSchemaNonModel {
230
254
return {
231
255
name : this . getModelName ( nonModel ) ,
232
- fields : nonModel . fields . reduce ( ( acc : JSONModelFields , field : CodeGenField ) => {
233
- const fieldMeta : JSONModelField = {
234
- name : this . getFieldName ( field ) ,
235
- isArray : field . isList ,
236
- type : this . getType ( field . type ) ,
237
- isRequired : ! field . isNullable ,
238
- attributes : [ ] ,
239
- } ;
240
-
241
- if ( field . isListNullable !== undefined ) {
242
- fieldMeta . isArrayNullable = field . isListNullable ;
243
- }
256
+ fields : this . generateFieldsMetadata ( nonModel . fields )
257
+ } ;
258
+ }
244
259
245
- if ( field . isReadOnly !== undefined ) {
246
- fieldMeta . isReadOnly = field . isReadOnly ;
247
- }
260
+ private generateInterfaceMetadata ( codeGenInterface : CodeGenInterface ) : JSONSchemaInterface {
261
+ return {
262
+ name : codeGenInterface . name ,
263
+ fields : this . generateFieldsMetadata ( codeGenInterface . fields ) ,
264
+ } ;
265
+ }
248
266
249
- const association : AssociationType | void = this . getFieldAssociation ( field ) ;
250
- if ( association ) {
251
- fieldMeta . association = association ;
252
- }
253
- acc [ fieldMeta . name ] = fieldMeta ;
254
- return acc ;
255
- } , { } ) ,
267
+ private generateUnionMetadata ( codeGenUnion : CodeGenUnion ) : JSONSchemaUnion {
268
+ return {
269
+ name : codeGenUnion . name ,
270
+ types : codeGenUnion . typeNames . map ( t => this . getType ( t ) )
256
271
} ;
257
272
}
273
+
258
274
private generateEnumMetadata ( enumObj : CodeGenEnum ) : JSONSchemaEnum {
259
275
return {
260
276
name : enumObj . name ,
261
277
values : Object . values ( enumObj . values ) ,
262
278
} ;
263
279
}
264
280
281
+ private generateFieldsMetadata ( fields : CodeGenField [ ] ) : JSONModelFields {
282
+ return fields . reduce ( ( acc : JSONModelFields , field : CodeGenField ) => {
283
+ const fieldMeta : JSONModelField = {
284
+ name : this . getFieldName ( field ) ,
285
+ isArray : field . isList ,
286
+ type : this . getType ( field . type ) ,
287
+ isRequired : ! field . isNullable ,
288
+ attributes : [ ] ,
289
+ } ;
290
+
291
+ if ( field . isListNullable !== undefined ) {
292
+ fieldMeta . isArrayNullable = field . isListNullable ;
293
+ }
294
+
295
+ if ( field . isReadOnly !== undefined ) {
296
+ fieldMeta . isReadOnly = field . isReadOnly ;
297
+ }
298
+
299
+ const association : AssociationType | void = this . getFieldAssociation ( field ) ;
300
+ if ( association ) {
301
+ fieldMeta . association = association ;
302
+ }
303
+ acc [ fieldMeta . name ] = fieldMeta ;
304
+ return acc ;
305
+ } , { } )
306
+ }
307
+
265
308
private getType ( gqlType : string ) : JSONModelFieldType {
266
309
// Todo: Handle unlisted scalars
267
310
if ( gqlType in METADATA_SCALAR_MAP ) {
@@ -273,6 +316,12 @@ export class AppSyncJSONVisitor<
273
316
if ( gqlType in this . nonModelMap ) {
274
317
return { nonModel : gqlType } ;
275
318
}
319
+ if ( gqlType in this . interfaceMap ) {
320
+ return { interface : this . interfaceMap [ gqlType ] . name } ;
321
+ }
322
+ if ( gqlType in this . unionMap ) {
323
+ return { union : this . unionMap [ gqlType ] . name } ;
324
+ }
276
325
if ( gqlType in this . modelMap ) {
277
326
return { model : gqlType } ;
278
327
}
0 commit comments