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

Remaining AC3 documentation updates #6429

Merged
merged 53 commits into from
Jul 14, 2020
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1bc7aff
Adjust explore section to align with top level docs sections
hwillson Mar 2, 2020
9593336
Remove Pupstagram references
hwillson Mar 2, 2020
e03ed55
Update `new ApolloClient` examples to include a cache
hwillson Jul 13, 2020
c18ad5f
Replace intro `cacheRedirects` example with `TypePolicies`
hwillson Mar 2, 2020
4f75961
Updates to use the AC3 ready Coinbase example
hwillson Mar 3, 2020
d447d24
Wire updated example apps into Queries section
hwillson Mar 3, 2020
8130403
Adjust the intro mutations section to use cache.modify
hwillson Mar 4, 2020
29da64d
Prepare local state section for cache policies + reactive vars
hwillson Jun 10, 2020
264b4cc
Add local resolver deprecation warning to local resolvers section
hwillson Jun 11, 2020
220fdf6
Add reactive variables section
hwillson Jun 11, 2020
dbe5b5f
Initial querying local state content
hwillson Jun 11, 2020
6f0b340
Initialize the cache + local data query flow with field policies
hwillson Jun 11, 2020
bd81162
Add "Handling @client fields with field policies" section
hwillson Jun 15, 2020
97c600a
Add "Integrating `@client` into remote queries" section
hwillson Jun 16, 2020
4233175
Update `cache.evict()` examples to use `EvictOptions`
hwillson Jun 16, 2020
43e84de
Remove broken Meteor integration docs
hwillson Jun 16, 2020
b79a533
Update to the latest docs theme
hwillson Jun 17, 2020
43f02f5
Fix broken links
hwillson Jun 16, 2020
084be4e
Remove Apollo Component code samples
hwillson Jun 17, 2020
767ff4d
Remove no longer valid local state fragment link
hwillson Jun 17, 2020
7956b77
Add dangling references section
hwillson Jun 18, 2020
fc6b63a
Explain `cache.identify`
hwillson Jun 18, 2020
777ba26
Add modifying fields (cache.modify) section
hwillson Jun 19, 2020
4979f99
Updated "Handling `@client` fields with the cache" section
hwillson Jun 19, 2020
3b50bbf
Update "Using `@client` fields as variables" section
hwillson Jun 19, 2020
d7f5b26
New "Using reactive variables to track `@client` state" section
hwillson Jun 20, 2020
0471737
Re-arrange the local state section
hwillson Jun 23, 2020
eca608a
Fully replace `cacheRedirects` examples with field policies
hwillson Jun 25, 2020
22bd499
Remove broken query splitting section
hwillson Jun 25, 2020
9f5e066
Update to latest docs theme
hwillson Jun 30, 2020
d9a2955
New `InMemoryCache` API section
hwillson Jun 26, 2020
8bdf676
Edits to local state overview
Jun 30, 2020
fb90369
Incorporate review from hwillson
Jun 30, 2020
cdd7c89
Update to the latest docs theme
hwillson Jun 17, 2020
fee5638
Update to latest docs theme
hwillson Jun 25, 2020
20227d6
WIP on Managing state with field policies
Jul 1, 2020
905f1a3
More WIP on managing state with field policies
Jul 2, 2020
7ba4f68
Get local-only fields article ready for review
Jul 2, 2020
8cca447
Refactor some content
Jul 7, 2020
523cf24
In-progress edits to core caching articles
Jul 7, 2020
a6cead7
Clarify use of cache.identify
Jul 7, 2020
182f60d
Clarify local-only field content
Jul 7, 2020
7fbe7a1
Fold querying and updating into local-only fields article
Jul 8, 2020
1b4451f
Fix broken links
Jul 8, 2020
2524fa4
Updates to reactive variables article
Jul 9, 2020
fefe916
Fix a word
Jul 9, 2020
ba40a80
Miscellaneous IA and formatting cleanup
Jul 10, 2020
bdc71e1
Edits to AC intro and getting started
Jul 11, 2020
bf9d073
Add missing dependency for ink
Jul 11, 2020
3079d15
Fix broken links
Jul 13, 2020
97457cc
update link checking exceptions and add redirect
Jul 13, 2020
64c725b
Add some missing dependencies
Jul 13, 2020
69f2951
Add a redirect for 3.0 beta docs to root
trevorblades Jul 14, 2020
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
Prev Previous commit
Next Next commit
Add "Handling @client fields with field policies" section
  • Loading branch information
