Skip to content

Commit cc8a2de

Browse files
committed
Refactor scalar casting logic for lists
1 parent f28dff6 commit cc8a2de

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

apollo-ios/Sources/ApolloAPI/SelectionSet+Equatable.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ public extension SelectionSet {
104104
func addData(for type: Selection.Field.OutputType, inList: Bool = false) {
105105
switch type {
106106
case .scalar, .customScalar:
107-
fields[field.responseKey] = fieldData
107+
if inList {
108+
guard let listData = fieldData as? [AnyHashable] else {
109+
preconditionFailure("Expected list data for field: \(field)")
110+
}
111+
112+
fields[field.responseKey] = unwrapAnyHashable(list: listData) as FieldValue
113+
} else {
114+
fields[field.responseKey] = fieldData
115+
}
108116

109117
case let .nonNull(innerType):
110118
addData(for: innerType, inList: inList)
@@ -146,6 +154,22 @@ public extension SelectionSet {
146154
preconditionFailure("Expected list data to contain objects.")
147155
}
148156

157+
private func unwrapAnyHashable(
158+
list: [AnyHashable]
159+
) -> [FieldValue] {
160+
if let nestedList = list as? [[AnyHashable]] {
161+
return nestedList.map { self.unwrapAnyHashable(list: $0) as FieldValue }
162+
}
163+
164+
return list.map {
165+
guard let base = $0.base as? FieldValue else {
166+
preconditionFailure("Expected list data to contain objects.")
167+
}
168+
return base
169+
}
170+
171+
}
172+
149173
private func addConditionalSelections(
150174
_ selections: [Selection],
151175
to fields: inout [String: FieldValue]

0 commit comments

Comments
 (0)