Skip to content

Commit

Permalink
Fix accumulators to accept child objects explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 21, 2021
1 parent 43f32ef commit 901e7bf
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 18 deletions.
5 changes: 4 additions & 1 deletion Sources/Apollo/GraphQLDependencyTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/Apollo/GraphQLExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Apollo/GraphQLMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ApolloAPI
#endif

#warning("TODO: Delete")
public typealias GraphQLMap = [String: JSONEncodable?]

fileprivate extension Dictionary where Key == String, Value == JSONEncodable? {
Expand Down
10 changes: 7 additions & 3 deletions Sources/Apollo/GraphQLResponseGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 12 additions & 0 deletions Sources/Apollo/GraphQLResultAccumulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,6 +53,11 @@ final class Zip2Accumulator<Accumulator1: GraphQLResultAccumulator, Accumulator2
try accumulator2.accept(list: list2, info: info))
}

func accept(childObject: ObjectResult, info: FieldExecutionInfo) throws -> 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))
Expand Down Expand Up @@ -107,6 +113,12 @@ final class Zip3Accumulator<Accumulator1: GraphQLResultAccumulator, Accumulator2
try accumulator3.accept(list: list3, info: info))
}

func accept(childObject: ObjectResult, info: FieldExecutionInfo) throws -> 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),
Expand Down
11 changes: 9 additions & 2 deletions Sources/Apollo/GraphQLResultNormalizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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
}
}
15 changes: 11 additions & 4 deletions Sources/Apollo/GraphQLSelectionSetMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ final class GraphQLSelectionSetMapper<SelectionSet: AnySelectionSet>: 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))
}
}
8 changes: 4 additions & 4 deletions Sources/ApolloAPI/CodegenV1/ResponseDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public struct ResponseDict {

let data: [String: Any?]

public init(data: [String: Any?]) {
public init(_ data: [String: Any?]) {
self.data = data
}

Expand All @@ -17,17 +17,17 @@ public struct ResponseDict {

public subscript<T: SelectionSet>(_ 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<T: SelectionSet>(_ 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<T: SelectionSet>(_ 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<T>(_ key: String) -> GraphQLEnum<T> {
Expand Down

0 comments on commit 901e7bf

Please sign in to comment.