![]() ![]() ![]() You don't modify the original cached object's data. If you return a different reference in your modifier function, you change which other cached object is contained in this field. Each reference points to its corresponding object in the cache by its cache ID. However, when you define a modifier function for a field that contains an object type or a list of objects, those objects are represented as references. ![]() For example, if you define a modifier function for an object's quantity field that has current value 5, your modifier function is passed the value 5. When you define a modifier function for a field that contains a scalar, an enum, or a list of these base types, the modifier function is passed the exact existing value for the field. If you don't provide a modifier function for a particular field, that field's cached value remains unchanged. Here's an example call to modify that modifies a name field to convert its value to upper case: It takes its associated field's current cached value as a parameter and returns whatever value should replace it. Optional broadcast and optimistic boolean values to customize behaviorĪ modifier function applies to a single field.A map of modifier functions to execute (one for each field to modify).The ID of a cached object to modify (which we recommend obtaining with cache.identify ).Watched queries can control what happens when they're invalidated by updates to the cache, by passing options like fetchPolicy and nextFetchPolicy to client.watchQuery or the useQuery hook.Ĭanonically documented in the API reference, the modify method takes the following parameters:.modify cannot write field s that do not already exist in the cache.modify circumvents any merge functions you've defined, which means that field s are always overwritten with exactly the values you specify.Like writeQuery and writeFragment, modify triggers a refresh of all active queries that depend on modified field s (unless you override this behavior by passing broadcast: false ).The modify method of InMemor圜ache enables you to directly modify the values of individual cached field s, or even delete fields entirely. Our board has 4 columns: one per "status.See the full API reference for cache.updateQuery and cache.updateFragment. To recap, we're pulling in JIRA issues to a Kanban board. When to Use Fragmentsīack to our JIRA example, I demonstrated precisely the sort of thing one should never do: making more than one GraphQL request to serve a single purpose. We've already seen how we can pass variables through our queries to receive contextual data: the next step is creating blocks of reusable code which may never change, which become the foundational building blocks of all future queries. This is where GraphQL Fragments come in to play. There's a core query being reused at the heart of our requests: we need to be dynamic enough to account for the fact that User 1 has checked off 13 different cuisines and strict delivery time windows, whereas User 2 doesn't give a shit. Yet, the information we're serving will always be restaurants. Aside from the obvious facts (residing in different locales), perhaps there's more metadata we can leverage from User 1's long-term app usage, versus User 2 who is a total noob to our knockoff app. Because we want our app to be intelligent, this means that circumstances in which User 1 makes a query are vastly different than User 2. Say we're building a GrubHub knockoff (we hate profits and love entering impenetrable parts of the market, it's not that uncommon really.) At its core, the information we're serving will always be restaurants we'll always want to return things like the restaurant address, name, rating, etc. Imagine a user cruising through your application, setting preferences, and arriving at core pieces of functionality under a content which is specific only to them. When building real applications consuming data via GraphQL, we usually don't know precisely the query we're going to want to run at run time. Last week we encountered a genuine scenario when working with GraphQL clients. ![]() Working With GraphQL Fragments and Mutations. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |