Skip to content

Commit

Permalink
Local Cache Mutation - Mutate Fragments (#2309)
Browse files Browse the repository at this point in the history
* Support Local Cache Mutations of Inline Fragments

* Support Local Cache Mutations of Named Fragments
  • Loading branch information
AnthonyMDev authored Jun 10, 2022
1 parent e0fea41 commit d545b86
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 16 deletions.
18 changes: 12 additions & 6 deletions Sources/ApolloAPI/LocalCacheMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ public extension LocalCacheMutation {
}
}

public protocol MutableSelectionSet: AnySelectionSet {}

public protocol MutableRootSelectionSet: RootSelectionSet, MutableSelectionSet {
public protocol MutableSelectionSet: SelectionSet {
var data: DataDict { get set }
}

extension MutableRootSelectionSet {

@inlinable public var __typename: String {
public extension MutableSelectionSet {
@inlinable var __typename: String {
get { data["__typename"] }
set { data["__typename"] = newValue }
}
}

public extension MutableSelectionSet where Fragments: FragmentContainer {
@inlinable var fragments: Fragments {
get { Self.Fragments(data: data) }
set { data._data = newValue.data._data}
}
}

public protocol MutableRootSelectionSet: RootSelectionSet, MutableSelectionSet {}

16 changes: 8 additions & 8 deletions Sources/ApolloAPI/SelectionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public protocol SelectionSet: AnySelectionSet, Hashable {

extension SelectionSet {

public static var schema: SchemaConfiguration.Type { Schema.self }
@inlinable public static var schema: SchemaConfiguration.Type { Schema.self }

var __objectType: Object.Type? { Schema.objectType(forTypename: __typename) }
@usableFromInline var __objectType: Object.Type? { Schema.objectType(forTypename: __typename) }

@inlinable public var __typename: String { data["__typename"] }

Expand All @@ -72,7 +72,7 @@ extension SelectionSet {
/// - Warning: This function is not supported for use outside of generated call sites.
/// Generated call sites are guaranteed by the GraphQL compiler to be safe.
/// Unsupported usage may result in unintended consequences including crashes.
public func _asInlineFragment<T: SelectionSet>(
@inlinable public func _asInlineFragment<T: SelectionSet>(
if conditions: Selection.Conditions? = nil
) -> T? where T.Schema == Schema {
guard let conditions = conditions else {
Expand All @@ -82,30 +82,30 @@ extension SelectionSet {
return conditions.evaluate(with: data._variables) ? _asType() : nil
}

private func _asType<T: SelectionSet>() -> T? where T.Schema == Schema {
@usableFromInline func _asType<T: SelectionSet>() -> T? where T.Schema == Schema {
guard let __objectType = __objectType,
__objectType._canBeConverted(to: T.__parentType) else { return nil }

return T.init(data: data)
}

public func _asInlineFragment<T: SelectionSet>(
@inlinable public func _asInlineFragment<T: SelectionSet>(
if conditions: [Selection.Condition]
) -> T? where T.Schema == Schema {
_asInlineFragment(if: Selection.Conditions([conditions]))
}

public func _asInlineFragment<T: SelectionSet>(
@inlinable public func _asInlineFragment<T: SelectionSet>(
if condition: Selection.Condition
) -> T? where T.Schema == Schema {
_asInlineFragment(if: Selection.Conditions(condition))
}

public func hash(into hasher: inout Hasher) {
@inlinable public func hash(into hasher: inout Hasher) {
hasher.combine(data)
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
@inlinable public static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.data == rhs.data
}
}
Expand Down
7 changes: 5 additions & 2 deletions Tests/ApolloInternalTestHelpers/MockLocalCacheMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ open class MockLocalCacheMutationFromSubscription<SelectionSet: MutableRootSelec
override open class var operationType: GraphQLOperationType { .subscription }
}

public protocol MockMutableRootSelectionSet: MutableRootSelectionSet {}
public protocol MockMutableRootSelectionSet: MutableRootSelectionSet
where Schema == MockSchemaConfiguration {}

public extension MockMutableRootSelectionSet {
static var schema: SchemaConfiguration.Type { MockSchemaConfiguration.self }
static var __parentType: ParentType { .Object(Object.self) }
}

public protocol MockMutableInlineFragment: MutableSelectionSet, InlineFragment
where Schema == MockSchemaConfiguration {}
Loading

0 comments on commit d545b86

Please sign in to comment.