Skip to content

Commit a0cf0bb

Browse files
BobaFettersAnthonyMDev
authored andcommitted
fix: Update graphql-js error handling (#3132)
1 parent d7d08d2 commit a0cf0bb

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Sources/ApolloCodegenLib/ApolloCodegen.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,14 @@ public class ApolloCodegen {
305305
)
306306

307307
guard graphqlErrors.isEmpty else {
308-
let errorlines = graphqlErrors.flatMap({ $0.logLines })
309-
CodegenLogger.log(String(describing: errorlines), logLevel: .error)
308+
let errorlines = graphqlErrors.flatMap({
309+
if let logLines = $0.logLines {
310+
return logLines
311+
} else {
312+
return ["\($0.name ?? "unknown"): \($0.message ?? "")"]
313+
}
314+
})
315+
CodegenLogger.log(errorlines.joined(separator: "\n"), logLevel: .error)
310316
throw Error.graphQLSourceValidationFailure(atLines: errorlines)
311317
}
312318

Sources/ApolloCodegenLib/Frontend/GraphQLError.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ public class GraphQLError: JavaScriptError {
99
private lazy var source: GraphQLSource = self["source"]
1010

1111
/// The source locations associated with this error.
12-
private(set) lazy var sourceLocations: [GraphQLSourceLocation] = {
13-
let locations: [JavaScriptObject] = self["locations"]
12+
private(set) lazy var sourceLocations: [GraphQLSourceLocation]? = {
13+
guard let locations: [JavaScriptObject] = self["locations"] else {
14+
return nil
15+
}
1416

1517
if let nodes: [ASTNode] = self["nodes"] {
1618
// We have AST nodes, so this is a validation error.
@@ -35,8 +37,8 @@ public class GraphQLError: JavaScriptError {
3537

3638
/// Log lines for this error in a format that allows Xcode to show errors inline at the correct location.
3739
/// See https://shazronatadobe.wordpress.com/2010/12/04/xcode-shell-build-phase-reporting-of-errors/
38-
var logLines: [String] {
39-
return sourceLocations.map {
40+
var logLines: [String]? {
41+
return sourceLocations?.map {
4042
return [$0.filePath, String($0.lineNumber), "error", message ?? "?"].joined(separator: ":")
4143
}
4244
}

Tests/ApolloCodegenTests/Frontend/SchemaLoadingTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class SchemaLoadingTests: XCTestCase {
5454
let error = try XCTDowncast(error as AnyObject, to: GraphQLError.self)
5555
XCTAssert(try XCTUnwrap(error.message).starts(with: "Syntax Error"))
5656

57-
XCTAssertEqual(error.sourceLocations.count, 1)
58-
XCTAssertEqual(error.sourceLocations[0].filePath, "schema.graphqls")
59-
XCTAssertEqual(error.sourceLocations[0].lineNumber, 3)
57+
XCTAssertEqual(error.sourceLocations?.count, 1)
58+
XCTAssertEqual(error.sourceLocations?[0].filePath, "schema.graphqls")
59+
XCTAssertEqual(error.sourceLocations?[0].lineNumber, 3)
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)