Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clientState cache if provided in apollo-boost #3439

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/apollo-boost/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### vNext

- Use clientState cache if provided [#3439](https://github.com/apollographql/apollo-client/pull/3439)

## 1.1.4
- Map coverage to original source
- fix request parameter type [#3056](https://github.com/apollographql/apollo-client/pull/3056)
Expand Down
12 changes: 12 additions & 0 deletions packages/apollo-boost/src/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ describe('config', () => {

expect(client.cache.config.cacheRedirects).toEqual(cacheRedirects);
});

it('allows you to pass in cache in clientState', () => {
class MockCache {}

const mockCache = new MockCache();

const client = new ApolloClient({
clientState: { cache: mockCache, resolvers },
});

expect(client.cache instanceof MockCache).toEqual(true);
});
});
52 changes: 31 additions & 21 deletions packages/apollo-boost/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ export interface PresetConfig {

export default class DefaultClient<TCache> extends ApolloClient<TCache> {
constructor(config: PresetConfig) {
const cache =
config && config.cacheRedirects
? new InMemoryCache({ cacheRedirects: config.cacheRedirects })
: new InMemoryCache();
let cache = null;

if (config) {
if (config.clientState && config.clientState.cache) {
cache = config.clientState.cache;
} else if (config.cacheRedirects) {
cache = new InMemoryCache({ cacheRedirects: config.cacheRedirects });
} else {
cache = new InMemoryCache();
}
} else {
cache = new InMemoryCache();
}

const stateLink =
config && config.clientState
Expand All @@ -50,24 +59,25 @@ export default class DefaultClient<TCache> extends ApolloClient<TCache> {

const requestHandler =
config && config.request
? new ApolloLink((operation, forward) =>
new Observable(observer => {
let handle: any;
Promise.resolve(operation)
.then(oper => config.request(oper))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));
? new ApolloLink(
(operation, forward) =>
new Observable(observer => {
let handle: any;
Promise.resolve(operation)
.then(oper => config.request(oper))
.then(() => {
handle = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));

return () => {
if (handle) handle.unsubscribe;
};
})
return () => {
if (handle) handle.unsubscribe;
};
}),
)
: false;

Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Oleksandr Stubailo <sashko@Oleksandrs-MacBook-Pro.local>
Olivier Ricordeau <olivier@ricordeau.org>
Pavol Fulop <pavol.fulop@regex.sk>
Pavol Fulop <pavolfulop@gmail.com>
Pete Nykänen <pete.a.nykanen@gmail.com>
Rasmus Eneman <rasmus@eneman.eu>
Robert Dickert <robert.dickert@gmail.com>
Robin Ricard <ricard.robin@gmail.com>
Expand Down