|
| 1 | +import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common'; |
| 2 | +import type { |
| 3 | + FieldDefinitionNode, |
| 4 | + GraphQLSchema, |
| 5 | + InputObjectTypeDefinitionNode, |
| 6 | + InputValueDefinitionNode, |
| 7 | + NameNode, |
| 8 | + TypeNode, |
| 9 | +} from 'graphql'; |
| 10 | + |
| 11 | +import type { ValidationSchemaPluginConfig } from '../config'; |
| 12 | +import { BaseSchemaVisitor } from '../schema_visitor'; |
| 13 | +import type { Visitor } from '../visitor'; |
| 14 | +import { |
| 15 | + isNamedType, |
| 16 | + isNonNullType, |
| 17 | +} from './../graphql'; |
| 18 | + |
| 19 | +const anySchema = `definedNonNullAnySchema`; |
| 20 | + |
| 21 | +export class ValibotSchemaVisitor extends BaseSchemaVisitor { |
| 22 | + constructor(schema: GraphQLSchema, config: ValidationSchemaPluginConfig) { |
| 23 | + super(schema, config); |
| 24 | + } |
| 25 | + |
| 26 | + importValidationSchema(): string { |
| 27 | + return `import * as v from 'valibot'`; |
| 28 | + } |
| 29 | + |
| 30 | + initialEmit(): string { |
| 31 | + return ''; |
| 32 | + } |
| 33 | + |
| 34 | + get InputObjectTypeDefinition() { |
| 35 | + return { |
| 36 | + leave: (node: InputObjectTypeDefinitionNode) => { |
| 37 | + const visitor = this.createVisitor('input'); |
| 38 | + const name = visitor.convertName(node.name.value); |
| 39 | + this.importTypes.push(name); |
| 40 | + return this.buildInputFields(node.fields ?? [], visitor, name); |
| 41 | + }, |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + protected buildInputFields( |
| 46 | + fields: readonly (FieldDefinitionNode | InputValueDefinitionNode)[], |
| 47 | + visitor: Visitor, |
| 48 | + name: string, |
| 49 | + ) { |
| 50 | + const shape = fields.map(field => generateFieldValibotSchema(this.config, visitor, field, 2)).join(',\n'); |
| 51 | + |
| 52 | + switch (this.config.validationSchemaExportType) { |
| 53 | + case 'const': |
| 54 | + throw new Error('not implemented'); |
| 55 | + case 'function': |
| 56 | + default: |
| 57 | + return new DeclarationBlock({}) |
| 58 | + .export() |
| 59 | + .asKind('function') |
| 60 | + .withName(`${name}Schema()`) |
| 61 | + .withBlock([indent(`return v.object({`), shape, indent('})')].join('\n')).string; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function generateFieldValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, indentCount: number): string { |
| 67 | + const gen = generateFieldTypeValibotSchema(config, visitor, field, field.type); |
| 68 | + return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount); |
| 69 | +} |
| 70 | + |
| 71 | +function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, parentType?: TypeNode): string { |
| 72 | + if (isNonNullType(type)) { |
| 73 | + const gen = generateFieldTypeValibotSchema(config, visitor, field, type.type, type); |
| 74 | + return maybeLazy(type.type, gen); |
| 75 | + } |
| 76 | + if (isNamedType(type)) { |
| 77 | + const gen = generateNameNodeValibotSchema(config, visitor, type.name); |
| 78 | + |
| 79 | + if (isNonNullType(parentType)) |
| 80 | + return gen; |
| 81 | + } |
| 82 | + console.warn('unhandled type:', type); |
| 83 | + return ''; |
| 84 | +} |
| 85 | + |
| 86 | +function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string { |
| 87 | + const converter = visitor.getNameNodeConverter(node); |
| 88 | + |
| 89 | + switch (converter?.targetKind) { |
| 90 | + case 'InterfaceTypeDefinition': |
| 91 | + case 'InputObjectTypeDefinition': |
| 92 | + case 'ObjectTypeDefinition': |
| 93 | + case 'UnionTypeDefinition': |
| 94 | + case 'EnumTypeDefinition': |
| 95 | + case 'ScalarTypeDefinition': |
| 96 | + throw new Error('not implemented'); |
| 97 | + default: |
| 98 | + if (converter?.targetKind) |
| 99 | + console.warn('Unknown targetKind', converter?.targetKind); |
| 100 | + |
| 101 | + return valibot4Scalar(config, visitor, node.value); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function maybeLazy(type: TypeNode, schema: string): string { |
| 106 | + return schema; |
| 107 | +} |
| 108 | + |
| 109 | +function valibot4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string { |
| 110 | + const tsType = visitor.getScalarType(scalarName); |
| 111 | + switch (tsType) { |
| 112 | + case 'string': |
| 113 | + return `v.string()`; |
| 114 | + case 'number': |
| 115 | + return `v.number()`; |
| 116 | + case 'boolean': |
| 117 | + return `v.boolean()`; |
| 118 | + } |
| 119 | + console.warn('unhandled scalar name:', scalarName); |
| 120 | + return anySchema; |
| 121 | +} |
0 commit comments