// GraphQL
import {
    GraphQLID,
    GraphQLString
} from 'graphql';
// RethinkDB
import r from 'rethinkdb';
// Constants
import { DB_HOST, DB_PORT } from '../../constants/configurations.js';

let BugMutation = {
	'updateBug': {
		type: GraphQLString,
		description: 'edit a bug ',
        args: {
			data: {
				type: GraphQLString,
				description: 'new bug data'
			}
		},
		resolve: async (root, { data }) => {
			let connection = null,
				query = null;

			try {
				console.log('data:');
				console.log(data);
				let bug = JSON.parse(data);
				console.log('bug:');
				console.log(bug);
				let id = null;
				if(!!bug.id){
					id = bug.id;
					delete bug.id;
				}
				if(!id){
					return 'Fail to update bug!';
				}
				query = r.db('work_genius').table('bugs_review').get(id).update(bug);
				connection = await r.connect({ host: DB_HOST, port: DB_PORT });
				await query.run(connection);
				await connection.close();
			} catch (err) {
				return 'Fail to update bug!';
			}
			return 'Update bug successfully!';
		}
	}

};

export default BugMutation;
@@ -541,24 +541,160 @@
 arse(data);%0A
+%09%09%09%09//total_row is used for pagination, don't need to be insert into db%0A%09%09%09%09if('total_row' in bug)%7B%0A%09%09%09%09%09delete bug%5B'total_row'%5D;%0A%09%09%09%09%7D%0A
 %09%09%09%09console.

fix a bug: when updating bug, we don't need the property 'total_row'