import { GraphQLSchema , GraphQLEnumType } from 'graphql' ; import { PluginFunction , Types } from '@graphql-codegen/plugin-helpers' ; import { EnumArrayPluginConfig } from './config' ; function getEnumTypeMap ( schema ) { const typeMap = schema . getTypeMap ( ) ; const result = [ ] ; for ( const key in typeMap ) { if ( typeMap [ key ] . astNode ?. kind === 'EnumTypeDefinition' ) { result . push ( typeMap [ key ] as GraphQLEnumType ) ; } } return result ; } function buildArrayDefinition ( e ) { const upperName = e . name . replace ( /[A-Z]/g , letter => `_${ letter }` ) . slice ( 1 ) . toUpperCase ( ) ; const values = e . getValues ( ) . map ( v => `'${ v . value }'` ) . join ( ', ' ) ; return `export const ${ upperName }: ${ e . name }[] = [${ values }];` ; } function buildImportStatement ( enums , importFrom ) { const names = Object . values ( enums ) . map ( e => e . name ) ; return [ `import { ${ names . join ( ', ' ) } } from "${ importFrom }";` ] ; } export const plugin = async ( schema , _documents , config ) : Promise < Types . PluginOutput > => { const importFrom = config . importFrom ; const enums = getEnumTypeMap ( schema ) ; const content = enums . map ( buildArrayDefinition ) . join ( '\n' ) ; const result = { content } ; if ( importFrom ) { result [ 'prepend' ] = buildImportStatement ( enums , importFrom ) ; } return result ; } ; export default { plugin } ;