Skip to content

how to add headers dynamically to CustomGraphQLClient executeQuery #2187

Description

@PandaGeek1024

with old DefaultGraphQLClient, we can add headers dynamically while a request comes. This is designed this way because we only know some specific headers when client send a request to service.

Now we are migrating from DefaultGraphQLClient to CustomGraphQLClient, but unfortunately the RequestExecutor is only provided during CustomGraphQLClient creation. This means all headers has to be predefined and known beforehand.

is there a way to achieve dynamic headers attaching as below old implementation?

Old code:

@Bean(name = [CORE_PRODUCT_GRAPHQL_CLIENT])
fun coreGraphQLClient(): GraphQLClient {
      return DefaultGraphQLClient(url)
}

fun executeQuery(query: String, graphQLHeaders: GraphqlHeaderMaps? = null): GraphQLResponse {
	return graphQLClient.executeQuery(query, HashMap<String, List<String>>()) { url, headers, body ->
		val requestHeaders = HttpHeaders()
		headers.forEach(requestHeaders::put)

		graphQLHeaders?.forEach(requestHeaders::put)

		val exchange: ResponseEntity<String> = restTemplate.exchange(
			url, HttpMethod.POST, HttpEntity<Any?>(body, requestHeaders),
			String::class.java
		)
		HttpResponse(exchange.statusCodeValue, exchange.body)
	}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions