Skip to content

Commit 3ef7b42

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios/' changes from e46fb72f..89e8b1af
89e8b1af Add mutateIfFulfilled functions (#608) git-subtree-dir: apollo-ios git-subtree-split: 89e8b1afa0241a191ef066b1a998ecbca386e1e7
1 parent 9c925ae commit 3ef7b42

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Sources/ApolloAPI/LocalCacheMutation.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,64 @@ public extension MutableSelectionSet where Fragments: FragmentContainer {
4242
}
4343
}
4444

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+
4575
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

Comments
 (0)