From 9519be128c232a73a97daf6a91066f35be55d50d Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Thu, 31 Mar 2022 22:29:50 -0700 Subject: [PATCH] add a createLabelPublisher --- .../Mutations/CreateLabelPublisher.swift | 62 +++++++++++++++++++ .../Queries/LibraryItemsQuery.swift | 10 --- .../Selections/FeedItemLabelSelection.swift | 12 ++++ 3 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift create mode 100644 apple/OmnivoreKit/Sources/Services/DataService/Selections/FeedItemLabelSelection.swift diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift new file mode 100644 index 000000000..6400f87cf --- /dev/null +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift @@ -0,0 +1,62 @@ +import Combine +import Foundation +import Models +import SwiftGraphQL + +public extension DataService { + func createLabelPublisher( + name: String, + color: String, + description: String? + ) -> AnyPublisher { + enum MutationResult { + case saved(label: FeedItemLabel) + case error(errorCode: Enums.CreateLabelErrorCode) + } + + let selection = Selection { + try $0.on( + createLabelSuccess: .init { .saved(label: try $0.label(selection: feedItemLabelSelection)) }, + createLabelError: .init { .error(errorCode: try $0.errorCodes().first ?? .badRequest) } + ) + } + + let mutation = Selection.Mutation { + try $0.createLabel( + input: InputObjects.CreateLabelInput( + name: name, + color: color, + description: OptionalArgument(description) + ), + selection: selection + ) + } + + let path = appEnvironment.graphqlPath + let headers = networker.defaultHeaders + + return Deferred { + Future { promise in + send(mutation, to: path, headers: headers) { result in + switch result { + case let .success(payload): + if let graphqlError = payload.errors { + promise(.failure(.message(messageText: "graphql error: \(graphqlError)"))) + } + + switch payload.data { + case let .saved(label: label): + promise(.success(label)) + case let .error(errorCode: errorCode): + promise(.failure(.message(messageText: errorCode.rawValue))) + } + case .failure: + promise(.failure(.message(messageText: "graphql error"))) + } + } + } + } + .receive(on: DispatchQueue.main) + .eraseToAnyPublisher() + } +} diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift index 174df3d26..532ffe88f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift @@ -148,16 +148,6 @@ let homeFeedItemSelection = Selection.Article { ) } -private let feedItemLabelSelection = Selection.Label { - FeedItemLabel( - id: try $0.id(), - name: try $0.name(), - color: try $0.color(), - createdAt: try $0.createdAt()?.value, - description: try $0.description() - ) -} - private let articleEdgeSelection = Selection.ArticleEdge { try $0.node(selection: homeFeedItemSelection) } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Selections/FeedItemLabelSelection.swift b/apple/OmnivoreKit/Sources/Services/DataService/Selections/FeedItemLabelSelection.swift new file mode 100644 index 000000000..998dc8dea --- /dev/null +++ b/apple/OmnivoreKit/Sources/Services/DataService/Selections/FeedItemLabelSelection.swift @@ -0,0 +1,12 @@ +import Models +import SwiftGraphQL + +let feedItemLabelSelection = Selection.Label { + FeedItemLabel( + id: try $0.id(), + name: try $0.name(), + color: try $0.color(), + createdAt: try $0.createdAt()?.value, + description: try $0.description() + ) +}