Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs(graphcache): Add docs for flexible key generation (#3019)

authored by

Phil Pluckthun and committed by
GitHub
e578fac3 7191074b

+31
+31
docs/graphcache/normalized-caching.md
··· 309 309 }); 310 310 ``` 311 311 312 + ### Flexible Key Generation 313 + 314 + In some cases, you may want to create a pattern for your key generation. For instance, you may want 315 + to say "create a special key for every type ending in `'Node'`. In such a case we recommend creating 316 + a small JS `Proxy` to take care of key generation for you and making the keys functional. 317 + 318 + ```js 319 + cacheExchange({ 320 + keys: new Proxy( 321 + { 322 + Image: () => null, 323 + }, 324 + { 325 + get(target, prop, receiver) { 326 + if (prop.endsWith('Node')) { 327 + return data => data.uid; 328 + } 329 + const fallback = data => data.uuid; 330 + return target[prop] || fallback; 331 + }, 332 + } 333 + ), 334 + }); 335 + ``` 336 + 337 + In the above example, we dynamically change the key generator depending on the typename. When 338 + a typename ends in `'Node'`, we return a key generator that uses the `uid` field. We still fall back 339 + to an object of manual key generation functions however. Lastly though, when a type doesn't have 340 + a predefined key generator, we change the default behavior from using `id` and `_id` fields to using 341 + `uuid` fields. 342 + 312 343 ## Non-Automatic Relations and Updates 313 344 314 345 While _Graphcache_ is able to store and update our entities in an in-memory relational data