[3.4.16] Does the type-level merge:true policy deeply merge the object?

@apollo/client Version: 3.4.16

My question is :

Does the type-level merge:true policy deeply merge the object? Why the type policy added to ProcessorType could merge priceInfo but the type policy added to CoreType could not work?

The example extended from #5762.
Schema is like this:

const PriceInfoType = new GraphQLObjectType({
  name: 'PriceInfoType',
  fields: {
    rawPrice: {
      type: GraphQLString,
    }
  }
})
const ProcessorType = new GraphQLObjectType({
  name: 'ProcessorType',
  fields: {
    priceInfo: {
      type: PriceInfoType,
    },
    model: {
      type: GraphQLString,
    },
    clockSpeed: {
      type: GraphQLInt,
    }
  }
})

const CoreType = new GraphQLObjectType({
  name: 'CoreType',
  fields: {
    cpu: {
      type: ProcessorType,
    }
  }
}) 


const ComputerType = new GraphQLObjectType({
  name: 'ComputerType',
  fields: {
    id: {
      type: GraphQLID,
    },
    name: {
      type: GraphQLString,
    },
    core: {
      type: CoreType,
    }
  }
})

Given that we cache some data with a query (MODEL_AND_PRICE_QUERY) like this:

  query ModelAndPrice {
    computer {
      id
      core {
        cpu {
          priceInfo { 
            rawPrice
          }
          model
        }  
      }
    }
  }

and later in the program run a query (CLOCK_SPEED_QUERY) like this (with cache-first fetch policy):

  query ClockSpeed {
    computer {
      id
      core {
        cpu {
          clockSpeed
        }  
      }
    }
  }

When cache policy is like this:

typePolicies: {
   ProcessorType: {
     merge: true
   },
}, 

The cpu data (ProcessorType) could be deep merged, including its non-normalized field priceInfo(PriceInfoType)

When cache policy is added to the parent type CoreType, like this:

typePolicies: {
    CoreType: {
      merge: true
    },
  },

The cpu data (ProcessorType) could not be deep merged , with priceInfo(PriceInfoType) overwritten by query ClockSpeed.

How to reproduce the issue:
Here is a reproduction of the issue: GitHub - Fishfaceeast/apollo-client-issue-2 at 3.4.16

Checkout branch 3.4.16 and start the app.

Click Model&Price button - The program fetches data. Then Model and price data is dislayed
Click Clock speed button - The program fetches data. Model and price data is overwritten by clock speed data.
Click Model&Price again - Model and price data is gone from the cache. The program fetches data again.

1 Like

Hi @Fishfaceeast, thanks for posting to the community forum! I believe this is expected behavior based on @benjamn’s comment in the issue you linked: [3.0] Nested object inside the cache gets overwritten by subsequent queries · Issue #5762 · apollographql/apollo-client · GitHub .

Based on apollo-client/mergeDeep.ts at 6875d3ced43162557cbd507558dfbdbc37512a69 · apollographql/apollo-client · GitHub, It appears that the client does attempt to deep merge fields. My best guess at this time after investigating for a bit is that the shape of these queries containing non-normalized data plays a role. Defining a merge function may be the most durable solution in this scenario.