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

Add type/field policy code splitting docs section #6766

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/source/caching/advanced-topics.mdx
Expand Up @@ -366,3 +366,66 @@ persistCache({
```

For more advanced usage, such as persisting the cache when the app is in the background, and additional configuration options, please check the [README of `apollo-cache-persist`](https://github.com/apollographql/apollo-cache-persist).

## Code splitting

Depending on the complexity and size of your cache type/field policies, you might not always want to define them up front, when you create your initial `InMemoryCache` instance. If you have type/field policies that are only needed in a specific part of your application, you can leverage `InMemoryCache`'s `policies.addTypePolicies()` method to adjust your cache policy map at any point. This can be really useful when leveraging techniques like route based code-splitting, using something like `react-loadable`.
hwillson marked this conversation as resolved.
Show resolved Hide resolved

Let's say we're building a messaging app and have a `/stats` route that is used to return the total number of messages stored locally. If we use `react-loadable` to load our `Stats` component like:

```jsx
import Loadable from 'react-loadable';

import Loading from './components/Loading';

export const Stats = Loadable({
loader: () => import('./components/stats/Stats'),
loading: Loading,
});
```

and wait until our `Stats` component is called to add a new type/field policy (using `cache.policies.addTypePolicies()`):

```jsx:title=Stats.jsx
import React from "react";
import { gql, useApolloClient, useQuery } from "@apollo/client";
import gql from "graphql-tag";
hwillson marked this conversation as resolved.
Show resolved Hide resolved

const GET_MESSAGE_COUNT = gql`
{
messageCount @client {
total
}
}
`;

const newPolicy = {
Query: {
fields: {
messageCount() {
// calculate and return the number of messages stored locally ...
return {
total: 123,
};
},
},
},
};

export function Stats() {
const client = useApolloClient();
client.cache.policies.addTypePolicies(newPolicy);
Comment on lines +414 to +416
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid re-adding the policies every time the component is rendered. I realize the component function gives us access to the client/cache, but maybe we can come up with a better pattern here? Maybe keep a Set of cache instances that we've seen, and only add the policies when we haven't seen this cache before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing @benjamn, I'l adjust. Thanks!

Copy link

@ashubham ashubham Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to clarify the best practice here. We could use useEffect with no dependencies too.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how that relates to this comment (that the query gets fired before to policies are loaded, unless you use a lazy query).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link @coler-j, the effect may be the reason why I was seeing the failure in the issue I opened (as you linked). This is our hook that adds type policies: https://github.com/magento/pwa-studio/blob/develop/packages/peregrine/lib/hooks/useTypePolicies.js#L29.


const { loading, data: { messageCount } } = useQuery(GET_MESSAGE_COUNT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since data can be undefined, we either need to avoid destructuring it, or provide a default = {} expression.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix @benjamn - thanks!


if (loading) return "Loading ...";

return (
<p>
Total number of messages: {messageCount.total}
</p>
);
};
```

our type/field policy code will only be included in the bundle a user downloads when (if) they access `/stats`. It won't be included in the initial application bundle, which helps keep the size of our initial bundle down, and ultimately helps with download and application startup times.