|
1 | | -import { isInput, isNonNullType, isListType, isNamedType, ObjectTypeDefinitionBuilder } from './../graphql'; |
2 | | -import { ValidationSchemaPluginConfig } from '../config'; |
| 1 | +import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common'; |
3 | 2 | import { |
4 | | - InputValueDefinitionNode, |
5 | | - NameNode, |
6 | | - TypeNode, |
| 3 | + EnumTypeDefinitionNode, |
| 4 | + FieldDefinitionNode, |
7 | 5 | GraphQLSchema, |
8 | 6 | InputObjectTypeDefinitionNode, |
9 | | - EnumTypeDefinitionNode, |
| 7 | + InputValueDefinitionNode, |
| 8 | + NameNode, |
10 | 9 | ObjectTypeDefinitionNode, |
11 | | - FieldDefinitionNode, |
| 10 | + TypeNode, |
12 | 11 | UnionTypeDefinitionNode, |
13 | 12 | } from 'graphql'; |
14 | | -import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common'; |
15 | | -import { Visitor } from '../visitor'; |
| 13 | +import { ValidationSchemaPluginConfig } from '../config'; |
16 | 14 | import { buildApi, formatDirectiveConfig } from '../directive'; |
17 | 15 | import { SchemaVisitor } from '../types'; |
| 16 | +import { Visitor } from '../visitor'; |
| 17 | +import { ObjectTypeDefinitionBuilder, isInput, isListType, isNamedType, isNonNullType } from './../graphql'; |
18 | 18 |
|
19 | 19 | const importYup = `import * as yup from 'yup'`; |
20 | 20 |
|
@@ -178,16 +178,32 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema |
178 | 178 | if (typ?.astNode?.kind === 'EnumTypeDefinition') { |
179 | 179 | return `${element}Schema`; |
180 | 180 | } |
181 | | - return `${element}Schema()`; |
| 181 | + switch (config.validationSchemaExportType) { |
| 182 | + case 'const': |
| 183 | + return `${element}Schema`; |
| 184 | + case 'function': |
| 185 | + default: |
| 186 | + return `${element}Schema()`; |
| 187 | + } |
182 | 188 | }) |
183 | 189 | .join(', '); |
184 | 190 | const union = indent(`return union<${unionName}>(${unionElements})`); |
185 | 191 |
|
186 | | - return new DeclarationBlock({}) |
187 | | - .export() |
188 | | - .asKind('function') |
189 | | - .withName(`${unionName}Schema(): yup.MixedSchema<${unionName}>`) |
190 | | - .withBlock(union).string; |
| 192 | + switch (config.validationSchemaExportType) { |
| 193 | + case 'const': |
| 194 | + return new DeclarationBlock({}) |
| 195 | + .export() |
| 196 | + .asKind('const') |
| 197 | + .withName(`${unionName}Schema: yup.MixedSchema<${unionName}>`) |
| 198 | + .withBlock(union).string; |
| 199 | + case 'function': |
| 200 | + default: |
| 201 | + return new DeclarationBlock({}) |
| 202 | + .export() |
| 203 | + .asKind('function') |
| 204 | + .withName(`${unionName}Schema(): yup.MixedSchema<${unionName}>`) |
| 205 | + .withBlock(union).string; |
| 206 | + } |
191 | 207 | }, |
192 | 208 | }, |
193 | 209 | // ScalarTypeDefinition: (node) => { |
|
0 commit comments