resolver.js 3.3 KB

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