Skip to content

Commit c4f2279

Browse files
committed
Let HttpNetworkTransport handle the Accept header
1 parent fa96da1 commit c4f2279

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

libraries/apollo-api/api/apollo-api.api

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,6 @@ public final class com/apollographql/apollo/api/http/ByteStringHttpBody : com/ap
934934
public final class com/apollographql/apollo/api/http/DefaultHttpRequestComposer : com/apollographql/apollo/api/http/HttpRequestComposer {
935935
public static final field Companion Lcom/apollographql/apollo/api/http/DefaultHttpRequestComposer$Companion;
936936
public fun <init> (Ljava/lang/String;)V
937-
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
938-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
939937
public fun compose (Lcom/apollographql/apollo/api/ApolloRequest;)Lcom/apollographql/apollo/api/http/HttpRequest;
940938
}
941939

libraries/apollo-api/api/apollo-api.klib.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ final class com.apollographql.apollo.api.http/ByteStringHttpBody : com.apollogra
493493

494494
final class com.apollographql.apollo.api.http/DefaultHttpRequestComposer : com.apollographql.apollo.api.http/HttpRequestComposer { // com.apollographql.apollo.api.http/DefaultHttpRequestComposer|null[0]
495495
constructor <init>(kotlin/String) // com.apollographql.apollo.api.http/DefaultHttpRequestComposer.<init>|<init>(kotlin.String){}[0]
496-
constructor <init>(kotlin/String, kotlin/String = ...) // com.apollographql.apollo.api.http/DefaultHttpRequestComposer.<init>|<init>(kotlin.String;kotlin.String){}[0]
497496

498497
final fun <#A1: com.apollographql.apollo.api/Operation.Data> compose(com.apollographql.apollo.api/ApolloRequest<#A1>): com.apollographql.apollo.api.http/HttpRequest // com.apollographql.apollo.api.http/DefaultHttpRequestComposer.compose|compose(com.apollographql.apollo.api.ApolloRequest<0:0>){0§<com.apollographql.apollo.api.Operation.Data>}[0]
499498

libraries/apollo-api/src/commonMain/kotlin/com/apollographql/apollo/api/http/DefaultHttpRequestComposer.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,27 @@ import okio.buffer
3333
*/
3434
class DefaultHttpRequestComposer(
3535
private val serverUrl: String,
36-
private val acceptHeaderQueriesAndMutations: String = HEADER_ACCEPT_VALUE_QUERIES_AND_MUTATIONS,
3736
) : HttpRequestComposer {
3837

39-
constructor(serverUrl: String) : this(
40-
serverUrl = serverUrl,
41-
acceptHeaderQueriesAndMutations = HEADER_ACCEPT_VALUE_QUERIES_AND_MUTATIONS,
42-
)
43-
4438
override fun <D : Operation.Data> compose(apolloRequest: ApolloRequest<D>): HttpRequest {
4539
val operation = apolloRequest.operation
4640
val customScalarAdapters = apolloRequest.executionContext[CustomScalarAdapters] ?: CustomScalarAdapters.Empty
4741

4842
val requestHeaders = mutableListOf<HttpHeader>().apply {
49-
add(HttpHeader("Accept", if (apolloRequest.operation is Subscription<*>) HEADER_ACCEPT_VALUE_SUBSCRIPTIONS else acceptHeaderQueriesAndMutations))
5043
if (apolloRequest.httpHeaders != null) {
5144
addAll(apolloRequest.httpHeaders)
5245
}
46+
if (get("accept") == null) {
47+
add(
48+
HttpHeader("accept",
49+
if (apolloRequest.operation is Subscription<*>) {
50+
"multipart/mixed;subscriptionSpec=1.0, application/graphql-response+json, application/json"
51+
} else {
52+
"multipart/mixed;deferSpec=20220824, application/graphql-response+json, application/json"
53+
}
54+
)
55+
)
56+
}
5357
}
5458

5559
val sendApqExtensions = apolloRequest.sendApqExtensions ?: false
@@ -109,11 +113,6 @@ class DefaultHttpRequestComposer(
109113
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v5_0_0)
110114
val HEADER_ACCEPT_VALUE_MULTIPART = "multipart/mixed;subscriptionSpec=1.0, application/graphql-response+json, application/json"
111115

112-
private const val HEADER_ACCEPT_VALUE_QUERIES_AND_MUTATIONS =
113-
"multipart/mixed;deferSpec=20220824, application/graphql-response+json, application/json"
114-
private const val HEADER_ACCEPT_VALUE_SUBSCRIPTIONS =
115-
"multipart/mixed;subscriptionSpec=1.0, application/graphql-response+json, application/json"
116-
117116
private fun <D : Operation.Data> buildGetUrl(
118117
serverUrl: String,
119118
operation: Operation<D>,

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/network/http/HttpNetworkTransport.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ private constructor(
5555
request: ApolloRequest<D>,
5656
): Flow<ApolloResponse<D>> {
5757
val customScalarAdapters = request.executionContext[CustomScalarAdapters]!!
58+
59+
val request = if (request.httpHeaders.orEmpty().none { it.name.lowercase() == "accept" }) {
60+
val accept = if (request.operation is Subscription<*>) {
61+
"multipart/mixed;subscriptionSpec=1.0, application/graphql-response+json, application/json"
62+
} else {
63+
incrementalDeliveryProtocolImpl.acceptHeader
64+
}
65+
request.newBuilder().addHttpHeader("accept", accept).build()
66+
} else {
67+
request
68+
}
5869
val httpRequest = httpRequestComposer.compose(request)
5970

6071
return execute(request, httpRequest, customScalarAdapters)
@@ -382,12 +393,7 @@ private constructor(
382393
"It is an error to set both 'httpRequestComposer' and 'serverUrl'"
383394
}
384395
val composer = httpRequestComposer
385-
?: serverUrl?.let {
386-
DefaultHttpRequestComposer(
387-
serverUrl = it,
388-
acceptHeaderQueriesAndMutations = incrementalDeliveryProtocol.impl.acceptHeader,
389-
)
390-
}
396+
?: serverUrl?.let { DefaultHttpRequestComposer(it) }
391397
?: error("No HttpRequestComposer found. Use 'httpRequestComposer' or 'serverUrl'")
392398

393399
if (headers.isNotEmpty()) {

0 commit comments

Comments
 (0)