Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Raise an Execution Exception before trying to decode a Query Response #150

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading