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

Introducing delegate if there is GraphQL errors #770

Merged
Prev Previous commit
Next Next commit
Fixed conflicts after rebase
  • Loading branch information
kimdv committed Oct 29, 2019
commit 05aeb47716318eb9bcbcf3666d14a6003e0d383e
8 changes: 0 additions & 8 deletions Sources/Apollo/GraphQLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,4 @@ public final class GraphQLResponse<Operation: GraphQLOperation> {
return GraphQLResult(data: nil, errors: errors, source: .server, dependentKeys: nil)
}
}

func parseErrorsOnlyFast() -> [GraphQLError]? {
guard let errorsEntry = self.body["errors"] as? [JSONObject] else {
return nil
}

return errorsEntry.map(GraphQLError.init)
}
}
30 changes: 24 additions & 6 deletions Sources/Apollo/HTTPNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class HTTPNetworkTransport {
error: error)

if let receivedError = error {
self?.handleErrorOrRetry(operation: operation,
self.handleErrorOrRetry(operation: operation,
files: files,
error: receivedError,
for: request,
Expand All @@ -162,7 +162,7 @@ public class HTTPNetworkTransport {
let unsuccessfulError = GraphQLHTTPResponseError(body: data,
response: httpResponse,
kind: .errorResponse)
self?.handleErrorOrRetry(operation: operation,
self.handleErrorOrRetry(operation: operation,
files: files,
error: unsuccessfulError,
for: request,
Expand All @@ -175,7 +175,7 @@ public class HTTPNetworkTransport {
let error = GraphQLHTTPResponseError(body: nil,
response: httpResponse,
kind: .invalidResponse)
self?.handleErrorOrRetry(operation: operation,
self.handleErrorOrRetry(operation: operation,
files: files,
error: error,
for: request,
Expand All @@ -188,7 +188,9 @@ public class HTTPNetworkTransport {
guard let body = try self.serializationFormat.deserialize(data: data) as? JSONObject else {
throw GraphQLHTTPResponseError(body: data, response: httpResponse, kind: .invalidResponse)
}

let graphQLResponse = GraphQLResponse(operation: operation, body: body)

if let errors = graphQLResponse.parseErrorsOnlyFast() {
// Handle specific errors from response
self.handleGraphQLErrorsIfNeeded(operation: operation,
Expand All @@ -200,7 +202,7 @@ public class HTTPNetworkTransport {
completionHandler(.success(graphQLResponse))
}
} catch let parsingError {
self?.handleErrorOrRetry(operation: operation,
self.handleErrorOrRetry(operation: operation,
files: files,
error: parsingError,
for: request,
Expand All @@ -226,12 +228,20 @@ public class HTTPNetworkTransport {
}

delegate.networkTransport(self, receivedGraphQLErrors: graphQLErrors, retryHandler: { [weak self] shouldRetry in
guard let self = self else {
// None of the rest of this really matters
return
}

guard shouldRetry else {
completionHandler(.success(response))
return
}

_ = self?.send(operation: operation, files: files, completionHandler: completionHandler)
_ = self.send(operation: operation,
isPersistedQueryRetry: self.enableAutoPersistedQueries,
files: files,
completionHandler: completionHandler)
})
}

Expand Down Expand Up @@ -275,12 +285,20 @@ public class HTTPNetworkTransport {
for: request,
response: response,
retryHandler: { [weak self] shouldRetry in
guard let self = self else {
// None of the rest of this really matters
return
}

guard shouldRetry else {
completionHandler(.failure(error))
return
}

_ = self?.send(operation: operation, files: files, completionHandler: completionHandler)
_ = self.send(operation: operation,
isPersistedQueryRetry: self.enableAutoPersistedQueries,
files: files,
completionHandler: completionHandler)
})
}

Expand Down