Skip to content

Commit b7d1e6e

Browse files
committed
Pass attributes in HttpGraphQlTransport
Closes gh-659
1 parent 4a0d01a commit b7d1e6e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

spring-graphql/src/main/java/org/springframework/graphql/client/HttpGraphQlTransport.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,6 +75,11 @@ public Mono<GraphQlResponse> execute(GraphQlRequest request) {
7575
.contentType(this.contentType)
7676
.accept(MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_GRAPHQL)
7777
.bodyValue(request.toMap())
78+
.attributes(attributes -> {
79+
if (request instanceof ClientGraphQlRequest clientRequest) {
80+
attributes.putAll(clientRequest.getAttributes());
81+
}
82+
})
7883
.retrieve()
7984
.bodyToMono(MAP_TYPE)
8085
.map(ResponseMapGraphQlResponse::new);

spring-graphql/src/test/java/org/springframework/graphql/client/WebGraphQlClientBuilderTests.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.time.Duration;
2121
import java.util.Collections;
2222
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
2324
import java.util.stream.Stream;
2425

2526
import graphql.ExecutionResultImpl;
@@ -47,6 +48,9 @@
4748
import org.springframework.test.web.reactive.server.HttpHandlerConnector;
4849
import org.springframework.util.Assert;
4950
import org.springframework.util.MimeType;
51+
import org.springframework.web.reactive.function.client.ClientRequest;
52+
import org.springframework.web.reactive.function.client.ClientResponse;
53+
import org.springframework.web.reactive.function.client.ExchangeFunction;
5054
import org.springframework.web.reactive.function.client.WebClient;
5155
import org.springframework.web.reactive.function.server.HandlerStrategies;
5256
import org.springframework.web.reactive.function.server.RouterFunction;
@@ -227,6 +231,20 @@ void codecConfigurerRegistersJsonPathMappingProvider(ClientBuilderSetup builderS
227231
assertThat(testDecoder.getLastValue()).isEqualTo(character);
228232
}
229233

234+
@Test
235+
void attributes() {
236+
237+
HttpBuilderSetup builderSetup = new HttpBuilderSetup();
238+
239+
builderSetup.initBuilder().url("/graphql-one").headers(headers -> headers.add("h", "one")).build()
240+
.document(DOCUMENT)
241+
.attribute("id", 123)
242+
.execute()
243+
.block(TIMEOUT);
244+
245+
assertThat(builderSetup.getClientAttributes()).containsEntry("id", 123);
246+
}
247+
230248

231249
private interface ClientBuilderSetup {
232250

@@ -275,13 +293,26 @@ protected WebGraphQlHandler webGraphQlHandler() {
275293

276294
private static class HttpBuilderSetup extends AbstractBuilderSetup {
277295

296+
private final Map<String, Object> clientAttributes = new ConcurrentHashMap<>();
297+
298+
public Map<String, Object> getClientAttributes() {
299+
return this.clientAttributes;
300+
}
301+
278302
@Override
279303
public HttpGraphQlClient.Builder<?> initBuilder() {
280304
GraphQlHttpHandler handler = new GraphQlHttpHandler(webGraphQlHandler());
281305
RouterFunction<ServerResponse> routerFunction = route().POST("/**", handler::handleRequest).build();
282306
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction, HandlerStrategies.withDefaults());
283307
HttpHandlerConnector connector = new HttpHandlerConnector(httpHandler);
284-
return HttpGraphQlClient.builder(WebClient.builder().clientConnector(connector));
308+
return HttpGraphQlClient.builder(WebClient.builder()
309+
.clientConnector(connector).filter(this::updateAttributes));
310+
}
311+
312+
private Mono<ClientResponse> updateAttributes(ClientRequest request, ExchangeFunction next) {
313+
this.clientAttributes.clear();
314+
this.clientAttributes.putAll(request.attributes());
315+
return next.exchange(request);
285316
}
286317

287318
}

0 commit comments

Comments
 (0)