Repository: apollographql/apollo-client

Problem statement:
FieldFunctionOptions return incorrect variables
<!--
  Thanks for filing an issue on Apollo Client!

  Please make sure that you include the following information to ensure that your issue is actionable.

  If you don't follow the template, your issue may end up being closed without anyone looking at it carefully, because it is not actionable for us without the information in this template.

  **PLEASE NOTE:** Feature requests and non-bug related discussions are no longer managed in this repo. Feature requests should be opened in https://github.com/apollographql/apollo-feature-requests.
-->

**Intended outcome:**
<!--
What you were trying to accomplish when the bug occurred, and as much code as possible related to the source of the problem.
-->

Prior to 3.5.0 and according to the doc, we can add a local-only field and utilize the policy file to populate the data by using the `read` and `readField` functions if necessary.

Code may look something like this:
```
fields: {
  isCanada: {
    read(_, { readField }) {
      return readField("name") === "Canada";
    }
  },
  name: {
    read(name, { variables }) {
      return name;
    }
  }
}
```
The `variables` in the `read` function should always return the actual variable passed to this particular query. So something like:
```
{
  code: "CA"
}
```
**Actual outcome:**
<!--
A description of what actually happened, including a screenshot or copy-paste of any related error messages, logs, or other output that might be related. Places to look for information include your browser console, server console, and network logs. Please avoid non-specific phrases like “didn’t work” or “broke”.
-->

Instead of returning the actual variables, it returns other metadata like:
<img width="221" alt="image" src="https://user-images.githubusercontent.com/7050316/172903626-434f3813-554b-4767-a552-6b026cf20817.png">

The actual variables are in `variables.variables` now

**How to reproduce the issue:**
<!--
If possible, please create a reproduction using https://github.com/apollographql/react-apollo-error-template and link to it here. If you prefer an in-browser way to create reproduction, try: https://codesandbox.io/s/github/apollographql/react-apollo-error-template

Instructions for how the issue can be reproduced by a maintainer or contributor. Be as specific as possible, and only mention what is necessary to reproduce the bug. If possible, try to isolate the exact circumstances in which the bug occurs and avoid speculation over what the cause might be.
-->

Set up a local field and the read function for that field. In that read function, have it access another field using `readField` function.  Set up another read function for the field being accessed, then console log the variables.

https://codesandbox.io/s/quizzical-panka-x9v60j?file=/src/index.js

Sorry I know I'm probably not supposed to share speculation but this is what I've found:
In 3.5.0, it passes the `context` object to `normalizeReadFieldOptions` function and later `options.variables` would essentially equal to that `context` object
https://github.com/apollographql/apollo-client/blob/538b79594d02189f511622d8fed85b679d69dfd3/src/cache/inmemory/policies.ts#L915

In 3.4.13, `options.variables` would equal to the `variables` object
https://github.com/apollographql/apollo-client/blob/92141607ff6c650145350c222a2434cd7008e4b5/src/cache/inmemory/policies.ts#L884
There was probably a reason I wasn't aware of but just want to share what I've found, thank you

**Versions**
<!--
Run the following command in your project directory, and paste its (automatically copied to clipboard) results here:

`npx envinfo@latest --preset apollo --clipboard`
-->
System:
    OS: macOS 12.3.1
  Binaries:
    Node: 14.19.3 - /usr/local/opt/node@14/bin/node
    npm: 6.14.17 - /usr/local/opt/node@14/bin/npm
  Browsers:
    Chrome: 102.0.5005.61
    Edge: 102.0.1245.33
    Firefox: 101.0
    Safari: 15.4
  npmPackages:
    @apollo/client: ^3.5.0 => 3.5.0



Hints:

Patch:
diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts
--- a/src/cache/inmemory/policies.ts
+++ b/src/cache/inmemory/policies.ts
@@ -912,7 +912,7 @@ function makeFieldFunctionOptions(
     canRead,
     readField<T>() {
       return policies.readField<T>(
-        normalizeReadFieldOptions(arguments, objectOrReference, context),
+        normalizeReadFieldOptions(arguments, objectOrReference, variables),
         context,
       );
     },


Test patch:
diff --git a/src/cache/inmemory/__tests__/policies.ts b/src/cache/inmemory/__tests__/policies.ts
--- a/src/cache/inmemory/__tests__/policies.ts
+++ b/src/cache/inmemory/__tests__/policies.ts
@@ -1650,6 +1650,80 @@ describe("type policies", function () {
       expect(cache.extract(true)).toEqual(expectedExtraction);
     });
 
+    it("should return correct variables in read function", function () {
+      const cache = new InMemoryCache({
+        typePolicies: {
+          Country: {
+            fields: {
+              isCanada: {
+                read(_, { readField }) {
+                  return readField("name") === "CA";
+                }
+              },
+              name: {
+                read(_, { variables }) {
+                  return variables?.code;
+                }
+              }
+            }
+          }
+        }
+      });
+
+      cache.writeQuery({
+        query: gql`
+          query Countries($code: ID!) {
+            country(code: $code) {
+              name
+            }
+          }
+        `,
+        data: {
+          country: {
+            __typename: "Country",
+            name: "CA",
+          },
+        },
+        variables: {
+          code: "CA",
+        },
+      });
+
+      const expectedExtraction = {
+        ROOT_QUERY: {
+          __typename: "Query",
+          "country({\"code\":\"CA\"})": {
+            __typename: "Country",
+            name: "CA",
+          },
+        },
+      };
+
+      expect(cache.extract(true)).toEqual(expectedExtraction);
+
+      const expectedResult = {
+        country: {
+          __typename: "Country",
+          name: "CA",
+          isCanada: true,
+        },
+      };
+
+      expect(cache.readQuery({
+        query: gql`
+          query Countries($code: ID!) {
+            country(code: $code) {
+              name
+              isCanada @client
+            }
+          }
+        `,
+        variables: {
+          code: "CA",
+        },
+      })).toEqual(expectedResult);
+    });
+
     it("read and merge can cooperate through options.storage", function () {
       const cache = new InMemoryCache({
         typePolicies: {