From 7ee85e248f8f848ca0e674673af4066f7257b1c1 Mon Sep 17 00:00:00 2001 From: Sahil Manchanda Date: Mon, 12 Jun 2023 13:25:20 +0530 Subject: [PATCH 1/6] Update cache-configuration.mdx - Added 'Customizing Type Policies' Segment --- docs/source/caching/cache-configuration.mdx | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index 68c670f1c69..c3ca24c1595 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -216,6 +216,29 @@ This code also has the following drawbacks: * It does nothing to protect against undefined object properties. * Accidentally using different key fields at different times can cause inconsistencies in the cache. +### Customizing Type Policies + +After creating an Apollo client object, you can use the `addTypePolicy` method to add or modify the type policy. + +Here is an example of how to use the `addTypePolicy` method: + +``` +cost client = new ApolloClient({ + uri: `https://api.example.com/graphql`, +}); + +//Add a type policy to the client object. +client.addTypePolicy({ + type: "Query". + fields: { + hello: { + resolver: () => "Hello, world!", + }, + }, +}); +``` +This code snippet first creates an ApolloClient object. Then, it uses, the `addTypePolicy` method to add a type policy to the client. The type policy specifies that the `hello` field on the `Query` type should return the string "Hello, world!". + ### Disabling normalization You can instruct the `InMemoryCache` _not_ to normalize objects of a particular type. This can be useful for metrics and other transient data that's identified by a timestamp and never receives updates. From 35f75e490ac6732c26e1a9efc5faa55c9d523963 Mon Sep 17 00:00:00 2001 From: Sahil Manchanda Date: Tue, 20 Jun 2023 18:18:58 +0530 Subject: [PATCH 2/6] Updated 'Customizing Type Policies' Segment Modified the changes as per the suggestions --- docs/source/caching/cache-configuration.mdx | 37 +++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index c3ca24c1595..61077af86f2 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -218,26 +218,43 @@ This code also has the following drawbacks: ### Customizing Type Policies -After creating an Apollo client object, you can use the `addTypePolicy` method to add or modify the type policy. +After creating an Apollo client object, you can use the `addTypePolicy` method to add or modify type policies. Here is an example of how to use the `addTypePolicy` method: ``` -cost client = new ApolloClient({ - uri: `https://api.example.com/graphql`, +const cache = new InMemoryCache({ + typePolicies: { + Person: { + fields: { + name: { + read(name = "UNKNOWN NAME") { + return name.toUpperCase();; + } + }, + }, + }, + }, }); -//Add a type policy to the client object. -client.addTypePolicy({ - type: "Query". - fields: { - hello: { - resolver: () => "Hello, world!", +// Add a type policy to the client object. +cache.policies.addTypePolicies({ + Person: { + fields: { + email: { + read(email = "unknown@example.com") { + return email; + }, + }, }, }, }); ``` -This code snippet first creates an ApolloClient object. Then, it uses, the `addTypePolicy` method to add a type policy to the client. The type policy specifies that the `hello` field on the `Query` type should return the string "Hello, world!". +The code creates an `InMemoryCache` with a custom type policy for the `Person` type. The type policy specifies that if the `name` field is not available in the cache, it should return a default value of "UNKNOWN NAME" and converts it to uppercase. + +Then, the code adds an additional type policy to the cache using `cache.policies.addTypePolicies`. This new type policy is related to the `Person` type and its `email` field. The custom read function specifies that if the email field is not available in the cache, it should return a default value of "unknown@example.com". + +Overall, the code sets up caching behaviors for the `Person` type, ensuring that default values are provided for the `name` and `email` fields if they are not present in the cache. ### Disabling normalization From 0184c6deaa67f68d33512402eaad60616188d7b5 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 20 Jun 2023 10:52:04 -0600 Subject: [PATCH 3/6] Add language to code block --- docs/source/caching/cache-configuration.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index 61077af86f2..c1e2422be77 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -222,7 +222,7 @@ After creating an Apollo client object, you can use the `addTypePolicy` method t Here is an example of how to use the `addTypePolicy` method: -``` +```ts const cache = new InMemoryCache({ typePolicies: { Person: { From edd372741e296202812635d2cac961973d5bdf18 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 20 Jun 2023 10:54:21 -0600 Subject: [PATCH 4/6] Adjust verbiage on intro paragraph --- docs/source/caching/cache-configuration.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index c1e2422be77..140c97d974c 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -218,9 +218,9 @@ This code also has the following drawbacks: ### Customizing Type Policies -After creating an Apollo client object, you can use the `addTypePolicy` method to add or modify type policies. +After creating an `InMemoryCache` instance, you can use the `addTypePolicies` method to add or modify type policies. -Here is an example of how to use the `addTypePolicy` method: +Here is an example of how to use the `addTypePolicies` method: ```ts const cache = new InMemoryCache({ From c5a8089fba05b7c2aae1e301ac372e1cf0268a48 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 20 Jun 2023 10:54:35 -0600 Subject: [PATCH 5/6] Update code comment for accuracy --- docs/source/caching/cache-configuration.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index 140c97d974c..2bf8663234c 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -237,7 +237,7 @@ const cache = new InMemoryCache({ }, }); -// Add a type policy to the client object. +// Add a type policy to the cache. cache.policies.addTypePolicies({ Person: { fields: { From e23c6ce34189897f57c8d4c6b238cda9230d1493 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 20 Jun 2023 11:00:39 -0600 Subject: [PATCH 6/6] Add space after code block --- docs/source/caching/cache-configuration.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/caching/cache-configuration.mdx b/docs/source/caching/cache-configuration.mdx index 2bf8663234c..3508449a22b 100644 --- a/docs/source/caching/cache-configuration.mdx +++ b/docs/source/caching/cache-configuration.mdx @@ -250,6 +250,7 @@ cache.policies.addTypePolicies({ }, }); ``` + The code creates an `InMemoryCache` with a custom type policy for the `Person` type. The type policy specifies that if the `name` field is not available in the cache, it should return a default value of "UNKNOWN NAME" and converts it to uppercase. Then, the code adds an additional type policy to the cache using `cache.policies.addTypePolicies`. This new type policy is related to the `Person` type and its `email` field. The custom read function specifies that if the email field is not available in the cache, it should return a default value of "unknown@example.com".