diff --git a/Sources/Apollo/GraphQLDependencyTracker.swift b/Sources/Apollo/GraphQLDependencyTracker.swift index b01b630b29..e32b182c8f 100644 --- a/Sources/Apollo/GraphQLDependencyTracker.swift +++ b/Sources/Apollo/GraphQLDependencyTracker.swift @@ -13,10 +13,13 @@ final class GraphQLDependencyTracker: GraphQLResultAccumulator { dependentKeys.insert(info.cachePath.joined) } - func accept(fieldEntry: Void, info: FieldExecutionInfo) { + func accept(childObject: Void, info: FieldExecutionInfo) { dependentKeys.insert(info.cachePath.joined) } + func accept(fieldEntry: Void, info: FieldExecutionInfo) { + } + func accept(fieldEntries: [Void], info: ObjectExecutionInfo) { } diff --git a/Sources/Apollo/GraphQLExecutor.swift b/Sources/Apollo/GraphQLExecutor.swift index 04ba2e2d43..2077d899ad 100644 --- a/Sources/Apollo/GraphQLExecutor.swift +++ b/Sources/Apollo/GraphQLExecutor.swift @@ -454,10 +454,11 @@ final class GraphQLExecutor { childExecutionInfo.resetCachePath(toCacheKey: cacheKeyForObject) } - return self.execute(selections: selections, - on: object, - info: childExecutionInfo, - accumulator: accumulator).map { $0 as! Accumulator.PartialResult } + return execute(selections: selections, + on: object, + info: childExecutionInfo, + accumulator: accumulator) + .map { try accumulator.accept(childObject: $0, info: fieldInfo) } } } diff --git a/Sources/Apollo/GraphQLMap.swift b/Sources/Apollo/GraphQLMap.swift index d488680c56..21adf543a3 100644 --- a/Sources/Apollo/GraphQLMap.swift +++ b/Sources/Apollo/GraphQLMap.swift @@ -2,6 +2,7 @@ import ApolloAPI #endif +#warning("TODO: Delete") public typealias GraphQLMap = [String: JSONEncodable?] fileprivate extension Dictionary where Key == String, Value == JSONEncodable? { diff --git a/Sources/Apollo/GraphQLResponseGenerator.swift b/Sources/Apollo/GraphQLResponseGenerator.swift index d603c56640..fac27dc24b 100644 --- a/Sources/Apollo/GraphQLResponseGenerator.swift +++ b/Sources/Apollo/GraphQLResponseGenerator.swift @@ -13,15 +13,19 @@ final class GraphQLResponseGenerator: GraphQLResultAccumulator { return list } + func accept(childObject: JSONObject, info: FieldExecutionInfo) throws -> JSONValue { + return childObject + } + func accept(fieldEntry: JSONValue, info: FieldExecutionInfo) -> (key: String, value: JSONValue) { return (info.responseKeyForField, fieldEntry) } - func accept(fieldEntries: [(key: String, value: JSONValue)], info: ObjectExecutionInfo) -> JSONValue { + func accept(fieldEntries: [(key: String, value: JSONValue)], info: ObjectExecutionInfo) -> JSONObject { return JSONObject(fieldEntries, uniquingKeysWith: { (_, last) in last }) } - func finish(rootValue: JSONValue) throws -> JSONObject { - return rootValue as! JSONObject + func finish(rootValue: JSONObject) throws -> JSONObject { + return rootValue } } diff --git a/Sources/Apollo/GraphQLResultAccumulator.swift b/Sources/Apollo/GraphQLResultAccumulator.swift index f375bc0db9..9ab4cfd333 100644 --- a/Sources/Apollo/GraphQLResultAccumulator.swift +++ b/Sources/Apollo/GraphQLResultAccumulator.swift @@ -7,6 +7,7 @@ protocol GraphQLResultAccumulator: AnyObject { func accept(scalar: JSONValue, info: FieldExecutionInfo) throws -> PartialResult func acceptNullValue(info: FieldExecutionInfo) throws -> PartialResult func accept(list: [PartialResult], info: FieldExecutionInfo) throws -> PartialResult + func accept(childObject: ObjectResult, info: FieldExecutionInfo) throws -> PartialResult func accept(fieldEntry: PartialResult, info: FieldExecutionInfo) throws -> FieldEntry func accept(fieldEntries: [FieldEntry], info: ObjectExecutionInfo) throws -> ObjectResult @@ -52,6 +53,11 @@ final class Zip2Accumulator PartialResult { + return (try accumulator1.accept(childObject: childObject.0, info: info), + try accumulator2.accept(childObject: childObject.1, info: info)) + } + func accept(fieldEntry: PartialResult, info: FieldExecutionInfo) throws -> FieldEntry { return (try accumulator1.accept(fieldEntry: fieldEntry.0, info: info), try accumulator2.accept(fieldEntry: fieldEntry.1, info: info)) @@ -107,6 +113,12 @@ final class Zip3Accumulator PartialResult { + return (try accumulator1.accept(childObject: childObject.0, info: info), + try accumulator2.accept(childObject: childObject.1, info: info), + try accumulator3.accept(childObject: childObject.2, info: info)) + } + func accept(fieldEntry: PartialResult, info: FieldExecutionInfo) throws -> FieldEntry { return (try accumulator1.accept(fieldEntry: fieldEntry.0, info: info), try accumulator2.accept(fieldEntry: fieldEntry.1, info: info), diff --git a/Sources/Apollo/GraphQLResultNormalizer.swift b/Sources/Apollo/GraphQLResultNormalizer.swift index 320636a272..27f88d8d39 100644 --- a/Sources/Apollo/GraphQLResultNormalizer.swift +++ b/Sources/Apollo/GraphQLResultNormalizer.swift @@ -15,11 +15,18 @@ final class GraphQLResultNormalizer: GraphQLResultAccumulator { return list } + func accept(childObject: CacheReference, info: FieldExecutionInfo) throws -> JSONValue { + return childObject + } + func accept(fieldEntry: JSONValue, info: FieldExecutionInfo) -> (key: String, value: JSONValue) { return (info.cacheKeyForField, fieldEntry) } - func accept(fieldEntries: [(key: String, value: JSONValue)], info: ObjectExecutionInfo) throws -> JSONValue { + func accept( + fieldEntries: [(key: String, value: JSONValue)], + info: ObjectExecutionInfo + ) throws -> CacheReference { let cachePath = info.cachePath.joined let object = JSONObject(fieldEntries, uniquingKeysWith: { (_, last) in last }) @@ -28,7 +35,7 @@ final class GraphQLResultNormalizer: GraphQLResultAccumulator { return CacheReference(key: cachePath) } - func finish(rootValue: JSONValue) throws -> RecordSet { + func finish(rootValue: CacheReference) throws -> RecordSet { return records } } diff --git a/Sources/Apollo/GraphQLSelectionSetMapper.swift b/Sources/Apollo/GraphQLSelectionSetMapper.swift index 41819056ff..a0bb3acd99 100644 --- a/Sources/Apollo/GraphQLSelectionSetMapper.swift +++ b/Sources/Apollo/GraphQLSelectionSetMapper.swift @@ -18,15 +18,22 @@ final class GraphQLSelectionSetMapper: GraphQLRes return list } + func accept(childObject: [String : Any?], info: FieldExecutionInfo) throws -> JSONValue? { + return childObject + } + func accept(fieldEntry: JSONValue?, info: FieldExecutionInfo) -> (key: String, value: JSONValue?) { return (info.responseKeyForField, fieldEntry) } - func accept(fieldEntries: [(key: String, value: JSONValue?)], info: ObjectExecutionInfo) throws -> ResponseDict { - return ResponseDict(data: .init(fieldEntries, uniquingKeysWith: { (_, last) in last })) + func accept( + fieldEntries: [(key: String, value: JSONValue?)], + info: ObjectExecutionInfo + ) throws -> [String: Any?] { + return .init(fieldEntries, uniquingKeysWith: { (_, last) in last }) } - func finish(rootValue: ResponseDict) -> SelectionSet { - return SelectionSet.init(data: rootValue) + func finish(rootValue: [String: Any?]) -> SelectionSet { + return SelectionSet.init(data: ResponseDict(rootValue)) } } diff --git a/Sources/ApolloAPI/CodegenV1/ResponseDict.swift b/Sources/ApolloAPI/CodegenV1/ResponseDict.swift index 17bac43c14..f4555df35e 100644 --- a/Sources/ApolloAPI/CodegenV1/ResponseDict.swift +++ b/Sources/ApolloAPI/CodegenV1/ResponseDict.swift @@ -3,7 +3,7 @@ public struct ResponseDict { let data: [String: Any?] - public init(data: [String: Any?]) { + public init(_ data: [String: Any?]) { self.data = data } @@ -17,17 +17,17 @@ public struct ResponseDict { public subscript(_ key: String) -> T { let objectData = data[key] as! [String: Any] - return T.init(data: ResponseDict(data: objectData)) + return T.init(data: ResponseDict(objectData)) } public subscript(_ key: String) -> T? { guard let objectData = data[key] as? [String: Any] else { return nil } - return T.init(data: ResponseDict(data: objectData)) + return T.init(data: ResponseDict(objectData)) } public subscript(_ key: String) -> [T] { let objectData = data[key] as! [[String: Any]] - return objectData.map { T.init(data: ResponseDict(data: $0)) } + return objectData.map { T.init(data: ResponseDict($0)) } } public subscript(_ key: String) -> GraphQLEnum {