-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathGraphQLSelectionSetMapper.swift
More file actions
73 lines (58 loc) · 2.27 KB
/
GraphQLSelectionSetMapper.swift
File metadata and controls
73 lines (58 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#if !COCOAPODS
import ApolloMigrationAPI
#endif
/// An accumulator that maps executed data to create a `SelectionSet`.
@_spi(Execution)
public final class GraphQLSelectionSetMapper<T: SelectionSet>: GraphQLResultAccumulator {
let dataDictMapper: DataDictMapper
public var requiresCacheKeyComputation: Bool {
dataDictMapper.requiresCacheKeyComputation
}
public var handleMissingValues: DataDictMapper.HandleMissingValues {
dataDictMapper.handleMissingValues
}
public init(handleMissingValues: DataDictMapper.HandleMissingValues = .disallow) {
self.dataDictMapper = DataDictMapper(handleMissingValues: handleMissingValues)
}
public func accept(scalar: AnyHashable, info: FieldExecutionInfo) throws -> AnyHashable? {
try dataDictMapper.accept(scalar: scalar, info: info)
}
public func accept(customScalar: AnyHashable, info: FieldExecutionInfo) throws -> AnyHashable? {
try dataDictMapper.accept(customScalar: customScalar, info: info)
}
public func acceptNullValue(info: FieldExecutionInfo) -> AnyHashable? {
return DataDict._NullValue
}
public func acceptMissingValue(info: FieldExecutionInfo) throws -> AnyHashable? {
switch handleMissingValues {
case .allowForOptionalFields where info.field.type.isNullable: fallthrough
case .allowForAllFields:
return nil
default:
throw JSONDecodingError.missingValue
}
}
public func accept(list: [AnyHashable?], info: FieldExecutionInfo) -> AnyHashable? {
return list
}
public func accept(childObject: DataDict, info: FieldExecutionInfo) throws -> AnyHashable? {
return childObject
}
public func accept(fieldEntry: AnyHashable?, info: FieldExecutionInfo) -> (key: String, value: AnyHashable)? {
guard let fieldEntry = fieldEntry else { return nil }
return (info.responseKeyForField, fieldEntry)
}
public func accept(
fieldEntries: [(key: String, value: AnyHashable)],
info: ObjectExecutionInfo
) throws -> DataDict {
return DataDict(
data: .init(fieldEntries, uniquingKeysWith: { (_, last) in last }),
fulfilledFragments: info.fulfilledFragments,
deferredFragments: info.deferredFragments
)
}
public func finish(rootValue: DataDict, info: ObjectExecutionInfo) -> T {
return T.init(_dataDict: rootValue)
}
}