Code Monkey home page Code Monkey logo

Comments (6)

martinbonnin avatar martinbonnin commented on June 3, 2024 1

Makes a lot of sense ๐Ÿ‘ Thanks for the feature request.

A version of buildPostBody that allows writing extensions is available here (will backport in v3 in a bit).

If you are not doing file uploads, you can use the following for the time being:

class WithExtensionsHttpRequestComposer(private val serverUrl: String) : HttpRequestComposer {
  override fun <D : Operation.Data> compose(apolloRequest: ApolloRequest<D>): HttpRequest {

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

    return request
  }
}

This is all using public API so it's not going anywhere any time soon. If you're using file uploads on the other hand, I'm afraid you'll have to copy/paste a lot of FileUploadAwareJsonWriter until the fix gets released.

from apollo-kotlin.

martinbonnin avatar martinbonnin commented on June 3, 2024 1

Would it be possible to add a buildRequestHeaders() method to DefaultHttpRequestComposer for simplify custom composer implementaion?

Woops, sorry that fell through!

We could do something like so:

/*
 * Adds the default headers to this HttpRequest.Builder:
 * - Accept
 * - X-APOLLO-OPERATION-ID
 * - X-APOLLO-OPERATION-NAME
 */
fun HttpRequest.Builder.addDefaultHttpHeaders(): HttpRequest.Builder {}

That you could use like so:

      return HttpRequest.Builder(
            method = HttpMethod.Post,
            url = serverUrl,
        ).body(
                DefaultHttpRequestComposer.buildPostBody(
                    operation,
                    customScalarAdapters,
                    query,
                ) {
                    name("extensions")
                    writeObject {
                        name("key")
                        value("value")
                    }
                }
            )
            .addDefaultHttpHeaders()
            .build()

Heads up we are also changing the HTTP headers sent in v4. See #5533

I'm curious if you need those header specifically? Would anything break if we remove X-APOLLO-OPERATION-ID for an example?

from apollo-kotlin.

morux2 avatar morux2 commented on June 3, 2024 1

@martinbonnin

Sorry for the delayed response. Thank you for informing us about the latest changes to Apollo!
We've confirmed that there is no issue with the changing of HTTP headers.
And so, we decided to stop sending Headers to follow the update to Apollo.

It's okay to close this issue. Thank you for your warm support and response to the request ๐Ÿ˜„

from apollo-kotlin.

martinbonnin avatar martinbonnin commented on June 3, 2024 1

Thanks for the follow up!

#5631 is now available in the SNAPSHOTs and will be in next 3.8 and 4.0-beta versions. Thanks again for surfacing this!

from apollo-kotlin.

morux2 avatar morux2 commented on June 3, 2024

Thanks for responding to our feature request and providing a temporary solution!
#5631 looks good to me.


We are not doing the file uploads, therefore temporary solutions looks good.
It seems that the serializeVariables function does not have a third argument.

val operation = apolloRequest.operation
val customScalarAdapters = apolloRequest.executionContext[CustomScalarAdapters] ?: CustomScalarAdapters.Empty
name("variables")
writeObject {
    operation.serializeVariables(this, customScalarAdapters)
}

from apollo-kotlin.

morux2 avatar morux2 commented on June 3, 2024

This is another feature request.

Currently, we are copying DefaultHttpRequestComposer's requestHeaders implementations.
Would it be possible to add a buildRequestHeaders() method to DefaultHttpRequestComposer for simplify custom composer implementaion?

class CustomHttpRequestComposer(
    private val serverUrl: String,
) : HttpRequestComposer {

  override fun <D : Operation.Data> compose(apolloRequest: ApolloRequest<D>): HttpRequest {
    val operation = apolloRequest.operation
    val requestHeaders = mutableListOf<HttpHeader>().apply {
        add(HttpHeader(DefaultHttpRequestComposer.HEADER_APOLLO_OPERATION_ID, operation.id()))
        add(HttpHeader(DefaultHttpRequestComposer.HEADER_APOLLO_OPERATION_NAME, operation.name()))
      if (apolloRequest.operation is Subscription<*>) {
        add(HttpHeader(HEADER_ACCEPT_NAME, HEADER_ACCEPT_VALUE_MULTIPART))
      } else {
        add(HttpHeader(HEADER_ACCEPT_NAME, HEADER_ACCEPT_VALUE_DEFER))
      }
      if (apolloRequest.httpHeaders != null) {
        addAll(apolloRequest.httpHeaders)
      }
    }
      return HttpRequest.Builder(
            method = HttpMethod.Post,
            url = serverUrl,
        ).addHeaders(requestHeaders)
            .body(
                DefaultHttpRequestComposer.buildPostBody(
                    operation,
                    customScalarAdapters,
                    query,
                ) {
                    name("extensions")
                    writeObject {
                        name("key")
                        value("value")
                    }
                }
            )
            .build()

    companion object {
        private const val HEADER_ACCEPT_NAME = "Accept"
        private const val HEADER_ACCEPT_VALUE_DEFER = "application/json"
        private const val HEADER_ACCEPT_VALUE_MULTIPART = "multipart/mixed; boundary=\"graphql\"; subscriptionSpec=1.0, application/json"
    }
}

from apollo-kotlin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.