resolver.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const Sequelize = require('sequelize');
  2. const helper = require('./helper');
  3. // MySQL
  4. const connection = {
  5. database: 'hello_world',
  6. username: 'benchmarkdbuser',
  7. password: 'benchmarkdbpass',
  8. host: 'tfb-database',
  9. dialect: 'mysql'
  10. }
  11. const sequelize = new Sequelize(
  12. connection.database,
  13. connection.username,
  14. connection.password, {
  15. host: connection.host,
  16. dialect: connection.dialect,
  17. logging: false
  18. },
  19. );
  20. const World = sequelize.define('world', {
  21. id: {
  22. autoIncrement: true,
  23. type: 'Sequelize.INTEGER',
  24. primaryKey: true
  25. },
  26. randomNumber: {
  27. type: 'Sequelize.INTEGER'
  28. }
  29. }, {
  30. timestamps: false,
  31. freezeTableName: true
  32. });
  33. const Fortune = sequelize.define('fortune', {
  34. id: {
  35. type: 'Sequelize.INTEGER',
  36. primaryKey: true
  37. },
  38. message: {
  39. type: 'Sequelize.STRING'
  40. }
  41. }, {
  42. timestamps: false,
  43. freezeTableName: true
  44. });
  45. async function arrayOfRandomWorlds(totalWorldToReturn) {
  46. var totalIterations = helper.sanititizeTotal(totalWorldToReturn);
  47. var arr = [];
  48. return new Promise(async (resolve, reject) => {
  49. for(var i = 0; i < totalIterations; i++) {
  50. let world = await World.findByPk(helper.randomizeNum());
  51. arr.push(world);
  52. }
  53. if(arr.length == totalIterations) {
  54. resolve(arr);
  55. }
  56. });
  57. };
  58. async function updateRandomWorlds(totalToUpdate) {
  59. const total = helper.sanititizeTotal(totalToUpdate);
  60. var arr = [];
  61. return new Promise(async (resolve, reject) => {
  62. for(var i = 0; i < total; i++) {
  63. const world = await World.findByPk(helper.randomizeNum());
  64. world.updateAttributes({
  65. randomNumber: helper.randomizeNum()
  66. })
  67. arr.push(world);
  68. }
  69. if(arr.length == total) {
  70. resolve(arr);
  71. }
  72. });
  73. };
  74. const sayHello = () => {
  75. var helloWorld = new Object;
  76. helloWorld.message = "Hello, World!";
  77. return JSON.stringify(helloWorld);
  78. };
  79. module.exports = {
  80. Query: {
  81. helloWorld: () => sayHello(),
  82. getAllWorlds: async() => await World.findAll(),
  83. singleDatabaseQuery: async() => await World.findByPk(helper.randomizeNum()),
  84. multipleDatabaseQueries: async(parent, args) => await arrayOfRandomWorlds(args.total),
  85. getWorldById: async(parent, args) => await World.findByPk(args.id),
  86. getAllFortunes: async() => await Fortune.findAll(),
  87. getRandomAndUpdate: async(parent, args) => await updateRandomWorlds(args.total)
  88. },
  89. Mutation: {
  90. createWorld: async(parent, args) => {
  91. let randInt = Math.floor(Math.random() * 1000) + 1;
  92. return await World.create({ id: null, randomNumber: randInt });
  93. },
  94. updateWorld: async(parent, args) => {
  95. return await World.update({id: args.id, randomNumber: args.randomNumber});
  96. }
  97. }
  98. }