Guest User

Untitled

a guest
Apr 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import { omit } from 'lodash';
  2. import VueApollo from 'vue-apollo';
  3.  
  4. const VUE_APOLLO_QUERY_KEYWORDS = [
  5. 'variables',
  6. 'watch',
  7. 'update',
  8. 'result',
  9. 'error',
  10. 'loadingKey',
  11. 'watchLoading',
  12. 'skip',
  13. 'throttle',
  14. 'debounce',
  15. 'subscribeToMore',
  16. 'prefetch',
  17. 'manual',
  18. ];
  19.  
  20. VueApollo.prototype.prefetchQuery = prefetchQuery;
  21.  
  22. function prefetchQuery(queryOptions, context, client) {
  23. let variables;
  24.  
  25. // Client
  26. if (!client) {
  27. client = this.defaultClient;
  28. } else if (typeof client === 'string') {
  29. client = this.clients[client];
  30. if (!client) {
  31. throw new Error(`[vue-apollo] Missing client '${client}' in 'apolloProvider'`);
  32. }
  33. }
  34.  
  35. // Simple query
  36. if (!queryOptions.query) {
  37. queryOptions = {
  38. query: queryOptions,
  39. }
  40. } else {
  41. const { prefetch } = queryOptions;
  42. const prefetchType = typeof prefetch;
  43.  
  44. // Resolve variables
  45. if (prefetchType !== 'undefined') {
  46. let result;
  47. if (prefetchType === 'function') {
  48. result = prefetch(context);
  49. } else {
  50. result = prefetch;
  51. }
  52.  
  53. if (!result) {
  54. return Promise.resolve();
  55. } else if (prefetchType === 'boolean') {
  56. const optVariables = queryOptions.variables;
  57. if (typeof optVariables !== 'undefined') {
  58. // Reuse `variables` option with `prefetch: true`
  59. if (typeof optVariables === 'function') {
  60. variables = optVariables.call(context);
  61. } else {
  62. variables = optVariables;
  63. }
  64. } else {
  65. variables = undefined;
  66. }
  67. } else {
  68. variables = result;
  69. }
  70. }
  71. }
  72.  
  73. // Query
  74. if (typeof queryOptions.query === 'function') {
  75. queryOptions.query = queryOptions.query(context);
  76. }
  77. return new Promise((resolve, reject) => {
  78. const options = omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS);
  79. options.variables = variables;
  80. if (process.env.VUE_ENV === 'server') {
  81. options.fetchPolicy = 'network-only';
  82. }
  83. client.query(options).then(resolve, reject);
  84. });
  85. }
Add Comment
Please, Sign In to add comment