Skip to content

Commit 3f9b8ea

Browse files
committed
fixed some code to use oldVisit function
1 parent d3ce6ab commit 3f9b8ea

File tree

3 files changed

+46
-58
lines changed

3 files changed

+46
-58
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { transformSchemaAST } from "@graphql-codegen/schema-ast";
22
import { YupSchemaVisitor } from "./yup/index";
33
import { ValidationSchemaPluginConfig } from "./types";
4-
import { PluginFunction, Types } from "@graphql-codegen/plugin-helpers";
5-
import { GraphQLSchema, visit } from "graphql";
4+
import { oldVisit, PluginFunction, Types } from "@graphql-codegen/plugin-helpers";
5+
import { GraphQLSchema } from "graphql";
66

77
export const plugin: PluginFunction<ValidationSchemaPluginConfig> = async (
88
schema: GraphQLSchema,
@@ -12,7 +12,9 @@ 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, visitor);
15+
const result = oldVisit(ast, {
16+
leave: visitor
17+
});
1618

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

src/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import { TypeScriptPluginConfig } from "@graphql-codegen/typescript";
2-
import {
3-
ASTKindToNode,
4-
ASTNode,
5-
ASTVisitor,
6-
} from "graphql";
72

83
export type ValidationSchema = "yup";
94

@@ -85,7 +80,3 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
8580
*/
8681
enumsAsTypes?: boolean;
8782
}
88-
89-
export type ValidationSchemaVisitor = ASTVisitor & {
90-
buildImports: () => string[]
91-
};

src/yup/index.ts

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { isInput, isNonNullType, isListType, isNamedType } from "./../graphql";
2-
import {
3-
ValidationSchemaPluginConfig,
4-
ValidationSchemaVisitor,
5-
} from "./../types";
2+
import { ValidationSchemaPluginConfig } from "./../types";
63
import {
74
InputValueDefinitionNode,
85
NameNode,
96
TypeNode,
107
GraphQLSchema,
8+
InputObjectTypeDefinitionNode,
9+
EnumTypeDefinitionNode,
1110
} from "graphql";
1211
import {
1312
DeclarationBlock,
@@ -20,7 +19,7 @@ const importYup = `import * as yup from 'yup'`;
2019
export const YupSchemaVisitor = (
2120
schema: GraphQLSchema,
2221
config: ValidationSchemaPluginConfig
23-
): ValidationSchemaVisitor => {
22+
) => {
2423
const tsVisitor = new TsVisitor(schema, config);
2524

2625
const importTypes: string[] = [];
@@ -35,52 +34,48 @@ export const YupSchemaVisitor = (
3534
}
3635
return [importYup];
3736
},
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");
48-
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-
},
37+
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
38+
const name = node.name.value;
39+
importTypes.push(name);
40+
41+
const shape = node.fields
42+
?.map((field) =>
43+
generateInputObjectFieldYupSchema(tsVisitor, schema, field, 2)
44+
)
45+
.join(",\n");
46+
47+
return new DeclarationBlock({})
48+
.export()
49+
.asKind("function")
50+
.withName(`${name}Schema(): yup.SchemaOf<${name}>`)
51+
.withBlock(
52+
[indent(`return yup.object({`), shape, indent("})")].join("\n")
53+
).string;
5754
},
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(", ");
55+
EnumTypeDefinition: (node: EnumTypeDefinitionNode) => {
56+
const enumname = node.name.value;
57+
importTypes.push(enumname);
58+
59+
if (config.enumsAsTypes) {
7860
return new DeclarationBlock({})
7961
.export()
8062
.asKind("const")
8163
.withName(`${enumname}Schema`)
82-
.withContent(`yup.mixed().oneOf([${values}])`).string;
83-
},
64+
.withContent(
65+
`yup.mixed().oneOf([${node.values
66+
?.map((v) => `'${tsVisitor.convertName(v.name.value)}'`)
67+
.join(", ")}])`
68+
).string;
69+
}
70+
71+
const values = node.values
72+
?.map((v) => `${enumname}.${tsVisitor.convertName(v.name.value)}`)
73+
.join(", ");
74+
return new DeclarationBlock({})
75+
.export()
76+
.asKind("const")
77+
.withName(`${enumname}Schema`)
78+
.withContent(`yup.mixed().oneOf([${values}])`).string;
8479
},
8580
// ScalarTypeDefinition: (node) => {
8681
// const decl = new DeclarationBlock({})

0 commit comments

Comments
 (0)