Skip to content

Commit

Permalink
fix: raise an execution exception before trying to decode response
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed Jul 12, 2023
1 parent 5e5ba24 commit 1999aa6
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Sources/SwiftGraphQLClient/Client/Selection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ extension GraphQLClient {
policy: Operation.Policy = .cacheFirst
) -> AnyPublisher<DecodedOperationResult<T>, Error> where TypeLock: GraphQLHttpOperation {
self.executeQuery(for: selection, as: operationName, url: request, policy: policy)
.tryMap { result in try result.decode(selection: selection) }
.tryMap { result in
// NOTE: If there was an error during the execution, we want to raise it before running
// the decoder on the `data` which will most likely fail.
if let error = result.error {
throw error
}
return try result.decode(selection: selection)
}
.eraseToAnyPublisher()
}

Expand All @@ -99,7 +106,14 @@ extension GraphQLClient {
policy: Operation.Policy = .cacheFirst
) -> AnyPublisher<DecodedOperationResult<T>, Error> where TypeLock: GraphQLHttpOperation {
self.executeMutation(for: selection, as: operationName, url: request, policy: policy)
.tryMap { result in try result.decode(selection: selection) }
.tryMap { result in
// NOTE: If there was an error during the execution, we want to raise it before running
// the decoder on the `data` which will most likely fail.
if let error = result.error {
throw error
}
return try result.decode(selection: selection)
}
.eraseToAnyPublisher()
}

Expand All @@ -111,7 +125,14 @@ extension GraphQLClient {
policy: Operation.Policy = .cacheFirst
) -> AnyPublisher<DecodedOperationResult<T>, Error> where TypeLock: GraphQLWebSocketOperation {
self.executeSubscription(of: selection, as: operationName, url: request, policy: policy)
.tryMap { result in try result.decode(selection: selection) }
.tryMap { result in
// NOTE: If there was an error during the execution, we want to raise it before running
// the decoder on the `data` which will most likely fail.
if let error = result.error {
throw error
}
return try result.decode(selection: selection)
}
.eraseToAnyPublisher()
}
}
Expand Down

0 comments on commit 1999aa6

Please sign in to comment.