OperationsManager.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. class OperationsManager {
  17. private var grpcClient: GrpcClient
  18. init(grpcClient: GrpcClient) {
  19. self.grpcClient = grpcClient
  20. }
  21. func queryRef<ResultDataType: Decodable,
  22. VariableType: OperationVariable>(for request: QueryRequest<VariableType>,
  23. with resultType: ResultDataType
  24. .Type,
  25. publisher: ResultsPublisherType = .auto)
  26. -> any ObservableQueryRef {
  27. switch publisher {
  28. case .auto, .observableMacro:
  29. if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
  30. return QueryRefObservation<ResultDataType, VariableType>(
  31. request: request,
  32. dataType: resultType,
  33. grpcClient: self.grpcClient
  34. ) as! (any ObservableQueryRef)
  35. } else {
  36. return QueryRefObservableObject<ResultDataType, VariableType>(
  37. request: request,
  38. dataType: resultType,
  39. grpcClient: grpcClient
  40. ) as! (any ObservableQueryRef)
  41. }
  42. case .observableObject:
  43. return QueryRefObservableObject<ResultDataType, VariableType>(
  44. request: request,
  45. dataType: resultType,
  46. grpcClient: grpcClient
  47. ) as! (any ObservableQueryRef)
  48. }
  49. }
  50. func mutationRef<ResultDataType: Decodable,
  51. VariableType: OperationVariable>(for request: MutationRequest<VariableType>,
  52. with resultType: ResultDataType
  53. .Type) -> MutationRef<ResultDataType, VariableType> {
  54. return MutationRef(request: request, grpcClient: grpcClient)
  55. }
  56. }