Skip to content

Commit ff7f07b

Browse files
authored
Merge branch 'Code-Hex:main' into main
2 parents 4a2a209 + 18bc46d commit ff7f07b

File tree

8 files changed

+588
-474
lines changed

8 files changed

+588
-474
lines changed

package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-codegen-typescript-validation-schema",
3-
"version": "0.6.0",
3+
"version": "0.6.2",
44
"description": "GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema",
55
"respository": {
66
"type": "git",
@@ -47,18 +47,18 @@
4747
"@graphql-codegen/cli": "2.12.0",
4848
"@graphql-codegen/typescript": "2.7.3",
4949
"@tsconfig/recommended": "1.0.1",
50-
"@types/jest": "28.1.8",
51-
"@typescript-eslint/eslint-plugin": "5.35.1",
52-
"@typescript-eslint/parser": "5.35.1",
53-
"eslint": "8.23.0",
54-
"jest": "29.0.1",
50+
"@types/jest": "29.0.3",
51+
"@typescript-eslint/eslint-plugin": "5.37.0",
52+
"@typescript-eslint/parser": "5.37.0",
53+
"eslint": "8.23.1",
54+
"jest": "29.0.3",
5555
"myzod": "1.8.8",
5656
"npm-run-all": "4.1.5",
5757
"prettier": "2.7.1",
58-
"ts-jest": "28.0.8",
59-
"typescript": "4.7.4",
58+
"ts-jest": "29.0.1",
59+
"typescript": "4.8.3",
6060
"yup": "0.32.11",
61-
"zod": "3.18.0"
61+
"zod": "3.19.1"
6262
},
6363
"dependencies": {
6464
"@graphql-codegen/plugin-helpers": "^2.3.2",

src/directive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const applyArgToApiSchemaTemplate = (template: string, apiArgs: any[]): string =
162162
const placeholder = match[0]; // `$1`
163163
const idx = parseInt(match[1], 10) - 1; // start with `1 - 1`
164164
const apiArg = apiArgs[idx];
165-
if (!apiArg) {
165+
if (apiArg === undefined) {
166166
template = template.replace(placeholder, '');
167167
continue;
168168
}

src/graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const ObjectTypeDefinitionBuilder = (
1414
): ObjectTypeDefinitionFn | undefined => {
1515
if (!useObjectTypes) return undefined;
1616
return node => {
17-
if (/^Query|Mutation|Subscription$/.test(node.name.value)) {
17+
if (/^(Query|Mutation|Subscription)$/.test(node.name.value)) {
1818
return;
1919
}
2020
return callback(node);

tests/graphql.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Kind, ObjectTypeDefinitionNode } from 'graphql';
2+
import { ObjectTypeDefinitionBuilder } from '../src/graphql';
3+
4+
describe('graphql', () => {
5+
describe('ObjectTypeDefinitionBuilder', () => {
6+
describe('useObjectTypes === true', () => {
7+
test.each([
8+
['Query', false],
9+
['Mutation', false],
10+
['Subscription', false],
11+
['QueryFoo', true],
12+
['MutationFoo', true],
13+
['SubscriptionFoo', true],
14+
['FooQuery', true],
15+
['FooMutation', true],
16+
['FooSubscription', true],
17+
['Foo', true],
18+
])(`A node with a name of "%s" should be matched? %s`, (nodeName, nodeIsMatched) => {
19+
const node: ObjectTypeDefinitionNode = {
20+
name: {
21+
kind: Kind.NAME,
22+
value: nodeName,
23+
},
24+
kind: Kind.OBJECT_TYPE_DEFINITION,
25+
};
26+
27+
const objectTypeDefFn = ObjectTypeDefinitionBuilder(true, (n: ObjectTypeDefinitionNode) => n);
28+
29+
expect(objectTypeDefFn).toBeDefined();
30+
31+
if (nodeIsMatched) {
32+
expect(objectTypeDefFn?.(node)).toBe(node);
33+
} else {
34+
expect(objectTypeDefFn?.(node)).toBeUndefined();
35+
}
36+
});
37+
});
38+
39+
describe('useObjectTypes === false', () => {
40+
test('should not return an ObjectTypeDefinitionFn', () => {
41+
const objectTypeDefFn = ObjectTypeDefinitionBuilder(false, (n: ObjectTypeDefinitionNode) => n);
42+
expect(objectTypeDefFn).toBeUndefined();
43+
});
44+
});
45+
});
46+
});

tests/myzod.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,38 @@ describe('myzod', () => {
626626
}
627627
});
628628
});
629+
630+
it('properly generates custom directive values', async () => {
631+
const schema = buildSchema(/* GraphQL */ `
632+
input UserCreateInput {
633+
name: String! @constraint(startsWith: "Sir")
634+
age: Int! @constraint(min: 0, max: 100)
635+
}
636+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
637+
`);
638+
const result = await plugin(
639+
schema,
640+
[],
641+
{
642+
schema: 'myzod',
643+
directives: {
644+
constraint: {
645+
min: 'min',
646+
max: 'max',
647+
startsWith: ['pattern', '/^$1/'],
648+
},
649+
},
650+
},
651+
{}
652+
);
653+
const wantContains = [
654+
// User Create Input
655+
'export function UserCreateInputSchema(): myzod.Type<UserCreateInput> {',
656+
'name: myzod.string().pattern(/^Sir/),',
657+
'age: myzod.number().min(0).max(100)',
658+
];
659+
for (const wantContain of wantContains) {
660+
expect(result.content).toContain(wantContain);
661+
}
662+
});
629663
});

