@@ -42,4 +42,64 @@ public extension MutableSelectionSet where Fragments: FragmentContainer {
42
42
}
43
43
}
44
44
45
+ public extension MutableSelectionSet where Self: InlineFragment {
46
+
47
+ /// Function for mutating a conditional inline fragment on a mutable selection set.
48
+ ///
49
+ /// This function is the only supported way to mutate an inline fragment. Because setting the
50
+ /// value for an inline fragment that was not already present would result in fatal data
51
+ /// inconsistencies, inline fragments properties are get-only. However, mutating the properties of
52
+ /// an inline fragment that has been fulfilled is allowed. This function enables the described
53
+ /// functionality by checking if the fragment is fulfilled and, if so, calling the mutation body.
54
+ ///
55
+ /// - Parameters:
56
+ /// - keyPath: The `KeyPath` to the inline fragment to mutate the properties of
57
+ /// - transform: A closure used to apply mutations to the inline fragment's properties.
58
+ /// - Returns: A `Bool` indicating if the fragment was fulfilled.
59
+ /// If this returns `false`, the `transform` block will not be called.
60
+ @discardableResult
61
+ mutating func mutateIfFulfilled< T: InlineFragment > (
62
+ _ keyPath: KeyPath < Self , T ? > ,
63
+ _ transform: ( inout T ) -> Void
64
+ ) -> Bool where T. RootEntityType == Self . RootEntityType {
65
+ guard var fragment = self [ keyPath: keyPath] else {
66
+ return false
67
+ }
68
+
69
+ transform ( & fragment)
70
+ self . __data = fragment. __data
71
+ return true
72
+ }
73
+ }
74
+
45
75
public protocol MutableRootSelectionSet : RootSelectionSet , MutableSelectionSet { }
76
+
77
+ public extension MutableRootSelectionSet {
78
+
79
+ /// Function for mutating a conditional inline fragment on a mutable selection set.
80
+ ///
81
+ /// This function is the only supported way to mutate an inline fragment. Because setting the
82
+ /// value for an inline fragment that was not already present would result in fatal data
83
+ /// inconsistencies, inline fragments properties are get-only. However, mutating the properties of
84
+ /// an inline fragment that has been fulfilled is allowed. This function enables the described
85
+ /// functionality by checking if the fragment is fulfilled and, if so, calling the mutation body.
86
+ ///
87
+ /// - Parameters:
88
+ /// - keyPath: The `KeyPath` to the inline fragment to mutate the properties of
89
+ /// - transform: A closure used to apply mutations to the inline fragment's properties.
90
+ /// - Returns: A `Bool` indicating if the fragment was fulfilled.
91
+ /// If this returns `false`, the `transform` block will not be called.
92
+ @discardableResult
93
+ mutating func mutateIfFulfilled< T: InlineFragment > (
94
+ _ keyPath: KeyPath < Self , T ? > ,
95
+ _ transform: ( inout T ) -> Void
96
+ ) -> Bool where T. RootEntityType == Self {
97
+ guard var fragment = self [ keyPath: keyPath] else {
98
+ return false
99
+ }
100
+
101
+ transform ( & fragment)
102
+ self . __data = fragment. __data
103
+ return true
104
+ }
105
+ }
0 commit comments