NerdWalletOSS / apollo-cache-policies

An extension of the Apollo 3 cache with support for advanced cache policies.
Apache License 2.0
156 stars 22 forks source link

Bugfix/empty variables #7

Closed danReynolds closed 4 years ago

danReynolds commented 4 years ago

Fields of a query that had arguments but no variables supplied would receive undefined variables in the policy event rather than {}. Since the empty object would be needed to retrieve the field and its values from the cache using an access like:

CreateEmployeeResponse: {
  onWrite: {
    EmployeesResponse: (
      { modify, readField },
      { fieldName, parent }
    ) => {
      // assuming the field createEmployee has empty arguments and is stored as createEmployee({})
      const createEmployeeResponse: any = readField({
        fieldName: parent.fieldName, // createEmployee
        from: parent.ref, // { __ref: "ROOT_QUERY" }
        args: parent.variables // Must be {} to read createEmployee({}) and not createEmployee
      });
    },
  },
},

Now, if the field the policy event is run on has arguments, we pass {} as the variables to the event so that it is able to access the field in the cache using the provided variables.