Skip to content

Commit

Permalink
feature: Adds support for GraphQL over HTTP media type (apollographql…
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Jan 9, 2025
1 parent 0d140a1 commit ee59710
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Sources/Apollo/MultipartResponseDeferParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct MultipartResponseDeferParser: MultipartResponseSpecificationParser {
switch self {

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

Expand Down
4 changes: 4 additions & 0 deletions Sources/Apollo/MultipartResponseParsingInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,8 @@ extension String {
.components(separatedBy: ";")
.map({ $0.trimmingCharacters(in: .whitespaces) })
}

var isValidGraphQLContentType: Bool {
self == "application/json" || self == "application/graphql-response+json"
}
}
4 changes: 2 additions & 2 deletions Sources/Apollo/MultipartResponseSubscriptionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct MultipartResponseSubscriptionParser: MultipartResponseSpecificationParser
switch self {

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

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

Expand Down
4 changes: 2 additions & 2 deletions Sources/Apollo/RequestChainNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ open class RequestChainNetworkTransport: NetworkTransport {
if Operation.operationType == .subscription {
request.addHeader(
name: "Accept",
value: "multipart/mixed;\(MultipartResponseSubscriptionParser.protocolSpec),application/json"
value: "multipart/mixed;\(MultipartResponseSubscriptionParser.protocolSpec),application/graphql-response+json,application/json"
)

} else {
request.addHeader(
name: "Accept",
value: "multipart/mixed;\(MultipartResponseDeferParser.protocolSpec),application/json"
value: "multipart/mixed;\(MultipartResponseDeferParser.protocolSpec),application/graphql-response+json,application/json"
)
}

Expand Down

0 comments on commit ee59710

Please sign in to comment.