apollo-ios: Fatal error ("Optional is only JSONEncodable if Wrapped is") when trying to update SqlNormalizedCache manually after a successful mutation operation

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
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Yep, that fixes the issue!!!

Nah, leave it open for now, I’ll keep annoying you about it from time to time 😇

@designatednerd: I started to dive into this, ran into a minor issue (that’s project-specific and I just need to push through), then got pulled into another issue altogether. This is still critical for our project and it’s next on my list, so I’ll get to it soon.

I’m happy to close this and re-open when I’ve had the chance to test, if that’s helpful.

I’ll test that here shortly and report back.