Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. const CommentsPageWithData = graphql(CommentsPageQuery, {
  2. props({ data }) {
  3. // ...
  4. },
  5. options({ params }) {
  6. return {
  7. reducer: (previousResults, action, variables) => {
  8.  
  9. let newResults = previousResults;
  10.  
  11. // check if a document belongs to the current query
  12. const belongsToQuery = (documents) => { /* ... */ }
  13.  
  14. // check if a document already exists in a results object
  15. const existsInResults = (results, document) => { /* ... */ }
  16.  
  17. // remove a document from a results object, used by edit and remove cases below
  18. const removeFromResults = (results, document) => { /* ... */ }
  19.  
  20. // add document to a results object
  21. const addToResults = (results, document) => { /* ... */ }
  22.  
  23. // reorder results
  24. const reorderResults = (results) => => { /* ... */ }
  25.  
  26. switch (action.operationName) {
  27.  
  28. case "new":
  29.  
  30. // get new document
  31. const newDocument = action.result.data["new"];
  32.  
  33. // if new document belongs to current results, add it
  34. if (belongsToQuery(newDocument)) {
  35. newResults = addToResults(previousResults, newDocument);
  36. newResults = reorderResults(newResults);
  37. }
  38.  
  39. break;
  40.  
  41. case "edit":
  42.  
  43. // get edited document
  44. const editedDocument = action.result.data[editMutationName];
  45.  
  46. // see if it still belongs to the current results
  47. if (belongsToQuery(previousResults, editedDocument)) {
  48.  
  49. // if document wasn't already in results, add it
  50. if (!existsInResults(previousResults, editedDocument)) {
  51. newResults = addToResults(previousResults, editedDocument);
  52. }
  53.  
  54. // reorder results
  55. newResults = reorderResults(newResults, options.sort);
  56.  
  57. } else {
  58.  
  59. // if edited doesn't belong to current list anymore, remove it
  60. newResults = removeFromResults(previousResults, editedDocument);
  61.  
  62. }
  63.  
  64. break;
  65.  
  66. case "remove":
  67.  
  68. // get removed document
  69. const removedDocument = action.result.data[removeMutationName];
  70.  
  71. // remove it from results object
  72. newResults = removeFromResults(previousResults, removedDocument);
  73.  
  74. break;
  75.  
  76. default:
  77.  
  78. // default case: just return previous results unchanged
  79. return previousResults;
  80.  
  81. }
  82.  
  83. // copy over arrays explicitely to ensure new sort is taken into account
  84. return {
  85. [listResolverName]: [...newResults[listResolverName]],
  86. [totalResolverName]: newResults[totalResolverName],
  87. }
  88.  
  89. }
  90. };
  91. },
  92. })(CommentsPage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement