MutationRef.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  16. public struct MutationRequest<Variable: OperationVariable>: OperationRequest {
  17. public var operationName: String
  18. public var variables: Variable?
  19. public init(operationName: String, variables: Variable? = nil) {
  20. self.operationName = operationName
  21. self.variables = variables
  22. }
  23. }
  24. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  25. public class MutationRef<ResultData: Decodable, Variable: OperationVariable>: OperationRef {
  26. public var request: any OperationRequest
  27. private var grpcClient: GrpcClient
  28. init(request: any OperationRequest, grpcClient: GrpcClient) {
  29. self.request = request
  30. self.grpcClient = grpcClient
  31. }
  32. public func execute() async throws -> OperationResult<ResultData> {
  33. let results = try await grpcClient.executeMutation(
  34. request: request as! MutationRequest<Variable>,
  35. resultType: ResultData.self
  36. )
  37. return results
  38. }
  39. }