Skip to content

Commit ee59710

Browse files
calvincestarigh-action-runner
authored and
gh-action-runner
committed
feature: Adds support for GraphQL over HTTP media type (apollographql/apollo-ios-dev#558)
1 parent 0d140a1 commit ee59710

4 files changed

+10
-6
lines changed

Sources/Apollo/MultipartResponseDeferParser.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct MultipartResponseDeferParser: MultipartResponseSpecificationParser {
1313
switch self {
1414

1515
case let .unsupportedContentType(type):
16-
return "Unsupported content type: application/json is required but got \(type)."
16+
return "Unsupported content type: 'application/graphql-response+json' or 'application/json' are supported, received '\(type)'."
1717
case .cannotParseChunkData:
1818
return "The chunk data could not be parsed."
1919
case .cannotParsePayloadData:
@@ -53,7 +53,7 @@ struct MultipartResponseDeferParser: MultipartResponseSpecificationParser {
5353
for dataLine in chunk.components(separatedBy: Self.dataLineSeparator.description) {
5454
switch DataLine(dataLine.trimmingCharacters(in: .newlines)) {
5555
case let .contentHeader(directives):
56-
guard directives.contains("application/json") else {
56+
guard directives.contains(where: { $0.isValidGraphQLContentType }) else {
5757
return .failure(ParsingError.unsupportedContentType(type: directives.joined(separator: ";")))
5858
}
5959

Sources/Apollo/MultipartResponseParsingInterceptor.swift

+4
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,8 @@ extension String {
194194
.components(separatedBy: ";")
195195
.map({ $0.trimmingCharacters(in: .whitespaces) })
196196
}
197+
198+
var isValidGraphQLContentType: Bool {
199+
self == "application/json" || self == "application/graphql-response+json"
200+
}
197201
}

Sources/Apollo/MultipartResponseSubscriptionParser.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct MultipartResponseSubscriptionParser: MultipartResponseSpecificationParser
1515
switch self {
1616

1717
case let .unsupportedContentType(type):
18-
return "Unsupported content type: application/json is required but got \(type)."
18+
return "Unsupported content type: 'application/graphql-response+json' or 'application/json' are supported, received '\(type)'."
1919
case .cannotParseChunkData:
2020
return "The chunk data could not be parsed."
2121
case let .irrecoverableError(message):
@@ -71,7 +71,7 @@ struct MultipartResponseSubscriptionParser: MultipartResponseSpecificationParser
7171
break
7272

7373
case let .contentHeader(directives):
74-
guard directives.contains("application/json") else {
74+
guard directives.contains(where: { $0.isValidGraphQLContentType }) else {
7575
return .failure(ParsingError.unsupportedContentType(type: directives.joined(separator: ";")))
7676
}
7777

Sources/Apollo/RequestChainNetworkTransport.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ open class RequestChainNetworkTransport: NetworkTransport {
103103
if Operation.operationType == .subscription {
104104
request.addHeader(
105105
name: "Accept",
106-
value: "multipart/mixed;\(MultipartResponseSubscriptionParser.protocolSpec),application/json"
106+
value: "multipart/mixed;\(MultipartResponseSubscriptionParser.protocolSpec),application/graphql-response+json,application/json"
107107
)
108108

109109
} else {
110110
request.addHeader(
111111
name: "Accept",
112-
value: "multipart/mixed;\(MultipartResponseDeferParser.protocolSpec),application/json"
112+
value: "multipart/mixed;\(MultipartResponseDeferParser.protocolSpec),application/graphql-response+json,application/json"
113113
)
114114
}
115115

0 commit comments

Comments
 (0)