hwillson committed Jul 14, 2020
commit bd811621a8fd58f691eda528073e06a2fd56254b
65 changes: 65 additions & 0 deletions docs/source/local-state/field-policies-reactive-vars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,68 @@ Let's look at both of these steps more closely.

- Retrieving `@client` data with the help of read functions (step 1 above) is explained in [Handling `@client` fields with field policies](#handling-client-fields-with-field-policies).
- Loading `@client` data from the cache (step 2 above) is explained in [Handling `@client` fields with the cache](#handling-client-fields-with-the-cache).

### Handling `@client` fields with field policies

Apollo Client [field policies](../caching/cache-field-behavior) can be used to locally compute GraphQL query fields that use a `@client` directive. This means that instead of sending your full GraphQL query to a remote GraphQL endpoint, which then runs resolver functions against your query to populate and return a result set, Apollo Client can run locally defined `read` functions to compute the value of `@client` fields. Let's look at an example:

```js
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

const cache = new InMemoryCache({
typePolicies: {
Launch: {
fields: {
isInCart: {
read(_, { variables }) {
return localStorage.getItem('CART').includes(
variables.launchId
);
}
}
}
}
}
});

const client = new ApolloClient({
uri: 'http://localhost:4000/graphql',
cache,
});

const GET_LAUNCH_DETAILS = gql`
query LaunchDetails($launchId: ID!) {
launch(id: $launchId) {
isInCart @client
site
rocket {
type
}
}
}
`;

// ... run the query using client.query, a useQuery hook, etc.
```

Here when the `GET_LAUNCH_DETAILS` query is executed, Apollo Client looks for a field policy `read` function that is associated with the `isInCart` field. It finds one as we've defined a `read` function for the `isInCart` field in the `InMemoryCache` constructor. This `read` function is run, then the result is calculated and merged in with the rest of the query result (if a `read` function can't be found, Apollo Client will check the cache for a matching field - see [Local data query flow](#local-data-query-flow) for more details).

In the above example we defined and associated a `isInCart` `read` function with the `Launch` GraphQL object type. Let's look at the `isInCart` `read` function more closely:

```js
Launch: {
fields: {
isInCart: {
read(_, { variables }) {
return localStorage.getItem('CART').includes(
variables.launchId
);
}
}
}
}
```

The first parameter of the `isInCart` `read` function gives us access to a previously stored `isInCart` field value from the cache, if one exists. Since we don't need to look at the cache in this example, we've named this parameter `_`. The second parameter is an object that gives us access to several read function properties and helper functions, which are explained in the [`FieldPolicy` API reference](../caching/cache-field-behavior/#fieldpolicy-api-reference). In our case we're only interested in using the `launchId` variable passed into the query, so we'll pull this out of the `variables` property. Using the `launchId` we check to see if our `localStorage` based cart array contains the specified launch, returning `true` / `false` accordingly. The returned boolean is then incorporated back into the result of running the original query, making it look like the value of `isInCart` came back alongside the rest of the server returned data.

Just like resolvers on the server, field policies are extremely flexible. They can be used to perform any kind of local computation you want, before returning a result for the specified field. You can manually query (or write to) the cache in different ways, call other helper utilities or libraries to prepare/validate/clean data, track statistics, call into other data stores to prepare a result, and so much more.