Skip to content

Commit 3f0687b

Browse files
authored
fix: Raise an Execution Exception before trying to decode a Query Response (#150)
1 parent 5e5ba24 commit 3f0687b

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Sources/SwiftGraphQLClient/Client/Selection.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ extension GraphQLClient {
8787
policy: Operation.Policy = .cacheFirst
8888
) -> AnyPublisher<DecodedOperationResult<T>, Error> where TypeLock: GraphQLHttpOperation {
8989
self.executeQuery(for: selection, as: operationName, url: request, policy: policy)
90-
.tryMap { result in try result.decode(selection: selection) }
90+
.tryMap { result in
91+
// NOTE: If there was an error during the execution, we want to raise it before running
92+
// the decoder on the `data` which will most likely fail.
93+
if let error = result.error {
94+
throw error
95+
}
96+
return try result.decode(selection: selection)
97+
}
9198
.eraseToAnyPublisher()
9299
}
93100

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

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

0 commit comments

Comments
 (0)