Writing cache data

Hi,

According Direct cache access - Apollo GraphQL Docs, I’m trying to write some data in my local cache on a fragment.

fragment MutableUserAvatar on User {
      avatar
      rhythmColor
    }

avatar is a custom scalar named URI with following implementation:

public typealias URI = URL

extension URI: CustomScalarType {
    public init(_jsonValue value: JSONValue) throws {
        guard let string = value as? String else {
            throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
        }

        guard let url = URL(string: string) else {
            throw JSONDecodingError.couldNotConvert(value: string, to: URL.self)
        }
        self = url
    }

    public var _jsonValue: JSONValue {
        absoluteString
    }
}

rhythmColor is an enum.

When writing the cache, I got that exception.

In: SQLiteSerialization.serialize(fields: Record.Fields)
Invalid type in JSON write (__SwiftValue)

It seems that Apollo is not using the “string” version of properties when encoding.
I have to write something like that to be sure it can be encoded:

store.withinReadWriteTransaction { transaction in
            try transaction.updateObject(ofType: MutableUserAvatar.self, withKey: key) { (data: inout MutableUserAvatar) in
                data.__data._data["avatar"] = url.absoluteString
                data.__data._data["rhythmColor"] = data.rhythmColor?.rawValue
            }
        }

Is it correct?
I hope it was easier.

Thanks for your help.
Benoit

Update. Fragment is, of course, defined as local cache mutation:

fragment MutableUserAvatar on User @apollo_client_ios_localCacheMutation {
	avatar
	rhythmColor
}