Skip to content

Commit

Permalink
Merge pull request #88 from BlueBaseJS/develop
Browse files Browse the repository at this point in the history
feat: ApolloClient is now saved as singleton (#87)
  • Loading branch information
artalat authored Dec 28, 2019
2 parents ad2dc25 + c27dc00 commit a3afed0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';
import withApolloProvider from './withApolloProvider';

let client: ApolloClient<{}>;
export default createPlugin({
description: '🌍 A BlueBase Plugin that integrates Apollo GraphQL Client',
key: 'plugin-apollo',
Expand All @@ -33,12 +34,15 @@ export default createPlugin({
const httpLink = createHttpLink(httpLinkOptions);
const links = await BB.Filters.run('plugin.apollo.links', [httpLink]);
const cache = await BB.Filters.run('plugin.apollo.cache', new InMemoryCache());

const client: ApolloClient<{}> = new ApolloClient({
cache,
link: ApolloLink.from(links),
...clientOptions,
});
// We Use global `client` thats why we check if client create then ignore
if (!client) {
client = new ApolloClient({
cache,
link: ApolloLink.from(links),
...clientOptions,
});
await BB.Filters.run('plugin.apollo.client', client);
}

BB.Components.addHocs('BlueBaseContent', withApolloProvider(client));

Expand Down
3 changes: 0 additions & 3 deletions src/withApolloProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { ApolloClient } from 'apollo-client';
import { ApolloProvider } from 'react-apollo';

export default (client: ApolloClient<{}>) => (App: React.ComponentType) => (props: any) => {

return (
<ApolloProvider client={client}>
<App {...props} />
</ApolloProvider>
);
};


0 comments on commit a3afed0

Please sign in to comment.