Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,45 @@ extension GraphQLSelectionSet {
let jsonData = try encoder.encode(jsonObject)
let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
let optionalDictionary = decodedDictionary.mapValues { $0 as Any? }

self.init(snapshot: optionalDictionary)
let convertedDictionary = Self.convertFieldValues(
in: optionalDictionary, using: Self.selections
)
self.init(snapshot: convertedDictionary)
} else {
self.init(snapshot: [:])
}
}

private static func convertFieldValues(
in snapshot: Snapshot, using selections: [GraphQLSelection]
) -> Snapshot {
var result = snapshot
for selection in selections {
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
}
return result
}

private static func convertValue(_ value: Any, for outputType: GraphQLOutputType) -> Any? {
switch outputType {
case .scalar(let decodableType):
if type(of: value) == decodableType {
return value
}
return (try? decodableType.init(jsonValue: value)) ?? value
case .nonNull(let innerType):
return convertValue(value, for: innerType)
case .list(let innerType):
guard let array = value as? [Any] else { return value }
return array.map { convertValue($0, for: innerType) ?? $0 }
case .object(let selections):
guard let dict = value as? [String: Any] else { return value }
return convertFieldValues(in: dict.mapValues { $0 as Any? }, using: selections)
}
}
}

enum APISwiftJSONValue: Codable {
Expand Down Expand Up @@ -724,12 +757,45 @@ extension GraphQLSelectionSet {
let jsonData = try encoder.encode(jsonObject)
let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
let optionalDictionary = decodedDictionary.mapValues { $0 as Any? }

self.init(snapshot: optionalDictionary)
let convertedDictionary = Self.convertFieldValues(
in: optionalDictionary, using: Self.selections
)
self.init(snapshot: convertedDictionary)
} else {
self.init(snapshot: [:])
}
}

private static func convertFieldValues(
in snapshot: Snapshot, using selections: [GraphQLSelection]
) -> Snapshot {
var result = snapshot
for selection in selections {
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
}
return result
}

private static func convertValue(_ value: Any, for outputType: GraphQLOutputType) -> Any? {
switch outputType {
case .scalar(let decodableType):
if type(of: value) == decodableType {
return value
}
return (try? decodableType.init(jsonValue: value)) ?? value
case .nonNull(let innerType):
return convertValue(value, for: innerType)
case .list(let innerType):
guard let array = value as? [Any] else { return value }
return array.map { convertValue($0, for: innerType) ?? $0 }
case .object(let selections):
guard let dict = value as? [String: Any] else { return value }
return convertFieldValues(in: dict.mapValues { $0 as Any? }, using: selections)
}
}
}

enum APISwiftJSONValue: Codable {
Expand Down Expand Up @@ -2032,12 +2098,45 @@ extension GraphQLSelectionSet {
let jsonData = try encoder.encode(jsonObject)
let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
let optionalDictionary = decodedDictionary.mapValues { $0 as Any? }

self.init(snapshot: optionalDictionary)
let convertedDictionary = Self.convertFieldValues(
in: optionalDictionary, using: Self.selections
)
self.init(snapshot: convertedDictionary)
} else {
self.init(snapshot: [:])
}
}

private static func convertFieldValues(
in snapshot: Snapshot, using selections: [GraphQLSelection]
) -> Snapshot {
var result = snapshot
for selection in selections {
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
}
return result
}

private static func convertValue(_ value: Any, for outputType: GraphQLOutputType) -> Any? {
switch outputType {
case .scalar(let decodableType):
if type(of: value) == decodableType {
return value
}
return (try? decodableType.init(jsonValue: value)) ?? value
case .nonNull(let innerType):
return convertValue(value, for: innerType)
case .list(let innerType):
guard let array = value as? [Any] else { return value }
return array.map { convertValue($0, for: innerType) ?? $0 }
case .object(let selections):
guard let dict = value as? [String: Any] else { return value }
return convertFieldValues(in: dict.mapValues { $0 as Any? }, using: selections)
}
}
}

