-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.ts
195 lines (177 loc) · 6.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { isInput, isNonNullType, isListType, isNamedType } from './../graphql';
import { ValidationSchemaPluginConfig } from '../config';
import {
InputValueDefinitionNode,
NameNode,
TypeNode,
GraphQLSchema,
InputObjectTypeDefinitionNode,
EnumTypeDefinitionNode,
} from 'graphql';
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
import { TsVisitor } from '@graphql-codegen/typescript';
import { buildApi, formatDirectiveConfig } from '../directive';
const importYup = `import * as yup from 'yup'`;
export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
const tsVisitor = new TsVisitor(schema, config);
const importTypes: string[] = [];
return {
buildImports: (): string[] => {
if (config.importFrom && importTypes.length > 0) {
return [importYup, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`];
}
return [importYup];
},
initialEmit: (): string => '',
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
const name = tsVisitor.convertName(node.name.value);
importTypes.push(name);
const shape = node.fields
?.map(field => generateInputObjectFieldYupSchema(config, tsVisitor, schema, field, 2))
.join(',\n');
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): yup.SchemaOf<${name}>`)
.withBlock([indent(`return yup.object({`), shape, indent('})')].join('\n')).string;
},
EnumTypeDefinition: (node: EnumTypeDefinitionNode) => {
const enumname = tsVisitor.convertName(node.name.value);
importTypes.push(enumname);
if (config.enumsAsTypes) {
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withContent(
`yup.mixed().oneOf([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`
).string;
}
const values = node.values
?.map(
enumOption =>
`${enumname}.${tsVisitor.convertName(enumOption.name, {
useTypesPrefix: false,
transformUnderscore: true,
})}`
)
.join(', ');
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withContent(`yup.mixed().oneOf([${values}])`).string;
},
// ScalarTypeDefinition: (node) => {
// const decl = new DeclarationBlock({})
// .export()
// .asKind("const")
// .withName(`${node.name.value}Schema`);
// if (tsVisitor.scalars[node.name.value]) {
// const tsType = tsVisitor.scalars[node.name.value];
// switch (tsType) {
// case "string":
// return decl.withContent(`yup.string()`).string;
// case "number":
// return decl.withContent(`yup.number()`).string;
// case "boolean":
// return decl.withContent(`yup.boolean()`).string;
// }
// }
// return decl.withContent(`yup.mixed()`).string;
// },
};
};
const generateInputObjectFieldYupSchema = (
config: ValidationSchemaPluginConfig,
tsVisitor: TsVisitor,
schema: GraphQLSchema,
field: InputValueDefinitionNode,
indentCount: number
): string => {
let gen = generateInputObjectFieldTypeYupSchema(config, tsVisitor, schema, field.type);
if (config.directives && field.directives) {
const formatted = formatDirectiveConfig(config.directives);
gen += buildApi(formatted, field.directives);
}
return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount);
};
const generateInputObjectFieldTypeYupSchema = (
config: ValidationSchemaPluginConfig,
tsVisitor: TsVisitor,
schema: GraphQLSchema,
type: TypeNode,
parentType?: TypeNode
): string => {
if (isListType(type)) {
const gen = generateInputObjectFieldTypeYupSchema(config, tsVisitor, schema, type.type, type);
if (!isNonNullType(parentType)) {
return `yup.array().of(${maybeLazy(type.type, gen)}).optional()`;
}
return `yup.array().of(${maybeLazy(type.type, gen)})`;
}
if (isNonNullType(type)) {
const gen = generateInputObjectFieldTypeYupSchema(config, tsVisitor, schema, type.type, type);
const nonNullGen = maybeNonEmptyString(config, tsVisitor, gen, type.type);
return maybeLazy(type.type, nonNullGen);
}
if (isNamedType(type)) {
return generateNameNodeYupSchema(config, tsVisitor, schema, type.name);
}
console.warn('unhandled type:', type);
return '';
};
const generateNameNodeYupSchema = (
config: ValidationSchemaPluginConfig,
tsVisitor: TsVisitor,
schema: GraphQLSchema,
node: NameNode
): string => {
const typ = schema.getType(node.value);
if (typ && typ.astNode?.kind === 'InputObjectTypeDefinition') {
const enumName = tsVisitor.convertName(typ.astNode.name.value);
return `${enumName}Schema()`;
}
if (typ && typ.astNode?.kind === 'EnumTypeDefinition') {
const enumName = tsVisitor.convertName(typ.astNode.name.value);
return `${enumName}Schema`;
}
const primitive = yup4Scalar(tsVisitor, node.value);
return primitive;
};
const maybeLazy = (type: TypeNode, schema: string): string => {
if (isNamedType(type) && isInput(type.name.value)) {
// https://github.com/jquense/yup/issues/1283#issuecomment-786559444
return `yup.lazy(() => ${schema}) as never`;
}
return schema;
};
const maybeNonEmptyString = (
config: ValidationSchemaPluginConfig,
tsVisitor: TsVisitor,
schema: string,
childType: TypeNode
): string => {
if (config.notAllowEmptyString === true && isNamedType(childType)) {
const maybeScalarName = childType.name.value;
const tsType = tsVisitor.scalars[maybeScalarName];
if (tsType === 'string') {
return `${schema}.required()`;
}
}
// fallback
return `${schema}.defined()`;
};
const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
const tsType = tsVisitor.scalars[scalarName];
switch (tsType) {
case 'string':
return `yup.string()`;
case 'number':
return `yup.number()`;
case 'boolean':
return `yup.boolean()`;
}
console.warn('unhandled name:', scalarName);
return `yup.mixed()`;
};