Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Fields in the general type (e.g. Post). Both the identifying field and any others the db dictates will be required.
const fields = new Array();
// Fields in the update input type (e.g. UpdatePostInput). Only the identifying field will be required, any others will be optional.
const updateFields = new Array();
// Field in the create input type (e.g. CreatePostInput).
const createFields = new Array();
// The primary key, used to help generate queries and mutations
let primaryKey = '';
let primaryKeyType = '';
// Field Lists needed as context for auto-generating the Query Resolvers
const intFieldList = new Array();
const stringFieldList = new Array();
const formattedTableName = toUpper(tableName);
for (const columnDescription of columnDescriptions) {
// If a field is the primary key, save it.
if (columnDescription.Key == 'PRI') {
primaryKey = columnDescription.Field;
primaryKeyType = getGraphQLTypeFromMySQLType(columnDescription.Type);
} else {
/**
* If the field is not a key, then store it in the fields list.
* As we need this information later to generate query resolvers
*
* Currently we will only auto-gen query resolvers for the Int and String scalars
*/
const type = getGraphQLTypeFromMySQLType(columnDescription.Type);
if (type === 'Int') {
intFieldList.push(columnDescription.Field);
ResourceFactory.prototype.makeUpdateResolver = function (type, nameOverride) {
var fieldName = nameOverride ? nameOverride : graphql_transformer_common_1.graphqlName("update" + graphql_transformer_common_1.toUpper(type));
return new appSync_1.default.Resolver({
ApiId: cloudform_1.Fn.GetAtt(graphql_transformer_common_1.ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: cloudform_1.Fn.GetAtt(graphql_transformer_common_1.ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: 'Mutation',
RequestMappingTemplate: graphql_mapping_template_1.print(graphql_mapping_template_1.compoundExpression([
graphql_mapping_template_1.ifElse(graphql_mapping_template_1.ref(graphql_transformer_common_1.ResourceConstants.SNIPPETS.AuthCondition), graphql_mapping_template_1.compoundExpression([
graphql_mapping_template_1.set(graphql_mapping_template_1.ref('condition'), graphql_mapping_template_1.ref(graphql_transformer_common_1.ResourceConstants.SNIPPETS.AuthCondition)),
graphql_mapping_template_1.qref('$condition.put("expression", "$condition.expression AND attribute_exists(#id)")'),
graphql_mapping_template_1.qref('$condition.expressionNames.put("#id", "id")')
]), graphql_mapping_template_1.set(graphql_mapping_template_1.ref('condition'), graphql_mapping_template_1.obj({
expression: graphql_mapping_template_1.str("attribute_exists(#id)"),
expressionNames: graphql_mapping_template_1.obj({
"#id": graphql_mapping_template_1.str("id")
})
}))),
private getSubscriptions(types: TableContext[]): ObjectTypeDefinitionNode {
const fields = [];
for (const typeContext of types) {
const type = typeContext.tableTypeDefinition;
const formattedTypeValue = toUpper(type.name.value);
fields.push(
getOperationFieldDefinition(`onCreate${formattedTypeValue}`, [], getNamedType(`${type.name.value}`), [
getDirectiveNode(`create${formattedTypeValue}`),
])
);
}
return getTypeDefinition(fields, 'Subscription');
}
private makeUpdateRelationalResolver(type: string, mutationTypeName: string = 'Mutation') {
const fieldName = graphqlName('update' + toUpper(type));
const updateSql = `UPDATE ${type} SET $update WHERE ${this.typePrimaryKeyMap.get(type)}=$ctx.args.update${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}`;
let selectSql;
if (this.typePrimaryKeyTypeMap.get(type).includes('String')) {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=\'$ctx.args.update${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}\'`;
} else {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=$ctx.args.update${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}`;
}
const reqFileName = `${mutationTypeName}.${fieldName}.req.vtl`;
const resFileName = `${mutationTypeName}.${fieldName}.res.vtl`;
private makeCreateRelationalResolver(type: string, mutationTypeName: string = 'Mutation') {
const fieldName = graphqlName('create' + toUpper(type));
let createSql = `INSERT INTO ${type} $colStr VALUES $valStr`;
let selectSql;
if (this.typePrimaryKeyTypeMap.get(type).includes('String')) {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=\'$ctx.args.create${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}\'`;
} else {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=$ctx.args.create${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}`;
}
const reqFileName = `${mutationTypeName}.${fieldName}.req.vtl`;
const resFileName = `${mutationTypeName}.${fieldName}.res.vtl`;
const reqTemplate = print(
private makeCreateRelationalResolver(type: string, mutationTypeName: string = 'Mutation') {
const fieldName = graphqlName('create' + toUpper(type));
let createSql = `INSERT INTO ${type} $colStr VALUES $valStr`;
let selectSql;
if (this.typePrimaryKeyTypeMap.get(type).includes('String')) {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=\'$ctx.args.create${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}\'`;
} else {
selectSql = `SELECT * FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=$ctx.args.create${toUpper(
type
)}Input.${this.typePrimaryKeyMap.get(type)}`;
}
const reqFileName = `${mutationTypeName}.${fieldName}.req.vtl`;
const resFileName = `${mutationTypeName}.${fieldName}.res.vtl`;
const reqTemplate = print(
compoundExpression([
set(ref('cols'), list([])),
set(ref('vals'), list([])),
forEach(ref('entry'), ref(`ctx.args.create${toUpper(type)}Input.keySet()`), [
set(ref('discard'), ref(`cols.add($entry)`)),
set(ref('discard'), ref(`vals.add("'$ctx.args.create${toUpper(type)}Input[$entry]'")`)),
]),
set(ref('valStr'), ref('vals.toString().replace("[","(").replace("]",")")')),
private protectOnUpdateSubscription(ctx: TransformerContext, rules: AuthRule[],
parent: ObjectTypeDefinitionNode, level: string, onUpdate?: string[]) {
if (onUpdate) {
onUpdate.forEach( (name) => {
this.addSubscriptionResolvers(ctx, rules, parent, level, name)
})
} else {
this.addSubscriptionResolvers(ctx, rules, parent,
level, graphqlName(ON_UPDATE_FIELD + toUpper(parent.name.value)))
}
}