tests/yup.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,38 @@ describe('yup', () => {
539539
}
540540
});
541541
});
542+
543+
it('properly generates custom directive values', async () => {
544+
const schema = buildSchema(/* GraphQL */ `
545+
input UserCreateInput {
546+
name: String! @constraint(startsWith: "Sir")
547+
age: Int! @constraint(min: 0, max: 100)
548+
}
549+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
550+
`);
551+
const result = await plugin(
552+
schema,
553+
[],
554+
{
555+
schema: 'yup',
556+
directives: {
557+
constraint: {
558+
min: 'min',
559+
max: 'max',
560+
startsWith: ['matches', '/^$1/'],
561+
},
562+
},
563+
},
564+
{}
565+
);
566+
const wantContains = [
567+
// User Create Input
568+
'export function UserCreateInputSchema(): yup.SchemaOf<UserCreateInput> {',
569+
'name: yup.string().defined().matches(/^Sir/),',
570+
'age: yup.number().defined().min(0).max(100)',
571+
];
572+
for (const wantContain of wantContains) {
573+
expect(result.content).toContain(wantContain);
574+
}
575+
});
542576
});

tests/zod.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -724,4 +724,38 @@ describe('zod', () => {
724724
}
725725
});
726726
});
727+
728+
it('properly generates custom directive values', async () => {
729+
const schema = buildSchema(/* GraphQL */ `
730+
input UserCreateInput {
731+
name: String! @constraint(startsWith: "Sir")
732+
age: Int! @constraint(min: 0, max: 100)
733+
}
734+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
735+
`);
736+
const result = await plugin(
737+
schema,
738+
[],
739+
{
740+
schema: 'zod',
741+
directives: {
742+
constraint: {
743+
min: 'min',
744+
max: 'max',
745+
startsWith: ['regex', '/^$1/'],
746+
},
747+
},
748+
},
749+
{}
750+
);
751+
const wantContains = [
752+
// User Create Input
753+
'export function UserCreateInputSchema(): z.ZodObject<Properties<UserCreateInput>> {',
754+
'name: z.string().regex(/^Sir/),',
755+
'age: z.number().min(0).max(100)',
756+
];
757+
for (const wantContain of wantContains) {
758+
expect(result.content).toContain(wantContain);
759+
}
760+
});
727761
});

0 commit comments

Comments
 (0)