examples on how to extend and have custom delete and update mutations
It would be great to have some examples on how to extend db2graphql to have some custom update and delete mutations. Thanks!
Hi @golemus, we are in low effort on this package as active development, still I'd gladly review a PR about it!
Hi, sorry but I am quite a newbie in nodejs ( maybe I'll ask for some help) for example simply adding to db2g.js
async del(table, where) { if (!this.connection) throw new Error('Invalid Knex instance'); return this.connection(table).where(where).del(); }
del method becomes supported
and in the mysql demo I was using:
const tableName="mydemotable";
// Add delete mutations to the schema
schema += type Mutation { delete${tableName}(id: ID!): ${tableName} };
const realdbtableName="db_real_table";
resolvers.Mutation = {
...resolvers.Mutation,
[delete${tableName}]: async (parent, args) => {
console.log("delete", ${tableName} );
const result = await api.del(${realdbtableName}, { ID: args.id });
return result;
}
};
I could add support for a single delete based on ID abstractions for now are a bit beyond for me now
could also add update on a single table
adding to db2g.js
async update(table, where, data) { if (!this.connection) throw new Error('Invalid Knex instance'); return this.connection(table).where(where).update(data); }
then
static schema expansion example:
// Add update mutations to the schema schema += ` type Mutation { update${tableName}(id: ID!, input: updateinput!): ${tableName} }
input updateinput { username: String user_email: String display_name: String } `;
// add update mutation
resolvers.Mutation = {
...resolvers.Mutation,
[update${tableName}]: async (parent, { id, input }) => {
console.log("update", ${tableName}," id", id, " input", input );
const result = await api.update(${dbtableName}, { ID: id }, input);
return result;
}
};
but I need help to extend
I'd love to help but as mentioned me and taviroquai aren't going to be actively developing this package. We are just putting in security fixes and maintaining compatibility to make it work.
Still, I'd love to provide feedback and help in a PR, but I need to ask you to make the first step.
Definitely, I like the idea of having delete and update mutations!
@golemus as mentioned, I'll be glad to review a PR and help you with that in case you want to make the first step :)