Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
generateClassLoader(): string {
const AMPLIFY_MODEL_VERSION = 'AMPLIFY_MODEL_VERSION';
const result: string[] = [this.generatePackageName(), '', this.generateImportStatements(LOADER_IMPORT_PACKAGES)];
result.push(
transformComment(dedent` Contains the set of model classes that implement {@link Model}
interface.`)
);
const loaderClassDeclaration = new JavaDeclarationBlock()
.withName(LOADER_CLASS_NAME)
.access('public')
.final()
.asKind('class')
.implements(['ModelProvider']);
// Schema version
// private static final String AMPLIFY_MODELS_VERSION = "hash-code";
loaderClassDeclaration.addClassMember(AMPLIFY_MODEL_VERSION, 'String', `"${this.computeVersion()}"`, [], 'private', {
final: true,
static: true,
});
.map(enumOption => {
let enumValue: string = (enumOption.name as any) as string;
const comment = transformComment((enumOption.description as any) as string, 1);
if (this.config.enumValues[enumName] && this.config.enumValues[enumName].mappedValues && this.config.enumValues[enumName].mappedValues[enumValue]) {
enumValue = this.config.enumValues[enumName].mappedValues[enumValue];
}
return comment + indent(wrapWithSingleQuotes(enumValue));
})
.join(' |\n')
private printMethod(method: ClassMethod): string {
const pieces = [
...method.methodAnnotations.map(a => `@${a}\n`),
method.access,
method.flags.static ? 'static' : null,
method.flags.final ? 'final' : null,
method.flags.transient ? 'transient' : null,
method.flags.volatile ? 'volatile' : null,
...(method.returnTypeAnnotations || []).map(annotation => `@${annotation}`),
method.returnType,
method.name,
].filter(f => f);
const args = method.args.map(arg => this.printMember(arg)).join(', ');
const comment = method.comment ? transformComment(method.comment) : '';
const possibleException = method.exception && method.exception.length ? ` throws ${method.exception.join(', ')}` : '';
return [
comment,
[pieces.join(' '), '(', args, ')', possibleException, ' ', '{'].filter(p => p).join(''),
'\n',
indentMultiline(method.implementation),
'\n',
'}',
]
.filter(p => p)
.join('');
}
FieldDefinition(node: FieldDefinitionNode, key?: number | string, parent?: any): string {
const typeString = (node.type as any) as string;
const originalFieldNode = parent[key] as FieldDefinitionNode;
const addOptionalSign = !this.config.avoidOptionals.object && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
const comment = transformComment((node.description as any) as string, 1);
return comment + indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name}${addOptionalSign ? '?' : ''}: ${typeString},`);
}
FieldDefinition(node: FieldDefinitionNode, key?: number | string, parent?: any): string {
const fieldDecorator = this.config.decoratorName.field;
let typeString = (node.type as any) as string;
const comment = transformComment((node.description as any) as string, 1);
const type = this.parseType(typeString);
const decorator = '\n' + indent(`@TypeGraphQL.${fieldDecorator}(type => ${type.isArray ? `[${type.type}]` : type.type}${type.isNullable ? ', { nullable: true }' : ''})`) + '\n';
typeString = this.fixDecorator(type, typeString);
return comment + decorator + indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name}!: ${typeString};`);
}
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string {
const originalFieldNode = parent[key] as FieldDefinitionNode;
const addOptionalSign = !this.config.avoidOptionals.inputValue && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
const comment = transformComment((node.description as any) as string, 1);
return comment + indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name}${addOptionalSign ? '?' : ''}: ${node.type},`);
}
FieldDefinition(node: FieldDefinitionNode): string {
const typeString = (node.type as any) as string;
const namePostfix = typeString.charAt(0) === '?' ? '?' : '';
const comment = transformComment((node.description as any) as string, 1);
return comment + indent(`${this.config.useFlowReadOnlyTypes ? '+' : ''}${node.name}${namePostfix}: ${typeString},`);
}
withComment(comment: string | StringValueNode | null): JavaDeclarationBlock {
if (comment) {
this._comment = transformComment(comment, 0);
}
return this;
}
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string {
const fieldDecorator = this.config.decoratorName.field;
let rawType = node.type as TypeNode | string;
const comment = transformComment((node.description as any) as string, 1);
const type = this.parseType(rawType);
const typeGraphQLType = type.isScalar && TYPE_GRAPHQL_SCALARS.includes(type.type) ? `TypeGraphQL.${type.type}` : type.type;
const decorator = '\n' + indent(`@TypeGraphQL.${fieldDecorator}(type => ${type.isArray ? `[${typeGraphQLType}]` : typeGraphQLType}${type.isNullable ? ', { nullable: true }' : ''})`) + '\n';
const nameString = (node.name as NameNode).kind ? (node.name as NameNode).value : node.name;
const typeString = (rawType as TypeNode).kind ? this.buildTypeString(type) : this.fixDecorator(type, rawType as string);
return comment + decorator + indent(`${this.config.immutableTypes ? 'readonly ' : ''}${nameString}!: ${typeString};`);
}
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string {
const originalFieldNode = parent[key] as FieldDefinitionNode;
const addOptionalSign = originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
const comment = transformComment((node.description as any) as string, 1);
return comment + indent(`${node.name}${addOptionalSign ? '?' : ''}: ${node.type},`);
}