Skip to content

Commit d2afce3

Browse files
authored
Merge pull request #90 from BlueBaseJS/feature/http-link
Feature/http link
2 parents c27dc00 + f526200 commit d2afce3

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/index.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { ApolloConsumer, Mutation, Query, Subscription } from 'react-apollo';
22
import { BlueBase, BootOptions, createPlugin } from '@bluebase/core';
33

44
import { ApolloClient } from 'apollo-client';
5-
import { ApolloLink } from 'apollo-link';
65
import { InMemoryCache } from 'apollo-cache-inmemory';
76
import { createHttpLink } from 'apollo-link-http';
87
import withApolloProvider from './withApolloProvider';
98

109
let client: ApolloClient<{}>;
10+
1111
export default createPlugin({
1212
description: '🌍 A BlueBase Plugin that integrates Apollo GraphQL Client',
1313
key: 'plugin-apollo',
@@ -27,35 +27,38 @@ export default createPlugin({
2727
},
2828

2929
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());
4640

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+
}
4848

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+
});
5759

58-
return bootOptions;
60+
return bootOptions;
61+
},
5962
},
6063
},
6164
});

0 commit comments

Comments
 (0)