Skip to content

Commit 929bbc8

Browse files
committed
fixed some code
1 parent 0331145 commit 929bbc8

File tree

4 files changed

+219
-193
lines changed

4 files changed

+219
-193
lines changed

src/graphql.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ import {
33
NonNullTypeNode,
44
NamedTypeNode,
55
TypeNode,
6-
GraphQLSchema,
7-
InputObjectTypeDefinitionNode,
8-
EnumTypeDefinitionNode,
9-
ScalarTypeDefinitionNode,
10-
visit,
116
} from "graphql";
12-
import { transformSchemaAST } from "@graphql-codegen/schema-ast";
13-
import { Nodes, ValidationSchemaPluginConfig } from "./types";
147

158
export const isListType = (typ: TypeNode): typ is ListTypeNode =>
169
typ.kind === "ListType";
@@ -19,37 +12,4 @@ export const isNonNullType = (typ: TypeNode): typ is NonNullTypeNode =>
1912
export const isNamedType = (typ: TypeNode): typ is NamedTypeNode =>
2013
typ.kind === "NamedType";
2114

22-
export const isRef = (kind: string) => kind.includes("Input");
23-
export const isBoolean = (kind: string) => kind === "Boolean";
24-
export const isString = (kind: string) => ["ID", "String"].includes(kind);
25-
export const isNumber = (kind: string) => ["Int", "Float"].includes(kind);
26-
27-
28-
export const retrieveSchema = (
29-
schema: GraphQLSchema,
30-
config: ValidationSchemaPluginConfig
31-
): Nodes => {
32-
const { ast } = transformSchemaAST(schema, config);
33-
34-
const inputObjects: InputObjectTypeDefinitionNode[] = [];
35-
const enums: Record<string, EnumTypeDefinitionNode> = {};
36-
const scalars: Record<string, ScalarTypeDefinitionNode> = {};
37-
38-
visit(ast, {
39-
leave: {
40-
InputObjectTypeDefinition: (node) => {
41-
inputObjects.unshift(node);
42-
},
43-
EnumTypeDefinition: (node) => {
44-
if (node.values) {
45-
enums[node.name.value] = node;
46-
}
47-
},
48-
ScalarTypeDefinition: (node) => {
49-
scalars[node.name.value] = node;
50-
},
51-
},
52-
});
53-
54-
return { inputObjects, enums, scalars };
55-
};
15+
export const isInput = (kind: string) => kind.includes("Input");

src/index.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
1-
import { ImportYup, YupGenerator } from "./yup/index";
2-
import { ValidationSchema, ValidationSchemaPluginConfig } from "./types";
1+
import { transformSchemaAST } from "@graphql-codegen/schema-ast";
2+
import { YupSchemaVisitor } from "./yup/index";
3+
import { ValidationSchemaPluginConfig } from "./types";
34
import { PluginFunction, Types } from "@graphql-codegen/plugin-helpers";
4-
import { GraphQLSchema } from "graphql";
5-
import { retrieveSchema } from "./graphql";
5+
import { GraphQLSchema, visit } from "graphql";
66

77
export const plugin: PluginFunction<ValidationSchemaPluginConfig> = async (
88
schema: GraphQLSchema,
99
_documents: Types.DocumentFile[],
1010
config: ValidationSchemaPluginConfig
1111
): Promise<Types.PluginOutput> => {
12-
const { inputObjects, enums, scalars } = retrieveSchema(schema, config);
12+
const { schema: _schema, ast } = transformSchemaAST(schema, config);
13+
const { buildImports, ...visitor } = YupSchemaVisitor(_schema, config);
1314

14-
const prepend = [importSchema(config.schema)];
15-
if (config.importFrom) {
16-
prepend.push(
17-
buildImportStatement(
18-
[
19-
// Should put on Scalar here?
20-
...Object.values(enums).map((enumdef) => enumdef.name.value),
21-
...inputObjects.map((inputObject) => inputObject.name.value),
22-
],
23-
config.importFrom
24-
)
25-
);
26-
}
27-
return {
28-
prepend,
29-
content:
30-
"\n" +
31-
[new YupGenerator({ inputObjects, enums, scalars }).generate()].join(
32-
"\n"
33-
),
34-
};
35-
};
15+
const result = visit(ast, {
16+
leave: visitor,
17+
});
3618

37-
const buildImportStatement = (types: string[], importFrom: string): string =>
38-
`import { ${types.join(", ")} } from "${importFrom}";`;
19+
// @ts-ignore
20+
const generated = result.definitions.filter((def) => typeof def === "string");
3921

40-
const importSchema = (schema?: ValidationSchema): string => {
41-
if (schema === "yup") {
42-
return ImportYup();
43-
}
44-
// TODO(codehex): support zod
45-
return ImportYup();
22+
return {
23+
prepend: buildImports(),
24+
content: "\n" + [...generated].join("\n"),
25+
};
4626
};

src/types.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { TypeScriptPluginConfig } from "@graphql-codegen/typescript";
1+
import { TsVisitor, TypeScriptPluginConfig } from "@graphql-codegen/typescript";
22
import {
3+
ASTKindToNode,
4+
ASTNode,
35
EnumTypeDefinitionNode,
46
InputObjectTypeDefinitionNode,
57
ScalarTypeDefinitionNode,
8+
VisitFn,
69
} from "graphql";
710

811
export type ValidationSchema = "yup";
@@ -24,7 +27,6 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
2427
* ```
2528
*/
2629
schema?: ValidationSchema;
27-
2830
/**
2931
* @description import types from generated typescript type path
3032
* if not given, omit import statement.
@@ -44,10 +46,51 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
4446
* ```
4547
*/
4648
importFrom?: string;
49+
/**
50+
* @description Generates validation schema for generated `const enum`, you can read more about const enums here: https://www.typescriptlang.org/docs/handbook/enums.html.
51+
* @default false
52+
*
53+
* @exampleMarkdown
54+
* ```yml
55+
* generates:
56+
* path/to/file.ts:
57+
* plugins:
58+
* - typescript
59+
* - graphql-codegen-validation-schema
60+
* config:
61+
* constEnums: true
62+
* ```
63+
*/
64+
constEnums?: boolean;
65+
/**
66+
* @description Generates validation schema for enum as TypeScript `type`
67+
* @default false
68+
*
69+
* @exampleMarkdown
70+
* ```yml
71+
* generates:
72+
* path/to/file.ts:
73+
* plugins:
74+
* - graphql-codegen-validation-schema
75+
* config:
76+
* enumsAsTypes: true
77+
* ```
78+
*
79+
* ```yml
80+
* generates:
81+
* path/to/file.ts:
82+
* plugins:
83+
* - typescript
84+
* - graphql-codegen-validation-schema
85+
* config:
86+
* enumsAsTypes: true
87+
* ```
88+
*/
89+
enumsAsTypes?: boolean;
4790
}
4891

49-
export interface Nodes {
50-
inputObjects: InputObjectTypeDefinitionNode[];
51-
enums: Record<string, EnumTypeDefinitionNode>;
52-
scalars: Record<string, ScalarTypeDefinitionNode>;
53-
}
92+
export type ValidationSchemaVisitor = {
93+
[K in keyof ASTKindToNode]?: VisitFn<ASTNode, ASTKindToNode[K]>;
94+
} & {
95+
buildImports: () => string[]
96+
};

0 commit comments

Comments
 (0)