import { oldVisit , PluginFunction , Types } from '@graphql-codegen/plugin-helpers' ; import { GraphQLSchema , concatAST , Kind , FragmentDefinitionNode } from 'graphql' ; import { RawConfig , LoadedFragment } from '@graphql-codegen/visitor-plugin-common' ; import { InputTypeVisitor } from './input-type-visitor' ; import { BaseJavaVisitor } from './base-java-visitor' ; import { OperationVisitor } from './operation-visitor' ; import { FileType } from './file-type' ; import { CustomTypeClassVisitor } from './custom-type-class' ; description export interface JavaApolloAndroidPluginConfig extends RawConfig { description exampleMarkdown package ? : string ; description exampleMarkdown typePackage ? : string ; description exampleMarkdown fragmentPackage ? : string ; fileType : FileType ; } export const plugin = ( schema , documents , config ) => { const allAst = concatAST ( documents . map ( v => v . document ) ) ; const allFragments = [ ... ( allAst . definitions . filter ( d => d . kind === Kind . FRAGMENT_DEFINITION ) as FragmentDefinitionNode [ ] ) . map ( fragmentDef => ( { node : fragmentDef , name : fragmentDef . name . value , onType : fragmentDef . typeCondition . name . value , isExternal : false , } ) ) , ... ( config . externalFragments || [ ] ) , ] ; let visitor ; switch ( config . fileType ) { case FileType . FRAGMENT : case FileType . OPERATION : { visitor = new OperationVisitor ( schema , config , allFragments ) ; break ; } case FileType . INPUT_TYPE : { visitor = new InputTypeVisitor ( schema , config ) ; break ; } case FileType . CUSTOM_TYPES : { visitor = new CustomTypeClassVisitor ( schema , config ) ; break ; } } if ( ! visitor ) { return { content : '' } ; } const visitResult = oldVisit ( allAst , visitor as any ) ; const additionalContent = visitor . additionalContent ( ) ; const imports = visitor . getImports ( ) ; return { prepend : [ `package ${ visitor . getPackage ( ) };\n` , ... imports ] , content : '\n' + [ ... visitResult . definitions . filter ( a => a && typeof a === 'string' ) , additionalContent ] . join ( '\n' ) , } ; } ;