|
| 1 | +import { GraphQLSchema } from 'graphql'; |
| 2 | +import { Types } from '@graphql-codegen/plugin-helpers'; |
| 3 | +import { ScalaBaseVisitor } from './base-visitor'; |
| 4 | +import { ScalaPluginCommonRawConfig } from './config'; |
| 5 | + |
| 6 | +export type BasePluginFunction<TRawConfig extends ScalaPluginCommonRawConfig> = ( |
| 7 | + schema: GraphQLSchema, |
| 8 | + documents: Types.DocumentFile[], |
| 9 | + config: TRawConfig, |
| 10 | + info: { outputFile?: string }, |
| 11 | +) => Types.PluginOutput; |
| 12 | + |
| 13 | +export const createBasePlugin = < |
| 14 | + TRawConfig extends ScalaPluginCommonRawConfig, |
| 15 | + TVisitor extends ScalaBaseVisitor<TRawConfig, any>, |
| 16 | +>( |
| 17 | + createVisitor: ( |
| 18 | + schema: GraphQLSchema, |
| 19 | + config: TRawConfig, |
| 20 | + outputFileInfo: { outputFile?: string }, |
| 21 | + ) => TVisitor, |
| 22 | +): BasePluginFunction<TRawConfig> => { |
| 23 | + return ( |
| 24 | + schema: GraphQLSchema, |
| 25 | + _: Types.DocumentFile[], |
| 26 | + config: TRawConfig, |
| 27 | + info: { outputFile?: string }, |
| 28 | + ): Types.PluginOutput => { |
| 29 | + if (!config.packageName && info.outputFile) { |
| 30 | + const parts = info.outputFile.split('/'); |
| 31 | + parts.pop(); |
| 32 | + |
| 33 | + if (parts.length > 0) { |
| 34 | + config = { |
| 35 | + ...config, |
| 36 | + packageName: parts.join('.'), |
| 37 | + }; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const visitor = createVisitor(schema, config, { outputFile: info.outputFile }); |
| 42 | + const typeNames = Object.keys(schema.getTypeMap()).filter( |
| 43 | + typeName => !typeName.startsWith('__'), |
| 44 | + ); |
| 45 | + |
| 46 | + const header = [ |
| 47 | + visitor.getPackage(), |
| 48 | + '', |
| 49 | + visitor.getImports(), |
| 50 | + '', |
| 51 | + 'getTypeClassDefinitions' in visitor ? (visitor as any).getTypeClassDefinitions() : '', |
| 52 | + ] |
| 53 | + .filter(Boolean) |
| 54 | + .join('\n'); |
| 55 | + |
| 56 | + const schemaScalarTypeNames = typeNames.filter(typeName => { |
| 57 | + const type = schema.getTypeMap()[typeName]; |
| 58 | + return type.astNode?.kind === 'ScalarTypeDefinition'; |
| 59 | + }); |
| 60 | + |
| 61 | + const customScalarDefinitions: string[] = []; |
| 62 | + if (config.scalars) { |
| 63 | + Object.keys(config.scalars).forEach(scalarName => { |
| 64 | + if (typeof config.scalars[scalarName] === 'string') { |
| 65 | + const scalarType = config.scalars[scalarName]; |
| 66 | + customScalarDefinitions.push(`type ${scalarName} = ${scalarType}`); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + const defaultScalars = visitor.generateCustomScalars |
| 72 | + ? visitor.generateCustomScalars([ |
| 73 | + ...schemaScalarTypeNames, |
| 74 | + ...Object.keys(config.scalars || {}), |
| 75 | + ]) |
| 76 | + : []; |
| 77 | + |
| 78 | + const contentFragments = typeNames.map(typeName => { |
| 79 | + const type = schema.getTypeMap()[typeName]; |
| 80 | + |
| 81 | + if ( |
| 82 | + type.astNode?.kind === 'ScalarTypeDefinition' && |
| 83 | + config.scalars && |
| 84 | + typeof config.scalars[typeName] === 'string' |
| 85 | + ) { |
| 86 | + return ''; |
| 87 | + } |
| 88 | + |
| 89 | + const astNodeKind = type.astNode?.kind as string | undefined; |
| 90 | + if (astNodeKind && astNodeKind in visitor) { |
| 91 | + const handler = (visitor as any)[astNodeKind]; |
| 92 | + if (typeof handler === 'function') { |
| 93 | + return handler.call(visitor, type.astNode); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return ''; |
| 98 | + }); |
| 99 | + |
| 100 | + const content = [ |
| 101 | + header, |
| 102 | + ...customScalarDefinitions, |
| 103 | + ...defaultScalars, |
| 104 | + ...contentFragments.filter(Boolean), |
| 105 | + ].join('\n\n'); |
| 106 | + |
| 107 | + return { |
| 108 | + content, |
| 109 | + prepend: [], |
| 110 | + append: [], |
| 111 | + }; |
| 112 | + }; |
| 113 | +}; |
0 commit comments