@@ -2,12 +2,12 @@ import { ApolloConsumer, Mutation, Query, Subscription } from 'react-apollo';
2
2
import { BlueBase , BootOptions , createPlugin } from '@bluebase/core' ;
3
3
4
4
import { ApolloClient } from 'apollo-client' ;
5
- import { ApolloLink } from 'apollo-link' ;
6
5
import { InMemoryCache } from 'apollo-cache-inmemory' ;
7
6
import { createHttpLink } from 'apollo-link-http' ;
8
7
import withApolloProvider from './withApolloProvider' ;
9
8
10
9
let client : ApolloClient < { } > ;
10
+
11
11
export default createPlugin ( {
12
12
description : '🌍 A BlueBase Plugin that integrates Apollo GraphQL Client' ,
13
13
key : 'plugin-apollo' ,
@@ -27,35 +27,38 @@ export default createPlugin({
27
27
} ,
28
28
29
29
filters : {
30
- 'bluebase.boot.end' : async ( bootOptions : BootOptions , _ctx : any , BB : BlueBase ) => {
31
- const httpLinkOptions = BB . Configs . getValue ( 'plugin.apollo.httpLinkOptions' ) ;
32
- const clientOptions = BB . Configs . getValue ( 'plugin.apollo.clientOptions' ) ;
33
-
34
- const httpLink = createHttpLink ( httpLinkOptions ) ;
35
- const links = await BB . Filters . run ( 'plugin.apollo.links' , [ httpLink ] ) ;
36
- const cache = await BB . Filters . run ( 'plugin.apollo.cache' , new InMemoryCache ( ) ) ;
37
- // We Use global `client` thats why we check if client create then ignore
38
- if ( ! client ) {
39
- client = new ApolloClient ( {
40
- cache,
41
- link : ApolloLink . from ( links ) ,
42
- ...clientOptions ,
43
- } ) ;
44
- await BB . Filters . run ( 'plugin.apollo.client' , client ) ;
45
- }
30
+ 'bluebase.boot.end' : {
31
+ key : 'apollo-register' ,
32
+ value : async ( bootOptions : BootOptions , _ctx : any , BB : BlueBase ) => {
33
+ if ( ! client ) {
34
+ const httpLinkOptions = BB . Configs . getValue ( 'plugin.apollo.httpLinkOptions' ) ;
35
+ const clientOptions = BB . Configs . getValue ( 'plugin.apollo.clientOptions' ) ;
36
+
37
+ const httpLink = createHttpLink ( httpLinkOptions ) ;
38
+ const link = await BB . Filters . run ( 'plugin.apollo.link' , httpLink ) ;
39
+ const cache = await BB . Filters . run ( 'plugin.apollo.cache' , new InMemoryCache ( ) ) ;
46
40
47
- BB . Components . addHocs ( 'BlueBaseContent' , withApolloProvider ( client ) ) ;
41
+ client = new ApolloClient ( {
42
+ cache,
43
+ link,
44
+ ...clientOptions ,
45
+ } ) ;
46
+ await BB . Filters . run ( 'plugin.apollo.client' , client ) ;
47
+ }
48
48
49
- BB . Filters . register ( {
50
- event : 'bluebase.reset' ,
51
- key : 'apollo-reset' ,
52
- priority : 50 ,
53
- value : async ( ) => {
54
- await client . clearStore ( ) ;
55
- } ,
56
- } ) ;
49
+ BB . Components . addHocs ( 'BlueBaseContent' , withApolloProvider ( client ) ) ;
50
+
51
+ BB . Filters . register ( {
52
+ event : 'bluebase.reset' ,
53
+ key : 'apollo-reset' ,
54
+ priority : 50 ,
55
+ value : async ( ) => {
56
+ await client . clearStore ( ) ;
57
+ } ,
58
+ } ) ;
57
59
58
- return bootOptions ;
60
+ return bootOptions ;
61
+ } ,
59
62
} ,
60
63
} ,
61
64
} ) ;
0 commit comments