-
Notifications
You must be signed in to change notification settings - Fork 743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal error: Optional is only JSONEncodable if Wrapped is #1255
Comments
Can you share what the generated code is for this query, please? I'm mostly curious to see what type |
It's an Int```public let operationName: String = "UncategorizedTransactions" public var fromDate: Timestamp public init(fromDate: Timestamp, toDate: Timestamp) { public var variables: GraphQLMap? { public struct Data: GraphQLSelectionSet {
}
|
Interesting - the only thing in there being generated as an optional is Is there a chance you could either share your project with me or get me a project that reproduces this in isolation? I'd really need to dig in a bit here to figure out what's going on. Email ellen at apollographql dot com. |
OK! So after some wrangling and switching from Carthage to SPM I was able to see what's causing this crash: When the code to compute the default cache key is getting hit, the optional it's trying to unwrap is a
This is extremely deep in our caching mechanism and I'm going to have to ping @martijnwalraven for some help on how to debug and fix this since he wrote this code and understands it a LOT better than I do. In the meantime, I would recommend using the |
This fixes apollographql#1255 by adding an extension method to `Optional` that implements `GraphQLInputValue.evaluate(with:)` by invoking that same method on the wrapped value. Without it, we'd be treating an optional `GraphQLVariable` as an optional `JSONEncodable`, which leads to a runtime crash because `GraphQLVariable` does not in fact conform to `JSONEncodable`. All this should really be cleaned up by adopting conditional conformances (which weren't available when this was originally written), but that turns out to lead to quite the rabbit hole of issues so that'll have to wait.
I got this "Optional is only JSONEncodable if Wrapped is" fatal error in the following scenario:
A fix is to also add the Feature request: please have the SDK learn how to log types that do not adopt |
@groue Can you clarify something here: Is the custom scalar's |
Hello @designatednerd. It is a fully custom type: a "base64 data" that consumes a json string from which it base64-decodes its Data property. EDIT: it looks like: extension GraphQL {
public struct Base64: JSONDecodable {
public let data: Data
public init(jsonValue value: JSONValue) throws {
let string = try String(jsonValue: value)
guard let data = Data(base64Encoded: string) else {
throw JSONDecodingError.couldNotConvert(value: value, to: Base64.self)
}
self.data = data
}
}
} |
Got it - thank you for the clarification! |
@designatednerd Do we need a new issue to track this? |
@AnthonyMDev Yeah that's probably a good idea, I'll open one |
Hey hey.
Getting this error:
Query which creates the situation:
Example of playground response
It doesn't crash if the query is like so:
Also it doesn't crash if the query is this:
The text was updated successfully, but these errors were encountered: