Skip to content

Commit d3ce6ab

Browse files
committed
fixed to use yarn
1 parent 39c3f8e commit d3ce6ab

File tree

5 files changed

+3556
-61
lines changed

5 files changed

+3556
-61
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
"@graphql-codegen/schema-ast": "^2.4.1",
4747
"@graphql-codegen/visitor-plugin-common": "^2.5.2",
4848
"@graphql-tools/utils": "^8.6.1",
49-
"graphql": "^15.8.0"
49+
"graphql": "^16.2.0"
5050
},
5151
"peerDependencies": {
52-
"graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
52+
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
5353
},
5454
"bugs": {
5555
"url": "https://github.com/Code-Hex/graphql-codegen-validation-schema/issues"

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export const plugin: PluginFunction<ValidationSchemaPluginConfig> = async (
1212
const { schema: _schema, ast } = transformSchemaAST(schema, config);
1313
const { buildImports, ...visitor } = YupSchemaVisitor(_schema, config);
1414

15-
const result = visit(ast, {
16-
leave: visitor,
17-
});
15+
const result = visit(ast, visitor);
1816

1917
// @ts-ignore
2018
const generated = result.definitions.filter((def) => typeof def === "string");

src/types.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { TsVisitor, TypeScriptPluginConfig } from "@graphql-codegen/typescript";
1+
import { TypeScriptPluginConfig } from "@graphql-codegen/typescript";
22
import {
33
ASTKindToNode,
44
ASTNode,
5-
EnumTypeDefinitionNode,
6-
InputObjectTypeDefinitionNode,
7-
ScalarTypeDefinitionNode,
8-
VisitFn,
5+
ASTVisitor,
96
} from "graphql";
107

118
export type ValidationSchema = "yup";
@@ -89,8 +86,6 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
8986
enumsAsTypes?: boolean;
9087
}
9188

92-
export type ValidationSchemaVisitor = {
93-
[K in keyof ASTKindToNode]?: VisitFn<ASTNode, ASTKindToNode[K]>;
94-
} & {
89+
export type ValidationSchemaVisitor = ASTVisitor & {
9590
buildImports: () => string[]
9691
};

src/yup/index.ts

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
isInput,
3-
isNonNullType,
4-
isListType,
5-
isNamedType,
6-
} from "./../graphql";
1+
import { isInput, isNonNullType, isListType, isNamedType } from "./../graphql";
72
import {
83
ValidationSchemaPluginConfig,
94
ValidationSchemaVisitor,
@@ -40,46 +35,52 @@ export const YupSchemaVisitor = (
4035
}
4136
return [importYup];
4237
},
43-
InputObjectTypeDefinition: (node) => {
44-
const name = node.name.value;
45-
importTypes.push(name);
46-
47-
const shape = node.fields
48-
?.map((field) => generateInputObjectFieldYupSchema(tsVisitor, schema, field, 2))
49-
.join(",\n");
50-
51-
return new DeclarationBlock({})
52-
.export()
53-
.asKind("function")
54-
.withName(`${name}Schema(): yup.SchemaOf<${name}>`)
55-
.withBlock(
56-
[indent(`return yup.object({`), shape, indent("})")].join("\n")
57-
).string;
58-
},
59-
EnumTypeDefinition: (node) => {
60-
const enumname = node.name.value;
61-
importTypes.push(enumname);
38+
InputObjectTypeDefinition: {
39+
leave(node) {
40+
const name = node.name.value;
41+
importTypes.push(name);
42+
43+
const shape = node.fields
44+
?.map((field) =>
45+
generateInputObjectFieldYupSchema(tsVisitor, schema, field, 2)
46+
)
47+
.join(",\n");
6248

63-
if (config.enumsAsTypes) {
49+
return new DeclarationBlock({})
50+
.export()
51+
.asKind("function")
52+
.withName(`${name}Schema(): yup.SchemaOf<${name}>`)
53+
.withBlock(
54+
[indent(`return yup.object({`), shape, indent("})")].join("\n")
55+
).string;
56+
},
57+
},
58+
EnumTypeDefinition: {
59+
leave(node) {
60+
const enumname = node.name.value;
61+
importTypes.push(enumname);
62+
63+
if (config.enumsAsTypes) {
64+
return new DeclarationBlock({})
65+
.export()
66+
.asKind("const")
67+
.withName(`${enumname}Schema`)
68+
.withContent(
69+
`yup.mixed().oneOf([${node.values
70+
?.map((v) => `'${tsVisitor.convertName(v.name.value)}'`)
71+
.join(", ")}])`
72+
).string;
73+
}
74+
75+
const values = node.values
76+
?.map((v) => `${enumname}.${tsVisitor.convertName(v.name.value)}`)
77+
.join(", ");
6478
return new DeclarationBlock({})
6579
.export()
6680
.asKind("const")
6781
.withName(`${enumname}Schema`)
68-
.withContent(
69-
`yup.mixed().oneOf([${node.values
70-
?.map((v) => `'${tsVisitor.convertName(v.name.value)}'`)
71-
.join(", ")}])`
72-
).string;
73-
}
74-
75-
const values = node.values
76-
?.map((v) => `${enumname}.${tsVisitor.convertName(v.name.value)}`)
77-
.join(", ");
78-
return new DeclarationBlock({})
79-
.export()
80-
.asKind("const")
81-
.withName(`${enumname}Schema`)
82-
.withContent(`yup.mixed().oneOf([${values}])`).string;
82+
.withContent(`yup.mixed().oneOf([${values}])`).string;
83+
},
8384
},
8485
// ScalarTypeDefinition: (node) => {
8586
// const decl = new DeclarationBlock({})
@@ -110,7 +111,11 @@ const generateInputObjectFieldYupSchema = (
110111
indentCount: number
111112
): string => {
112113
// TOOD(codehex): handle directive
113-
const gen = generateInputObjectFieldTypeYupSchema(tsVisitor, schema, field.type);
114+
const gen = generateInputObjectFieldTypeYupSchema(
115+
tsVisitor,
116+
schema,
117+
field.type
118+
);
114119
return indent(
115120
`${field.name.value}: ${maybeLazy(field.type, gen)}`,
116121
indentCount
@@ -123,11 +128,19 @@ const generateInputObjectFieldTypeYupSchema = (
123128
type: TypeNode
124129
): string => {
125130
if (isListType(type)) {
126-
const gen = generateInputObjectFieldTypeYupSchema(tsVisitor, schema, type.type);
131+
const gen = generateInputObjectFieldTypeYupSchema(
132+
tsVisitor,
133+
schema,
134+
type.type
135+
);
127136
return `yup.array().of(${maybeLazy(type.type, gen)})`;
128137
}
129138
if (isNonNullType(type)) {
130-
const gen = generateInputObjectFieldTypeYupSchema(tsVisitor, schema, type.type);
139+
const gen = generateInputObjectFieldTypeYupSchema(
140+
tsVisitor,
141+
schema,
142+
type.type
143+
);
131144
return maybeLazy(type.type, `${gen}.required()`);
132145
}
133146
if (isNamedType(type)) {
@@ -169,12 +182,12 @@ const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
169182
const tsType = tsVisitor.scalars[scalarName];
170183
switch (tsType) {
171184
case "string":
172-
return `yup.string()`
185+
return `yup.string()`;
173186
case "number":
174-
return `yup.number()`
187+
return `yup.number()`;
175188
case "boolean":
176-
return `yup.boolean()`
189+
return `yup.boolean()`;
177190
}
178191
console.warn("unhandled name:", scalarName);
179-
return `yup.mixed()`
192+
return `yup.mixed()`;
180193
};

0 commit comments

Comments
 (0)