@@ -15,34 +15,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
15
15
16
16
17
17
18
- var asyncToGenerator = function ( fn ) {
19
- return function ( ) {
20
- var gen = fn . apply ( this , arguments ) ;
21
- return new Promise ( function ( resolve , reject ) {
22
- function step ( key , arg ) {
23
- try {
24
- var info = gen [ key ] ( arg ) ;
25
- var value = info . value ;
26
- } catch ( error ) {
27
- reject ( error ) ;
28
- return ;
29
- }
30
-
31
- if ( info . done ) {
32
- resolve ( value ) ;
33
- } else {
34
- return Promise . resolve ( value ) . then ( function ( value ) {
35
- step ( "next" , value ) ;
36
- } , function ( err ) {
37
- step ( "throw" , err ) ;
38
- } ) ;
39
- }
40
- }
41
18
42
- return step ( "next" ) ;
43
- } ) ;
44
- } ;
45
- } ;
46
19
47
20
var classCallCheck = function ( instance , Constructor ) {
48
21
if ( ! ( instance instanceof Constructor ) ) {
@@ -263,6 +236,12 @@ function omit(obj, properties) {
263
236
} , { } ) ;
264
237
}
265
238
239
+ function addGqlError ( error ) {
240
+ if ( error . graphQLErrors && error . graphQLErrors . length ) {
241
+ error . gqlError = error . graphQLErrors [ 0 ] ;
242
+ }
243
+ }
244
+
266
245
var SmartApollo = function ( ) {
267
246
function SmartApollo ( vm , key , options ) {
268
247
var autostart = arguments . length > 3 && arguments [ 3 ] !== undefined ? arguments [ 3 ] : true ;
@@ -420,8 +399,10 @@ var SmartApollo = function () {
420
399
}
421
400
} , {
422
401
key : 'nextResult' ,
423
- value : function nextResult ( ) {
424
- throw new Error ( 'Not implemented' ) ;
402
+ value : function nextResult ( result ) {
403
+ var error = result . error ;
404
+
405
+ if ( error ) addGqlError ( error ) ;
425
406
}
426
407
} , {
427
408
key : 'callHandlers' ,
@@ -477,6 +458,8 @@ var SmartApollo = function () {
477
458
} , {
478
459
key : 'catchError' ,
479
460
value : function catchError ( error ) {
461
+ addGqlError ( error ) ;
462
+
480
463
var catched = this . errorHandler ( error ) ;
481
464
482
465
if ( catched ) return ;
@@ -626,14 +609,10 @@ var SmartQuery = function (_SmartApollo) {
626
609
627
610
// Create observer
628
611
this . observer = this . vm . $apollo . watchQuery ( this . generateApolloOptions ( variables ) ) ;
629
-
630
- // Create subscription
631
- this . sub = this . observer . subscribe ( {
632
- next : this . nextResult . bind ( this ) ,
633
- error : this . catchError . bind ( this )
634
- } ) ;
635
612
}
636
613
614
+ this . startQuerySubscription ( ) ;
615
+
637
616
if ( this . options . fetchPolicy !== 'no-cache' ) {
638
617
var currentResult = this . maySetLoading ( ) ;
639
618
@@ -644,6 +623,17 @@ var SmartQuery = function (_SmartApollo) {
644
623
645
624
get ( SmartQuery . prototype . __proto__ || Object . getPrototypeOf ( SmartQuery . prototype ) , 'executeApollo' , this ) . call ( this , variables ) ;
646
625
}
626
+ } , {
627
+ key : 'startQuerySubscription' ,
628
+ value : function startQuerySubscription ( ) {
629
+ if ( this . sub && ! this . sub . closed ) return ;
630
+
631
+ // Create subscription
632
+ this . sub = this . observer . subscribe ( {
633
+ next : this . nextResult . bind ( this ) ,
634
+ error : this . catchError . bind ( this )
635
+ } ) ;
636
+ }
647
637
} , {
648
638
key : 'maySetLoading' ,
649
639
value : function maySetLoading ( ) {
@@ -661,6 +651,8 @@ var SmartQuery = function (_SmartApollo) {
661
651
} , {
662
652
key : 'nextResult' ,
663
653
value : function nextResult ( result ) {
654
+ get ( SmartQuery . prototype . __proto__ || Object . getPrototypeOf ( SmartQuery . prototype ) , 'nextResult' , this ) . call ( this , result ) ;
655
+
664
656
var data = result . data ,
665
657
loading = result . loading ;
666
658
@@ -676,7 +668,7 @@ var SmartQuery = function (_SmartApollo) {
676
668
} else if ( ! this . options . manual ) {
677
669
if ( typeof this . options . update === 'function' ) {
678
670
this . setData ( this . options . update . call ( this . vm , data ) ) ;
679
- } else if ( data [ this . key ] === undefined ) {
671
+ } else if ( typeof data [ this . key ] === ' undefined' && Object . keys ( data ) . length ) {
680
672
console . error ( 'Missing ' + this . key + ' attribute on result' , data ) ;
681
673
} else {
682
674
this . setData ( data [ this . key ] ) ;
@@ -699,6 +691,18 @@ var SmartQuery = function (_SmartApollo) {
699
691
value : function catchError ( error ) {
700
692
get ( SmartQuery . prototype . __proto__ || Object . getPrototypeOf ( SmartQuery . prototype ) , 'catchError' , this ) . call ( this , error ) ;
701
693
this . loadingDone ( ) ;
694
+ this . nextResult ( this . observer . currentResult ( ) ) ;
695
+ // The observable closes the sub if an error occurs
696
+ this . resubscribeToQuery ( ) ;
697
+ }
698
+ } , {
699
+ key : 'resubscribeToQuery' ,
700
+ value : function resubscribeToQuery ( ) {
701
+ var lastError = this . observer . getLastError ( ) ;
702
+ var lastResult = this . observer . getLastResult ( ) ;
703
+ this . observer . resetLastResults ( ) ;
704
+ this . startQuerySubscription ( ) ;
705
+ Object . assign ( this . observer , { lastError : lastError , lastResult : lastResult } ) ;
702
706
}
703
707
} , {
704
708
key : 'watchLoading' ,
@@ -900,6 +904,8 @@ var SmartSubscription = function (_SmartApollo) {
900
904
} , {
901
905
key : 'nextResult' ,
902
906
value : function nextResult ( data ) {
907
+ get ( SmartSubscription . prototype . __proto__ || Object . getPrototypeOf ( SmartSubscription . prototype ) , 'nextResult' , this ) . call ( this , data ) ;
908
+
903
909
if ( typeof this . options . result === 'function' ) {
904
910
this . options . result . call ( this . vm , data ) ;
905
911
}
@@ -1520,7 +1526,6 @@ var CApolloQuery = {
1520
1526
error : function error ( _error ) {
1521
1527
this . result . loading = false ;
1522
1528
this . result . error = _error ;
1523
- console . log ( this . $apollo . queries . query . observer . currentResult ( ) ) ;
1524
1529
this . $emit ( 'error' , _error ) ;
1525
1530
}
1526
1531
} ;
@@ -1545,7 +1550,8 @@ var CApolloQuery = {
1545
1550
var result = this . $scopedSlots . default ( {
1546
1551
result : this . result ,
1547
1552
query : this . $apollo . queries . query ,
1548
- isLoading : this . $apolloData . loading
1553
+ isLoading : this . $apolloData . loading ,
1554
+ gqlError : this . result && this . result . error && this . result . error . gqlError
1549
1555
} ) ;
1550
1556
if ( Array . isArray ( result ) ) {
1551
1557
result = result . concat ( this . $slots . default ) ;
@@ -1664,56 +1670,32 @@ var CApolloMutation = {
1664
1670
mutate : function mutate ( options ) {
1665
1671
var _this = this ;
1666
1672
1667
- return asyncToGenerator ( /*#__PURE__*/ regeneratorRuntime . mark ( function _callee ( ) {
1668
- var result ;
1669
- return regeneratorRuntime . wrap ( function _callee$ ( _context ) {
1670
- while ( 1 ) {
1671
- switch ( _context . prev = _context . next ) {
1672
- case 0 :
1673
- _this . loading = true ;
1674
- _this . error = null ;
1675
- _context . prev = 2 ;
1676
- _context . next = 5 ;
1677
- return _this . $apollo . mutate ( _extends ( {
1678
- mutation : _this . mutation ,
1679
- variables : _this . variables ,
1680
- optimisticResponse : _this . optimisticResponse ,
1681
- update : _this . update ,
1682
- refetchQueries : _this . refetchQueries
1683
- } , options ) ) ;
1684
-
1685
- case 5 :
1686
- result = _context . sent ;
1687
-
1688
- _this . $emit ( 'done' , result ) ;
1689
- _context . next = 13 ;
1690
- break ;
1691
-
1692
- case 9 :
1693
- _context . prev = 9 ;
1694
- _context . t0 = _context [ 'catch' ] ( 2 ) ;
1695
-
1696
- _this . error = _context . t0 ;
1697
- _this . $emit ( 'error' , _context . t0 ) ;
1698
-
1699
- case 13 :
1700
- _this . loading = false ;
1701
-
1702
- case 14 :
1703
- case 'end' :
1704
- return _context . stop ( ) ;
1705
- }
1706
- }
1707
- } , _callee , _this , [ [ 2 , 9 ] ] ) ;
1708
- } ) ) ( ) ;
1673
+ this . loading = true ;
1674
+ this . error = null ;
1675
+ this . $apollo . mutate ( _extends ( {
1676
+ mutation : this . mutation ,
1677
+ variables : this . variables ,
1678
+ optimisticResponse : this . optimisticResponse ,
1679
+ update : this . update ,
1680
+ refetchQueries : this . refetchQueries
1681
+ } , options ) ) . then ( function ( result ) {
1682
+ _this . $emit ( 'done' , result ) ;
1683
+ _this . loading = false ;
1684
+ } ) . catch ( function ( e ) {
1685
+ addGqlError ( e ) ;
1686
+ _this . error = e ;
1687
+ _this . $emit ( 'error' , e ) ;
1688
+ _this . loading = false ;
1689
+ } ) ;
1709
1690
}
1710
1691
} ,
1711
1692
1712
1693
render : function render ( h ) {
1713
1694
var result = this . $scopedSlots . default ( {
1714
1695
mutate : this . mutate ,
1715
1696
loading : this . loading ,
1716
- error : this . error
1697
+ error : this . error ,
1698
+ gqlError : this . error && this . error . gqlError
1717
1699
} ) ;
1718
1700
if ( Array . isArray ( result ) ) {
1719
1701
result = result . concat ( this . $slots . default ) ;
@@ -1730,9 +1712,38 @@ function hasProperty(holder, key) {
1730
1712
return typeof holder !== 'undefined' && holder . hasOwnProperty ( key ) ;
1731
1713
}
1732
1714
1733
- var launch = function launch ( ) {
1715
+ function proxyData ( ) {
1734
1716
var _this = this ;
1735
1717
1718
+ var apollo = this . $options . apollo ;
1719
+
1720
+ if ( apollo ) {
1721
+ var _loop = function _loop ( key ) {
1722
+ if ( key . charAt ( 0 ) !== '$' ) {
1723
+ var options = apollo [ key ] ;
1724
+ // Property proxy
1725
+ if ( ! options . manual && ! hasProperty ( _this . $options . props , key ) && ! hasProperty ( _this . $options . computed , key ) && ! hasProperty ( _this . $options . methods , key ) ) {
1726
+ Object . defineProperty ( _this , key , {
1727
+ get : function get$$1 ( ) {
1728
+ return _this . $data . $apolloData . data [ key ] ;
1729
+ } ,
1730
+ enumerable : true ,
1731
+ configurable : true
1732
+ } ) ;
1733
+ }
1734
+ }
1735
+ } ;
1736
+
1737
+ // watchQuery
1738
+ for ( var key in apollo ) {
1739
+ _loop ( key ) ;
1740
+ }
1741
+ }
1742
+ }
1743
+
1744
+ function launch ( ) {
1745
+ var _this2 = this ;
1746
+
1736
1747
var apolloProvider = this . $apolloProvider ;
1737
1748
1738
1749
if ( this . _apolloLaunched || ! apolloProvider ) return ;
@@ -1762,46 +1773,31 @@ var launch = function launch() {
1762
1773
// Apollo Data
1763
1774
Object . defineProperty ( this , '$apolloData' , {
1764
1775
get : function get$$1 ( ) {
1765
- return _this . $data . $apolloData ;
1776
+ return _this2 . $data . $apolloData ;
1766
1777
} ,
1767
1778
enumerable : true ,
1768
1779
configurable : true
1769
1780
} ) ;
1770
1781
1771
1782
// watchQuery
1772
-
1773
- var _loop = function _loop ( key ) {
1783
+ for ( var key in apollo ) {
1774
1784
if ( key . charAt ( 0 ) !== '$' ) {
1775
1785
var options = apollo [ key ] ;
1776
- // Property proxy
1777
- if ( ! options . manual && ! hasProperty ( _this , key ) && ! hasProperty ( _this . $props , key ) && ! hasProperty ( _this . $data , key ) ) {
1778
- Object . defineProperty ( _this , key , {
1779
- get : function get$$1 ( ) {
1780
- return _this . $data . $apolloData . data [ key ] ;
1781
- } ,
1782
- enumerable : true ,
1783
- configurable : true
1784
- } ) ;
1785
- }
1786
- _this . $apollo . addSmartQuery ( key , options ) ;
1786
+ this . $apollo . addSmartQuery ( key , options ) ;
1787
1787
}
1788
- } ;
1789
-
1790
- for ( var key in apollo ) {
1791
- _loop ( key ) ;
1792
1788
}
1793
1789
1794
1790
if ( apollo . subscribe ) {
1795
1791
Globals . Vue . util . warn ( 'vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.' ) ;
1796
1792
}
1797
1793
1798
1794
if ( apollo . $subscribe ) {
1799
- for ( var key in apollo . $subscribe ) {
1800
- this . $apollo . addSmartSubscription ( key , apollo . $subscribe [ key ] ) ;
1795
+ for ( var _key in apollo . $subscribe ) {
1796
+ this . $apollo . addSmartSubscription ( _key , apollo . $subscribe [ _key ] ) ;
1801
1797
}
1802
1798
}
1803
1799
}
1804
- } ;
1800
+ }
1805
1801
1806
1802
function defineReactiveSetter ( $apollo , key , value , deep ) {
1807
1803
if ( typeof value !== 'undefined' ) {
@@ -1875,6 +1871,7 @@ function install(Vue, options) {
1875
1871
}
1876
1872
} : { } , {
1877
1873
1874
+ beforeCreate : proxyData ,
1878
1875
created : launch ,
1879
1876
1880
1877
destroyed : function destroyed ( ) {
@@ -1899,7 +1896,7 @@ function install(Vue, options) {
1899
1896
ApolloProvider . install = install ;
1900
1897
1901
1898
// eslint-disable-next-line no-undef
1902
- ApolloProvider . version = "3.0.0-beta.13 " ;
1899
+ ApolloProvider . version = "3.0.0-beta.14 " ;
1903
1900
1904
1901
// Apollo provider
1905
1902
var ApolloProvider$1 = ApolloProvider ;
0 commit comments