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

[3.x backport] Allow buildPostBody to write operation extensions #5631

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libraries/apollo-annotations/api/apollo-annotations.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public final class com/apollographql/apollo3/annotations/ApolloDeprecatedSince$V
public static final field v3_6_3 Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
public static final field v3_7_2 Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
public static final field v3_7_5 Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
public static final field v3_8_3 Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
public static fun valueOf (Ljava/lang/String;)Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
public static fun values ()[Lcom/apollographql/apollo3/annotations/ApolloDeprecatedSince$Version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ annotation class ApolloDeprecatedSince(val version: Version) {
v3_6_3,
v3_7_2,
v3_7_5,
v3_8_3,
}
}
1 change: 1 addition & 0 deletions libraries/apollo-api/api/apollo-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ public final class com/apollographql/apollo3/api/http/DefaultHttpRequestComposer
public final class com/apollographql/apollo3/api/http/DefaultHttpRequestComposer$Companion {
public final fun appendQueryParameters (Ljava/lang/String;Ljava/util/Map;)Ljava/lang/String;
public final fun buildParamsMap (Lcom/apollographql/apollo3/api/Operation;Lcom/apollographql/apollo3/api/CustomScalarAdapters;ZZ)Lokio/ByteString;
public final fun buildPostBody (Lcom/apollographql/apollo3/api/Operation;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lcom/apollographql/apollo3/api/http/HttpBody;
public final fun buildPostBody (Lcom/apollographql/apollo3/api/Operation;Lcom/apollographql/apollo3/api/CustomScalarAdapters;ZLjava/lang/String;)Lcom/apollographql/apollo3/api/http/HttpBody;
public final fun composePayload (Lcom/apollographql/apollo3/api/ApolloRequest;)Ljava/util/Map;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo3.api.http

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloInternal
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.CustomScalarAdapters
Expand Down Expand Up @@ -111,8 +112,8 @@ class DefaultHttpRequestComposer(
writer: JsonWriter,
operation: Operation<D>,
customScalarAdapters: CustomScalarAdapters,
sendApqExtensions: Boolean,
query: String?,
extensionsWriter: (JsonWriter.() -> Unit),
): Map<String, Upload> {
val uploads: Map<String, Upload>
writer.writeObject {
Expand All @@ -131,19 +132,37 @@ class DefaultHttpRequestComposer(
value(query)
}

extensionsWriter()
}

return uploads
}

private fun <D : Operation.Data> composePostParams(
writer: JsonWriter,
operation: Operation<D>,
customScalarAdapters: CustomScalarAdapters,
sendApqExtensions: Boolean,
query: String?,
): Map<String, Upload> {
return composePostParams(
writer, operation, customScalarAdapters, query, apqExtensionsWriter(operation.id(), sendApqExtensions)
)
}

private fun apqExtensionsWriter(id: String, sendApqExtensions: Boolean): JsonWriter.() -> Unit {
return {
if (sendApqExtensions) {
name("extensions")
writeObject {
name("persistedQuery")
writeObject {
name("version").value(1)
name("sha256Hash").value(operation.id())
name("sha256Hash").value(id)
}
}
}
}

return uploads
}

/**
Expand Down Expand Up @@ -212,11 +231,22 @@ class DefaultHttpRequestComposer(
}
}

@Deprecated("Use buildPostBody(operation, customScalarADapters, query, extensionsWriter) instead")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v3_8_3)
fun <D : Operation.Data> buildPostBody(
operation: Operation<D>,
customScalarAdapters: CustomScalarAdapters,
autoPersistQueries: Boolean,
query: String?,
): HttpBody {
return buildPostBody(operation, customScalarAdapters, query, apqExtensionsWriter(operation.id(), autoPersistQueries))
}

fun <D : Operation.Data> buildPostBody(
operation: Operation<D>,
customScalarAdapters: CustomScalarAdapters,
autoPersistQueries: Boolean,
query: String?,
extensionsWriter: JsonWriter.() -> Unit,
): HttpBody {
val uploads: Map<String, Upload>

Expand All @@ -225,8 +255,8 @@ class DefaultHttpRequestComposer(
this,
operation,
customScalarAdapters,
autoPersistQueries,
query
query,
extensionsWriter
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package test

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.CustomScalarAdapters
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.http.ByteStringHttpBody
import com.apollographql.apollo3.api.http.DefaultHttpRequestComposer
import com.apollographql.apollo3.api.http.HttpMethod
import com.apollographql.apollo3.api.http.HttpRequest
import com.apollographql.apollo3.api.http.HttpRequestComposer
import com.apollographql.apollo3.api.json.buildJsonByteString
import com.apollographql.apollo3.api.json.jsonReader
import com.apollographql.apollo3.api.json.readAny
import com.apollographql.apollo3.api.json.writeObject
import com.apollographql.apollo3.integration.normalizer.HeroNameQuery
import com.apollographql.apollo3.integration.fullstack.LaunchDetailsQuery
import com.apollographql.apollo3.mockserver.MockServer
import com.apollographql.apollo3.mockserver.enqueue
import com.apollographql.apollo3.network.http.HttpNetworkTransport
Expand All @@ -23,51 +23,51 @@ import kotlin.test.assertEquals
class WithExtensionsHttpRequestComposer(private val serverUrl: String) : HttpRequestComposer {
override fun <D : Operation.Data> compose(apolloRequest: ApolloRequest<D>): HttpRequest {

return HttpRequest.Builder(HttpMethod.Post, serverUrl)
.body(
ByteStringHttpBody(
"application/json",
buildJsonByteString {
writeObject {
name("query")
value(apolloRequest.operation.document())
name("operationName")
value(apolloRequest.operation.name())
name("extensions")
value("extension value")
}
}
)
)
.build()
val request = HttpRequest.Builder(HttpMethod.Post, serverUrl)
.body(
DefaultHttpRequestComposer.buildPostBody(
apolloRequest.operation,
apolloRequest.executionContext[CustomScalarAdapters]!!,
apolloRequest.operation.document(),
) {
name("extensions")
writeObject {
name("key")
value("value")
}
}
)
.build()

return request
}
}

class BodyExtensionsTest {
@Suppress("UNCHECKED_CAST")
@Test
fun bodyExtensions() = runTest {
val mockServer = MockServer()
val serverUrl = mockServer.url()

val apolloClient = ApolloClient.Builder()
.networkTransport(
HttpNetworkTransport.Builder()
.httpRequestComposer(WithExtensionsHttpRequestComposer(serverUrl))
.build()
)
.build()
.networkTransport(
HttpNetworkTransport.Builder()
.httpRequestComposer(WithExtensionsHttpRequestComposer(mockServer.url()))
.build()
)
.build()

mockServer.enqueue(statusCode = 500)
kotlin.runCatching {
apolloClient.query(HeroNameQuery())
.execute()
apolloClient.query(LaunchDetailsQuery("42")).execute()
}

val request = mockServer.takeRequest()

@Suppress("UNCHECKED_CAST")
val asMap = Buffer().write(request.body).jsonReader().readAny() as Map<String, Any>
assertEquals(asMap["extensions"], "extension value")
assertEquals((asMap["extensions"] as Map<String, Any>).get("key"), "value")
assertEquals((asMap["variables"] as Map<String, Any>).get("id"), "42")

apolloClient.dispose()
mockServer.stop()
Expand Down
Loading