import { DEFAULT_SCALARS , NormalizedScalarsMap } from '@graphql-codegen/visitor-plugin-common' ; import { GraphQLSchema } from 'graphql' ; import { AppSyncModelTypeScriptVisitor } from './appsync-typescript-visitor' ; import { CodeGenEnum , CodeGenModel , ParsedAppSyncModelConfig , RawAppSyncModelConfig } from './appsync-visitor' ; export interface RawAppSyncModelJavaScriptConfig extends RawAppSyncModelConfig { name type boolean description example isDeclaration ? : boolean ; } export interface ParsedAppSyncModelJavaScriptConfig extends ParsedAppSyncModelConfig { isDeclaration : boolean ; } export class AppSyncModelJavascriptVisitor < TRawConfig extends RawAppSyncModelJavaScriptConfig = RawAppSyncModelJavaScriptConfig , TPluginConfig extends ParsedAppSyncModelJavaScriptConfig = ParsedAppSyncModelJavaScriptConfig > extends AppSyncModelTypeScriptVisitor < TRawConfig , TPluginConfig > { protected IMPORT_STATEMENTS = [ 'import { ModelInit, MutableModel, PersistentModelConstructor } from "@aws-amplify/datastore";' ] ; constructor ( schema , rawConfig , additionalConfig , defaultScalars = DEFAULT_SCALARS ) { super ( schema , rawConfig , additionalConfig , defaultScalars ) ; this . _parsedConfig . isDeclaration = rawConfig . isDeclaration || false ; } generate ( ) { const shouldUseModelNameFieldInHasManyAndBelongsTo = false ; this . processDirectives ( shouldUseModelNameFieldInHasManyAndBelongsTo ) ; if ( this . _parsedConfig . isDeclaration ) { const imports = this . generateImports ( ) ; const enumDeclarations = Object . values ( this . enumMap ) . map ( enumObj => this . generateEnumDeclarations ( enumObj , true ) ) . join ( '\n\n' ) ; const modelDeclarations = Object . values ( this . modelMap ) . map ( typeObj => this . generateModelDeclaration ( typeObj , true ) ) . join ( '\n\n' ) ; const nonModelDeclarations = Object . values ( this . nonModelMap ) . map ( typeObj => this . generateModelDeclaration ( typeObj , true ) ) . join ( '\n\n' ) ; if ( ! this . config . isTimestampFieldsAdded ) { return [ imports , enumDeclarations , nonModelDeclarations , modelDeclarations ] . join ( '\n\n' ) ; } else { const modelMetaData = Object . values ( this . modelMap ) . map ( typeObj => this . generateModelMetaData ( typeObj ) ) . join ( '\n\n' ) ; return [ imports , enumDeclarations , nonModelDeclarations , modelMetaData , modelDeclarations ] . join ( '\n\n' ) ; } } else { const imports = this . generateImportsJavaScriptImplementation ( ) ; const enumDeclarations = Object . values ( this . enumMap ) . map ( ( e ) => this . generateEnumObject ( e ) ) . join ( '\n\n' ) ; const modelInitialization = this . generateModelInitialization ( [ ... Object . values ( this . modelMap ) , ... Object . values ( this . nonModelMap ) ] , false ) ; const modelExports = this . generateExports ( [ ... Object . values ( this . modelMap ) , ... Object . values ( this . enumMap ) , ... Object . values ( this . nonModelMap ) , ] ) ; return [ imports , enumDeclarations , modelInitialization , modelExports ] . join ( '\n\n' ) ; } } param enumObj param exportEnum protected generateEnumObject ( enumObj , exportEnum = false ) { const enumName = this . getEnumName ( enumObj ) ; const header = [ exportEnum ? 'export' : null , 'const' , enumName ] . filter ( h => h ) . join ( ' ' ) ; return `${ header } = ${ JSON . stringify ( enumObj . values , null , 2 ) };` ; } /**
     * Generate import statements to be used in the JavaScript model file
     */ protected generateImportsJavaScriptImplementation ( ) { return [ '// @ts-check' , "import { initSchema } from '@aws-amplify/datastore';" , "import { schema } from './schema';" ] . join ( '\n' ) ; } protected generateModelTypeDeclarationName ( model ) { return `${ this . getModelName ( model ) }` ; } }