Skip to content

implement for variables #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implemented to build variable schema
  • Loading branch information
Code-Hex committed Aug 13, 2023
commit 3b531d07ed12bf24d9e882a77a79ff586a66f780
77 changes: 53 additions & 24 deletions src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,66 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
const name = visitor.convertName(node.name.value);
importTypes.push(name);

// Building schema for field arguments.
const argumentBlocks = visitor.buildArgumentsSchemaBlock(node, (typeName, field) => {
importTypes.push(typeName);
const args = field.arguments ?? [];
const shape = args.map(field => generateFieldMyZodSchema(config, visitor, field, 2)).join(',\n');
switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${typeName}Schema: myzod.Type<${typeName}>`)
.withContent([`myzod.object({`, shape, '})'].join('\n')).string;

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${typeName}Schema(): myzod.Type<${typeName}>`)
.withBlock([indent(`return myzod.object({`), shape, indent('})')].join('\n')).string;
}
});
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '';

// Building schema for fields.
const shape = node.fields?.map(field => generateFieldMyZodSchema(config, visitor, field, 2)).join(',\n');

switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: myzod.Type<${name}>`)
.withContent(
[
`myzod.object({`,
indent(`__typename: myzod.literal('${node.name.value}').optional(),`, 2),
shape,
'})',
].join('\n')
).string;
return (
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: myzod.Type<${name}>`)
.withContent(
[
`myzod.object({`,
indent(`__typename: myzod.literal('${node.name.value}').optional(),`, 2),
shape,
'})',
].join('\n')
).string + appendArguments
);

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): myzod.Type<${name}>`)
.withBlock(
[
indent(`return myzod.object({`),
indent(`__typename: myzod.literal('${node.name.value}').optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string;
return (
new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): myzod.Type<${name}>`)
.withBlock(
[
indent(`return myzod.object({`),
indent(`__typename: myzod.literal('${node.name.value}').optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string + appendArguments
);
}
}),
},
Expand Down
26 changes: 25 additions & 1 deletion src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TsVisitor } from '@graphql-codegen/typescript';
import { GraphQLSchema, NameNode, specifiedScalarTypes } from 'graphql';
import { FieldDefinitionNode, GraphQLSchema, NameNode, ObjectTypeDefinitionNode, specifiedScalarTypes } from 'graphql';

import { ValidationSchemaPluginConfig } from './config';

Expand Down Expand Up @@ -50,4 +50,28 @@ export class Visitor extends TsVisitor {
const tsType = this.getScalarType(name);
return tsType === 'string';
}

public buildArgumentsSchemaBlock(
node: ObjectTypeDefinitionNode,
callback: (typeName: string, field: FieldDefinitionNode) => string
) {
const fieldsWithArguments = node.fields?.filter(field => field.arguments && field.arguments.length > 0) ?? [];
if (fieldsWithArguments.length === 0) {
return undefined;
}
return fieldsWithArguments
.map(field => {
const name =
node.name.value +
(this.config.addUnderscoreToArgsType ? '_' : '') +
this.convertName(field, {
useTypesPrefix: false,
useTypesSuffix: false,
}) +
'Args';

return callback(name, field);
})
.join('\n');
}
}
78 changes: 54 additions & 24 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
const name = visitor.convertName(node.name.value);
importTypes.push(name);

// Building schema for field arguments.
const argumentBlocks = visitor.buildArgumentsSchemaBlock(node, (typeName, field) => {
importTypes.push(typeName);
const args = field.arguments ?? [];
const shape = args.map(field => generateFieldYupSchema(config, visitor, field, 2)).join(',\n');
switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${typeName}Schema: yup.ObjectSchema<${typeName}>`)
.withContent([`yup.object({`, shape, '})'].join('\n')).string;

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${typeName}Schema(): yup.ObjectSchema<${typeName}>`)
.withBlock([indent(`return yup.object({`), shape, indent('})')].join('\n')).string;
}
});
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '';

// Building schema for fields.

const shape = node.fields
?.map(field => {
const fieldSchema = generateFieldYupSchema(config, visitor, field, 2);
Expand All @@ -97,33 +123,37 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema

switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: yup.ObjectSchema<${name}>`)
.withContent(
[
`yup.object({`,
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
'})',
].join('\n')
).string;
return (
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: yup.ObjectSchema<${name}>`)
.withContent(
[
`yup.object({`,
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
'})',
].join('\n')
).string + appendArguments
);

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): yup.ObjectSchema<${name}>`)
.withBlock(
[
indent(`return yup.object({`),
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string;
return (
new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): yup.ObjectSchema<${name}>`)
.withBlock(
[
indent(`return yup.object({`),
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string + appendArguments
);
}
}),
},
Expand Down
74 changes: 53 additions & 21 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,66 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
const name = visitor.convertName(node.name.value);
importTypes.push(name);

// Building schema for field arguments.
const argumentBlocks = visitor.buildArgumentsSchemaBlock(node, (typeName, field) => {
importTypes.push(typeName);
const args = field.arguments ?? [];
const shape = args.map(field => generateFieldZodSchema(config, visitor, field, 2)).join(',\n');
switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${typeName}Schema: z.ZodObject<Properties<${typeName}>>`)
.withContent([`z.object({`, shape, '})'].join('\n')).string;

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${typeName}Schema(): z.ZodObject<Properties<${typeName}>>`)
.withBlock([indent(`return z.object({`), shape, indent('})')].join('\n')).string;
}
});
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '';

// Building schema for fields.
const shape = node.fields?.map(field => generateFieldZodSchema(config, visitor, field, 2)).join(',\n');

switch (config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: z.ZodObject<Properties<${name}>>`)
.withContent(
[`z.object({`, indent(`__typename: z.literal('${node.name.value}').optional(),`, 2), shape, '})'].join(
'\n'
)
).string;
return (
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: z.ZodObject<Properties<${name}>>`)
.withContent(
[
`z.object({`,
indent(`__typename: z.literal('${node.name.value}').optional(),`, 2),
shape,
'})',
].join('\n')
).string + appendArguments
);

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): z.ZodObject<Properties<${name}>>`)
.withBlock(
[
indent(`return z.object({`),
indent(`__typename: z.literal('${node.name.value}').optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string;
return (
new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): z.ZodObject<Properties<${name}>>`)
.withBlock(
[
indent(`return z.object({`),
indent(`__typename: z.literal('${node.name.value}').optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string + appendArguments
);
}
}),
},
Expand Down