index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const videoA = {
  2. id: 'a',
  3. title: 'Create a GraphQL schema',
  4. duration: 120,
  5. watched: true
  6. };
  7. const videoB = {
  8. id: 'b',
  9. title: 'Ember.js CLI',
  10. duration: 240,
  11. watched: false
  12. };
  13. const videos = [videoA, videoB];
  14. const createVideo = ({ title, duration, released }) => {
  15. const video = {
  16. id: (new Buffer(title, 'utf8')).toString('base64'),
  17. title,
  18. duration,
  19. released
  20. };
  21. videos.push(video);
  22. return video;
  23. };
  24. const getVideoById = (id) => new Promise((resolve) => {
  25. const [video] = videos.filter(currentVideo => currentVideo.id === id);
  26. resolve(video);
  27. });
  28. // Shortcut: we have the data immediately, so no promise.
  29. // const getVideos = () => videos;
  30. // General case: asynchronously load the videos
  31. const getVideos = () => new Promise(resolve => resolve(videos));
  32. const getObjectById = (type, id) => {
  33. const types = {
  34. video: getVideoById
  35. };
  36. return types[type](id);
  37. };
  38. exports.createVideo = createVideo;
  39. exports.getObjectById = getObjectById;
  40. exports.getVideoById = getVideoById;
  41. exports.getVideos = getVideos;