Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async createOneToOneRelation(currentTable: string, gqlField: InputModelFieldContext, tableName: string):
Promise {
let fieldname = `${currentTable}Id`
if (gqlField.hasDirectives && gqlField.annotations.OneToOne.field) {
fieldname = gqlField.annotations.OneToOne.field;
}
if (!gqlField.isArray) {
// tslint:disable-next-line: no-parameter-reassignment
tableName = gqlField.type.toLowerCase();
const hasColumn = await this.dbConnection.schema.hasColumn(
tableName,
fieldname,
);
if (hasColumn) {
logger.info('skipping relation creation');
} else {
await this.dbConnection.schema.alterTable(
tableName,
(table: Knex.TableBuilder) => {
table
.integer(fieldname)
.unique()
.unsigned();
table
.foreign(fieldname)
.references('id')
.inTable(currentTable);
},
);
}
} else {
public async createManyToManyRelation(currentTable: string, gqlField: InputModelFieldContext):
Promise {
const newTable = `${currentTable}_${gqlField.type.toLowerCase()}`;
// tslint:disable-next-line: await-promise
const hasTable = await this.dbConnection.schema.hasTable(newTable);
if (gqlField.isArray) {
if (hasTable) {
logger.info('skipping rela tion creation');
} else {
const tableOne = gqlField.type.toLowerCase();
const tableTwo = currentTable;
const fieldOne = `${tableOne}Id`;
const fieldTwo = `${currentTable}Id`;
await this.dbConnection.schema.createTable(
newTable,
(table: Knex.TableBuilder) => {
table.increments();
table.integer(fieldOne).unsigned();
table
.foreign(fieldOne)
.references('id')
.inTable(tableOne);
table.integer(fieldTwo).unsigned();
table
public async createOneToManyRelation(currentTable: string, gqlField: InputModelFieldContext, tableName: string):
Promise {
let fieldname = `${currentTable}Id`
if (gqlField.hasDirectives && gqlField.annotations.OneToMany.field) {
fieldname = gqlField.annotations.OneToMany.field;
}
if (gqlField.isArray) {
// tslint:disable-next-line: no-parameter-reassignment
tableName = gqlField.type.toLowerCase();
// tslint:disable-next-line: await-promise
const hasColumn = await this.dbConnection.schema.hasColumn(
tableName,
fieldname,
);
if (hasColumn) {
logger.info('skipping relation creation');
} else {
await this.dbConnection.schema.alterTable(
tableName,
(table: Knex.TableBuilder) => {
table.integer(fieldname).unsigned();
table
.foreign(fieldname)
.references('id')
.inTable(currentTable);
},
);
}
} else {
throw new Error(
'Incorrect syntax declaration. Declaration should be an array.',
);
public generate() {
const resolvers = {
Query: {},
Mutation: {},
Subscription: {}
};
for (const resolverElement of this.inputContext) {
if (resolverElement.config.disableGen) {
continue;
}
const objectName = resolverElement.name.toLowerCase();
if (resolverElement.config.create) {
const resolverCreateField = getFieldName(resolverElement.name, GraphbackOperationType.CREATE);
// tslint:disable-next-line: no-any
resolvers.Mutation[resolverCreateField] = (parent: any, args: any, context: any) => {
return this.service.create(objectName, args.input, {
publishEvent: resolverElement.config.subCreate
}, context)
}
}
if (resolverElement.config.update) {
const updateField = getFieldName(resolverElement.name, GraphbackOperationType.UPDATE);
// tslint:disable-next-line: no-any
resolvers.Mutation[updateField] = (parent: any, args: any, context: any) => {
return this.service.update(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subUpdate
}, context)
}
}
return this.service.create(objectName, args.input, {
publishEvent: resolverElement.config.subCreate
}, context)
}
}
if (resolverElement.config.update) {
const updateField = getFieldName(resolverElement.name, GraphbackOperationType.UPDATE);
// tslint:disable-next-line: no-any
resolvers.Mutation[updateField] = (parent: any, args: any, context: any) => {
return this.service.update(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subUpdate
}, context)
}
}
if (resolverElement.config.delete) {
const deleteField = getFieldName(resolverElement.name, GraphbackOperationType.DELETE);
// tslint:disable-next-line: no-any
resolvers.Mutation[deleteField] = (parent: any, args: any, context: any) => {
return this.service.delete(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subDelete
}, context)
}
}
if (resolverElement.config.findAll) {
const findAllField = getFieldName(resolverElement.name, GraphbackOperationType.FIND_ALL, 's');
// tslint:disable-next-line: no-any
resolvers.Query[findAllField] = (parent: any, args: any, context: any) => {
return this.service.findAll(objectName, context)
}
}
if (resolverElement.config.find) {
for (const resolverElement of this.inputContext) {
if (resolverElement.config.disableGen) {
continue;
}
const objectName = resolverElement.name.toLowerCase();
if (resolverElement.config.create) {
const resolverCreateField = getFieldName(resolverElement.name, GraphbackOperationType.CREATE);
// tslint:disable-next-line: no-any
resolvers.Mutation[resolverCreateField] = (parent: any, args: any, context: any) => {
return this.service.create(objectName, args.input, {
publishEvent: resolverElement.config.subCreate
}, context)
}
}
if (resolverElement.config.update) {
const updateField = getFieldName(resolverElement.name, GraphbackOperationType.UPDATE);
// tslint:disable-next-line: no-any
resolvers.Mutation[updateField] = (parent: any, args: any, context: any) => {
return this.service.update(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subUpdate
}, context)
}
}
if (resolverElement.config.delete) {
const deleteField = getFieldName(resolverElement.name, GraphbackOperationType.DELETE);
// tslint:disable-next-line: no-any
resolvers.Mutation[deleteField] = (parent: any, args: any, context: any) => {
return this.service.delete(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subDelete
}, context)
}
}
resolvers.Mutation[deleteField] = (parent: any, args: any, context: any) => {
return this.service.delete(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subDelete
}, context)
}
}
if (resolverElement.config.findAll) {
const findAllField = getFieldName(resolverElement.name, GraphbackOperationType.FIND_ALL, 's');
// tslint:disable-next-line: no-any
resolvers.Query[findAllField] = (parent: any, args: any, context: any) => {
return this.service.findAll(objectName, context)
}
}
if (resolverElement.config.find) {
const findField = getFieldName(resolverElement.name, GraphbackOperationType.FIND, 's');
// tslint:disable-next-line: no-any
resolvers.Query[findField] = (parent: any, args: any, context: any) => {
return this.service.findBy(objectName, args.fields, context)
}
}
this.createSubscriptions(resolverElement, resolvers, objectName)
}
// Delete Mutations key if not needed.
if (Object.keys(resolvers.Mutation).length === 0) {
delete resolvers.Mutation;
}
// Delete Subscriptions key if not needed.
if (Object.keys(resolvers.Subscription).length === 0) {
publishEvent: resolverElement.config.subUpdate
}, context)
}
}
if (resolverElement.config.delete) {
const deleteField = getFieldName(resolverElement.name, GraphbackOperationType.DELETE);
// tslint:disable-next-line: no-any
resolvers.Mutation[deleteField] = (parent: any, args: any, context: any) => {
return this.service.delete(objectName, args.id, args.input, {
publishEvent: resolverElement.config.subDelete
}, context)
}
}
if (resolverElement.config.findAll) {
const findAllField = getFieldName(resolverElement.name, GraphbackOperationType.FIND_ALL, 's');
// tslint:disable-next-line: no-any
resolvers.Query[findAllField] = (parent: any, args: any, context: any) => {
return this.service.findAll(objectName, context)
}
}
if (resolverElement.config.find) {
const findField = getFieldName(resolverElement.name, GraphbackOperationType.FIND, 's');
// tslint:disable-next-line: no-any
resolvers.Query[findField] = (parent: any, args: any, context: any) => {
return this.service.findBy(objectName, args.fields, context)
}
}
this.createSubscriptions(resolverElement, resolvers, objectName)
}
export const runGeneration = async ({db, client, backend, silent }: CliFlags, cwd: string, generateConfig: GenerateConfig, schemaString: string) => {
const tasks: ListrTask[] = [];
if (backend || client) {
// Creates model context that is shared with all generators to provide results
const inputContext = graphQLInputContext.createModelContext(schemaString, generateConfig.graphqlCRUD)
if (backend) {
tasks.push({
title: 'Generating Backend Schema and Resolvers',
task: () => createBackendFiles(cwd, inputContext, generateConfig),
})
}
if (client) {
tasks.push({
title: 'Generating Client-side Operations',
task: () => createClientFiles(cwd, inputContext, generateConfig),
})
}
}
if (db) {
public async updateDatabaseResources(
context: DatabaseContextProvider,
types: InputModelTypeContext[],
changes: Change[]
): Promise {
if (changes.length > 0) {
logger.info(`Updating database schema`)
for (const change of changes) {
const parts = change.path.split('.');
const changedType = {
name: parts[0],
field: parts[1],
}
const gqlType = types.find((t: InputModelTypeContext) => t.name === changedType.name);
const tableName = context.getFieldName(gqlType);
if (change.type === ChangeType.FieldAdded) {
await this.addField(tableName, changedType.field, gqlType);