Skip to content

Commit

Permalink
Fixing mutation class definitnion
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Jan 12, 2020
1 parent ad44c0a commit 7cfca8f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
38 changes: 27 additions & 11 deletions templates/swift/MutationStruct.swift.stencil
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
extension {{ structPrepared.name }} {
struct {{ mutationStruct.mutation.name }}{% if mutationStruct.mutation.referencedFragment %}<V: Fragment>{% endif %}: Mutation{% if mutationStruct.mutation.referencedFragment %} where V.UnderlyingType == {{ swiftType }}{% endif %} {
class {{ mutationStruct.mutation.name }}{% if mutationStruct.mutation.referencedFragment %}<V: Fragment>{% endif %}: Mutation{% if mutationStruct.mutation.referencedFragment %} where V.UnderlyingType == {{ swiftType }}{% endif %} {

{% if mutationStruct.mutation.referencedFragment %}
typealias Value = V
{% else %}
typealias Value = {{ swiftType }}
{% endif %}

@State private(set) var isLoading = false
@State private(set) var error: Error? = nil
@State private(set) var value: Value? = .none
@Published private var _isLoading = false
@Published private var _error: Error? = nil
@Published private var _value: Value? = .none

let api: {{ mutationStruct.mutation.api.name }}
private let api: {{ mutationStruct.mutation.api.name }}

var isLoading: AnyPublisher<Bool, Never> {
return $_isLoading.eraseToAnyPublisher()
}

var value: AnyPublisher<Value, Never> {
return $_value.compactMap { $0 }.eraseToAnyPublisher()
}

var error: AnyPublisher<Error, Never> {
return $_error.compactMap { $0 }.eraseToAnyPublisher()
}

init(api: {{ mutationStruct.mutation.api.name }}) {
self.api = api
}
}
}

Expand All @@ -23,15 +39,15 @@ extension {{ structPrepared.name }}.{{ mutationStruct.mutation.name }} {

@discardableResult
func commit({{ queryRendererArguments|codeArray|join:", " }}) -> Self {
isLoading = true
_isLoading = true
api.client.perform(mutation: Apollo{{ mutationStruct.mutation.api.name }}.{{ mutationStruct.mutation.name }}Mutation({{ queryArgumentAssignments|codeArray|join:", " }})) { result in
self.isLoading = false
self._isLoading = false
switch result {
case .success(let response):
case let .success(response):
guard let data = response.data else { return }
self.value = {{ expression }}
case .failure(let error):
self.error = error
self._value = {{ expression }}
case let .failure(error):
self._error = error
}
}
return self
Expand Down
7 changes: 6 additions & 1 deletion templates/swift/StructureAPI.swift.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Apollo
import Foundation
import SwiftUI
import Combine

// MARK: Basic API

Expand Down Expand Up @@ -31,6 +32,10 @@ extension Optional: Fragment where Wrapped: Fragment {

protocol Mutation {
associatedtype Value

var isLoading: AnyPublisher<Bool, Never> { get }
var value: AnyPublisher<Value, Never> { get }
var error: AnyPublisher<Error, Never> { get }
}

// MARK: - Basic API: Paths
Expand Down Expand Up @@ -169,7 +174,7 @@ private struct QueryRenderer<Query: GraphQLQuery, Content: View>: View {
@Published var isLoading: Bool = false
@Published var value: Query.Data? = nil
@Published var error: String? = nil
private var cancellable: Cancellable?
private var cancellable: Apollo.Cancellable?

deinit {
cancel()
Expand Down

0 comments on commit 7cfca8f

Please sign in to comment.