How can we avoid sending hardcoded format while reading fragment

Is there a better way to send the non hardcode key as define below

ref: Direct cache access - Apollo GraphQL Docs - Apollo Doc

let data = try transaction.readObject(
    ofType: HeroDetails.self,
    withKey: "Hero:123"
  )

The reason is when we update the sdk, we missed to change the cacheKey format and we hit into issue. Trying to avoid such things in future.

We set the format of cacheKeyInfo as below, as defined in doc.

cacheKeyInfo(for type: ApolloAPI.Object, object: ApolloAPI.ObjectData) -> CacheKeyInfo? {
   guard let id = object["id"] as? String else {
      return nil
    }
    return CacheKeyInfo(id: id)
}

Hi @P_1433 - unfortunately there is no easy way to do this right now. Forming that cache key is a bit of implicit knowledge in knowing the format of the key (such as this) and it’s not particularly safe as a String either.

We recognize that we need to build a better way for users to derive a cache key in a type safe way. I suggest creating an issue to track this, it would benefit other users experiencing the same problem too.

Thanks for the information @calvincestari , will create an enhancement ticket.

1 Like