resolver-mongo.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const mongoose = require('mongoose');
  2. const helper = require('./helper');
  3. const WorldSchema = new mongoose.Schema({
  4. _id: Number,
  5. randomNumber: Number
  6. }, {
  7. collection: 'world'
  8. });
  9. const World = mongoose.model('world', WorldSchema);
  10. const FortuneSchema = new mongoose.Schema({
  11. _id: Number,
  12. message: String
  13. }, {
  14. collection: 'fortune'
  15. });
  16. const Fortune = mongoose.model('fortune', FortuneSchema);
  17. const toClientWorld = (world) => {
  18. if (world) {
  19. world.id = world._id;
  20. delete world._id;
  21. }
  22. return world;
  23. };
  24. async function getRandomWorld() {
  25. return toClientWorld(await World.findOne({_id: helper.randomizeNum()}).lean().exec());
  26. }
  27. // Methods
  28. async function arrayOfRandomWorlds(totalWorldsToReturn) {
  29. const totalIterations = helper.sanititizeTotal(totalWorldsToReturn);
  30. const promises = [];
  31. for (let i = 1; i <= totalIterations; i++) {
  32. promises.push(getRandomWorld());
  33. }
  34. return await Promise.all(promises);
  35. }
  36. async function getAndUpdateRandomWorld() {
  37. // it would be nice to use findOneAndUpdate here, but for some reason the test fails with it.
  38. const world = await World.findOne({_id: helper.randomizeNum()}).lean().exec();
  39. world.randomNumber = helper.randomizeNum();
  40. await World.updateOne({
  41. _id: world._id
  42. }, {
  43. $set: {
  44. randomNumber: world.randomNumber
  45. }
  46. }).exec();
  47. return toClientWorld(world);
  48. }
  49. async function updateRandomWorlds(totalToUpdate) {
  50. const totalIterations = helper.sanititizeTotal(totalToUpdate);
  51. const promises = [];
  52. for (let i = 1; i <= totalIterations; i++) {
  53. promises.push(getAndUpdateRandomWorld());
  54. }
  55. return await Promise.all(promises);
  56. }
  57. const sayHello = () => {
  58. return JSON.stringify({
  59. message: "Hello, World!"
  60. });
  61. };
  62. module.exports = {
  63. Query: {
  64. helloWorld: () => sayHello(),
  65. getAllWorlds: async () => toClientWorld(await World.find({}).lean().exec()),
  66. singleDatabaseQuery: async () => toClientWorld(await World.findOne({_id: helper.randomizeNum()}).lean().exec()),
  67. multipleDatabaseQueries: async (parent, args) => await arrayOfRandomWorlds(args.total),
  68. getWorldById: async (parent, args) => toClientWorld(await World.findById(args.id).lean().exec()),
  69. getAllFortunes: async () => toClientWorld(await Fortune.find({}).lean().exec()),
  70. getRandomAndUpdate: async (parent, args) => await updateRandomWorlds(args.total)
  71. },
  72. Mutation: {
  73. createWorld: async (parent, args) => {
  74. const randInt = helper.randomizeNum();
  75. return await World.create({_id: null, randomNumber: randInt});
  76. },
  77. updateWorld: async (parent, args) => {
  78. return World.updateOne({_id: args.id}, {
  79. randomNumber: args.randomNumber
  80. }).exec();
  81. }
  82. }
  83. }