enum APISwiftJSONValue: Codable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,45 @@ extension GraphQLSelectionSet {
let jsonData = try encoder.encode(jsonObject)
let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
let optionalDictionary = decodedDictionary.mapValues { $0 as Any? }

self.init(snapshot: optionalDictionary)
let convertedDictionary = Self.convertFieldValues(
in: optionalDictionary, using: Self.selections
)
self.init(snapshot: convertedDictionary)
} else {
self.init(snapshot: [:])
}
}

private static func convertFieldValues(
in snapshot: Snapshot, using selections: [GraphQLSelection]
) -> Snapshot {
var result = snapshot
for selection in selections {
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
Comment on lines +116 to +119
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convertFieldValues(in:using:) only processes GraphQLField selections. Generated selections arrays also include GraphQLFragmentSpread, GraphQLTypeCondition, GraphQLBooleanCondition, and GraphQLTypeCase (see Swift codegen), so scalar conversion will be skipped for enum/scalar fields that are selected via fragments/inline fragments/type cases. This likely reintroduces the same decoding crash for enums inside fragments/type conditions. Consider recursively traversing these selection kinds (e.g., expanding fragment spreads to fragment.selections, recursing into selections for boolean/type conditions, and processing both default and all variants for type cases) so all reachable fields get converted.

Suggested change
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
switch selection {
case let field as GraphQLField:
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
case let fragmentSpread as GraphQLFragmentSpread:
result = convertFieldValues(
in: result,
using: type(of: fragmentSpread.fragment).selections
)
case let booleanCondition as GraphQLBooleanCondition:
result = convertFieldValues(
in: result,
using: booleanCondition.selections
)
case let typeCondition as GraphQLTypeCondition:
result = convertFieldValues(
in: result,
using: typeCondition.selections
)
case let typeCase as GraphQLTypeCase:
result = convertFieldValues(
in: result,
using: typeCase.default
)
for (_, variantSelections) in typeCase.variants {
result = convertFieldValues(
in: result,
using: variantSelections
)
}
default:
continue
}

Copilot uses AI. Check for mistakes.
}
return result
}

private static func convertValue(_ value: Any, for outputType: GraphQLOutputType) -> Any? {
switch outputType {
case .scalar(let decodableType):
if type(of: value) == decodableType {
return value
}
return (try? decodableType.init(jsonValue: value)) ?? value
case .nonNull(let innerType):
return convertValue(value, for: innerType)
case .list(let innerType):
guard let array = value as? [Any] else { return value }
return array.map { convertValue($0, for: innerType) ?? $0 }
case .object(let selections):
guard let dict = value as? [String: Any] else { return value }
return convertFieldValues(in: dict.mapValues { $0 as Any? }, using: selections)
}
}
}

enum APISwiftJSONValue: Codable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,45 @@ extension GraphQLSelectionSet {
let jsonData = try encoder.encode(jsonObject)
let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
let optionalDictionary = decodedDictionary.mapValues { $0 as Any? }

self.init(snapshot: optionalDictionary)
let convertedDictionary = Self.convertFieldValues(
in: optionalDictionary, using: Self.selections
)
self.init(snapshot: convertedDictionary)
} else {
self.init(snapshot: [:])
}
}

private static func convertFieldValues(
in snapshot: Snapshot, using selections: [GraphQLSelection]
) -> Snapshot {
var result = snapshot
for selection in selections {
guard let field = selection as? GraphQLField else { continue }
let key = field.responseKey
guard let value = result[key], let unwrapped = value else { continue }
result[key] = convertValue(unwrapped, for: field.type)
}
return result
}

private static func convertValue(_ value: Any, for outputType: GraphQLOutputType) -> Any? {
switch outputType {
case .scalar(let decodableType):
if type(of: value) == decodableType {
return value
}
return (try? decodableType.init(jsonValue: value)) ?? value
case .nonNull(let innerType):
return convertValue(value, for: innerType)
case .list(let innerType):
guard let array = value as? [Any] else { return value }
return array.map { convertValue($0, for: innerType) ?? $0 }
case .object(let selections):
guard let dict = value as? [String: Any] else { return value }
return convertFieldValues(in: dict.mapValues { $0 as Any? }, using: selections)
}
}
}

enum APISwiftJSONValue: Codable {
Expand Down
Loading