Skip to content

Fatal error ("Optional is only JSONEncodable if Wrapped is") when trying to update SqlNormalizedCache manually after a successful mutation operation #1305

@nateirwin

Description

@nateirwin

While using Apollo iOS 0.29.1, I appear to be running into an issue similar to the one reported here. I asked for help with this issue in Spectrum, and @designatednerd asked me to create a new issue.

I'm trying to update the SqlNormalizedCache (which is setup and working properly) using the result of a successful mutation operation:

apollo.perform(mutation: InsertTaskResponseMutation(taskId: 1, response: "")) { result in
    guard
        let resultData = try? result.get().data,
        let taskResponseDetails = resultData.insertTaskResponses?.returning.first?.fragments.taskResponseDetails
    else {
        return
    }

    apollo.store.withinReadWriteTransaction({ transaction in
        try! transaction.write(object: taskResponseDetails, withKey: "task_responses-\(taskResponseDetails.id)")
    })
}

This is throwing a fatal error in "JSONStandardTypeConversions.swift" at line 109:

Screen Shot 2020-07-08 at 12 01 31 PM

Printing the description of self:

Printing description of self:
▿ Optional<Any>
  - some : 07/08/2020

In this case, self is a property on the response property, which is a custom jsonb type. Here's the type alias:

public typealias jsonb = [String : Any?]

extension Dictionary: JSONDecodable {
    public init(jsonValue value: JSONValue) throws {
        guard let dictionary = value as? Dictionary else {
            throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
        }
        
        self = dictionary
    }
}

And here's the query:

query ChallengeTaskResponse($taskResponseId: Int) {
    challenge_responses(where: {id: {_eq: $taskResponseId}}) {
        ...ChallengeTaskResponseDetails
    }
}

The mutation:

mutation InsertTaskResponse($response: jsonb) {
    insert_task_responses(objects: {response: $response}) {
        returning {
            ...TaskResponseDetails
        }
    }
}

And, finally, the fragment that's used by both the query and the mutation:

fragment TaskResponseDetails on task_responses {
    id
    response // The 'jsonb' custom data type
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions