Skip to content

Commit fbb848d

Browse files
committed
make current with latest changes in 'spring-graphql'
issue spring-projects#3501
1 parent e5cb818 commit fbb848d

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

spring-integration-graphql/src/main/java/org/springframework/integration/graphql/outbound/GraphQlMessageHandler.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 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.
@@ -17,8 +17,11 @@
1717
package org.springframework.integration.graphql.outbound;
1818

1919
import java.util.Collections;
20+
import java.util.Locale;
2021
import java.util.Map;
2122

23+
import javax.annotation.Nullable;
24+
2225
import org.springframework.beans.factory.BeanFactory;
2326
import org.springframework.expression.Expression;
2427
import org.springframework.expression.common.LiteralExpression;
@@ -49,6 +52,10 @@ public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler
4952

5053
private Expression variablesExpression = new SupplierExpression<>(() -> Collections.emptyMap());
5154

55+
private Locale locale = null;
56+
57+
private String executionId = null;
58+
5259
public GraphQlMessageHandler(final GraphQlService graphQlService) {
5360
Assert.notNull(graphQlService, "'graphQlService' must not be null");
5461

@@ -100,6 +107,23 @@ public void setVariablesExpression(Expression variablesExpression) {
100107
this.variablesExpression = variablesExpression;
101108
}
102109

110+
/**
111+
* Set a Locale for GraphQL Query to execute.
112+
* @param locale the locale to use.
113+
*/
114+
public void setLocale(@Nullable Locale locale) {
115+
this.locale = locale;
116+
}
117+
118+
/**
119+
* Set a Execution Id for GraphQL Query to execute.
120+
* @param executionId the executionId to use.
121+
*/
122+
public void setExecutionId(String executionId) {
123+
Assert.hasText(executionId, "'executionId' must not be empty");
124+
this.executionId = executionId;
125+
}
126+
103127
@Override
104128
protected final void doInit() {
105129
BeanFactory beanFactory = getBeanFactory();
@@ -116,10 +140,11 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
116140
}
117141
else {
118142
Assert.notNull(this.queryExpression, "'queryExpression' must not be null");
143+
Assert.hasText(this.executionId, "'executionId' must not be empty");
119144
String query = evaluateQueryExpression(requestMessage);
120145
String operationName = evaluateOperationNameExpression(requestMessage);
121146
Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
122-
requestInput = new RequestInput(query, operationName, variables);
147+
requestInput = new RequestInput(query, operationName, variables, this.locale, this.executionId);
123148
}
124149

125150
return this.graphQlService

spring-integration-graphql/src/test/java/org/springframework/integration/graphql/outbound/GraphQlMessageHandlerTests.java

+42-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 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

2121
import java.time.Duration;
2222
import java.util.Collections;
23+
import java.util.Locale;
2324
import java.util.Map;
2425
import java.util.Objects;
2526
import java.util.UUID;
@@ -32,6 +33,7 @@
3233
import org.springframework.core.io.ClassPathResource;
3334
import org.springframework.graphql.GraphQlService;
3435
import org.springframework.graphql.RequestInput;
36+
import org.springframework.graphql.RequestOutput;
3537
import org.springframework.graphql.data.method.annotation.Argument;
3638
import org.springframework.graphql.data.method.annotation.MutationMapping;
3739
import org.springframework.graphql.data.method.annotation.QueryMapping;
@@ -54,9 +56,9 @@
5456
import org.springframework.stereotype.Repository;
5557
import org.springframework.test.annotation.DirtiesContext;
5658
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
59+
import org.springframework.util.AlternativeJdkIdGenerator;
60+
import org.springframework.util.IdGenerator;
5761

58-
import graphql.ExecutionResult;
59-
import graphql.ExecutionResultImpl;
6062
import graphql.execution.reactive.SubscriptionPublisher;
6163
import reactor.core.publisher.Flux;
6264
import reactor.core.publisher.Mono;
@@ -86,17 +88,26 @@ public class GraphQlMessageHandlerTests {
8688
@Autowired
8789
private UpdateRepository updateRepository;
8890

91+
@Autowired
92+
private IdGenerator idGenerator;
93+
94+
8995
@Test
9096
@SuppressWarnings("unchecked")
9197
void testHandleMessageForQueryWithRequestInputProvided() {
9298

99+
Locale locale = Locale.getDefault();
100+
String executionId = this.idGenerator.generateId().toString();
101+
this.graphQlMessageHandler.setLocale(locale);
102+
this.graphQlMessageHandler.setExecutionId(executionId);
103+
93104
StepVerifier verifier = StepVerifier.create(
94105
Flux.from(this.resultChannel)
95106
.map(Message::getPayload)
96-
.cast(ExecutionResult.class)
107+
.cast(RequestOutput.class)
97108
)
98109
.consumeNextWith(result -> {
99-
assertThat(result).isInstanceOf(ExecutionResultImpl.class);
110+
assertThat(result).isInstanceOf(RequestOutput.class);
100111
Map<String, Object> data = result.getData();
101112
Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
102113
assertThat(testQuery.get("id")).isEqualTo("test-data");
@@ -106,7 +117,7 @@ void testHandleMessageForQueryWithRequestInputProvided() {
106117

107118
this.inputChannel.send(
108119
MessageBuilder
109-
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap()))
120+
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap(), locale, executionId))
110121
.build()
111122
);
112123

@@ -117,14 +128,17 @@ void testHandleMessageForQueryWithRequestInputProvided() {
117128
@SuppressWarnings("unchecked")
118129
void testHandleMessageForQueryWithQueryProvided() {
119130

131+
String executionId = this.idGenerator.generateId().toString();
132+
this.graphQlMessageHandler.setExecutionId(executionId);
133+
120134
String fakeQuery = "{ testQuery { id } }";
121135
this.graphQlMessageHandler.setQuery(fakeQuery);
122136

123137
StepVerifier.create(
124-
Mono.from((Mono<ExecutionResult>) this.graphQlMessageHandler.handleRequestMessage(MessageBuilder.withPayload(fakeQuery).build()))
138+
Mono.from((Mono<RequestOutput>) this.graphQlMessageHandler.handleRequestMessage(MessageBuilder.withPayload(fakeQuery).build()))
125139
)
126140
.consumeNextWith(result -> {
127-
assertThat(result).isInstanceOf(ExecutionResultImpl.class);
141+
assertThat(result).isInstanceOf(RequestOutput.class);
128142
Map<String, Object> data = result.getData();
129143
Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
130144
assertThat(testQuery.get("id")).isEqualTo("test-data");
@@ -137,16 +151,19 @@ void testHandleMessageForQueryWithQueryProvided() {
137151
@SuppressWarnings("unchecked")
138152
void testHandleMessageForMutationWithRequestInputProvided() {
139153

154+
String executionId = this.idGenerator.generateId().toString();
155+
this.graphQlMessageHandler.setExecutionId(executionId);
156+
140157
String fakeId = UUID.randomUUID().toString();
141158
Update expected = new Update(fakeId);
142159

143160
StepVerifier verifier = StepVerifier.create(
144161
Flux.from(this.resultChannel)
145162
.map(Message::getPayload)
146-
.cast(ExecutionResult.class)
163+
.cast(RequestOutput.class)
147164
)
148165
.consumeNextWith(result -> {
149-
assertThat(result).isInstanceOf(ExecutionResultImpl.class);
166+
assertThat(result).isInstanceOf(RequestOutput.class);
150167
Map<String, Object> data = result.getData();
151168
Map<String, Object> update = (Map<String, Object>) data.get("update");
152169
assertThat(update.get("id")).isEqualTo(fakeId);
@@ -159,7 +176,7 @@ void testHandleMessageForMutationWithRequestInputProvided() {
159176

160177
this.inputChannel.send(
161178
MessageBuilder
162-
.withPayload(new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, Collections.emptyMap()))
179+
.withPayload(new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, Collections.emptyMap(), null, executionId))
163180
.build()
164181
);
165182

@@ -176,17 +193,20 @@ void testHandleMessageForMutationWithRequestInputProvided() {
176193
@SuppressWarnings("unchecked")
177194
void testHandleMessageForSubscriptionWithRequestInputProvided() {
178195

196+
String executionId = this.idGenerator.generateId().toString();
197+
this.graphQlMessageHandler.setExecutionId(executionId);
198+
179199
StepVerifier verifier = StepVerifier.create(
180200
Flux.from(this.resultChannel)
181201
.map(Message::getPayload)
182-
.cast(ExecutionResult.class)
183-
.map(ExecutionResult::getData)
202+
.cast(RequestOutput.class)
203+
.mapNotNull(RequestOutput::getData)
184204
.cast(SubscriptionPublisher.class)
185205
.map(Flux::from)
186206
.flatMap(data -> data)
187207
)
188-
.consumeNextWith(executionResult -> {
189-
Map<String, Object> results = (Map<String, Object>) executionResult.getData();
208+
.consumeNextWith(requestOutput -> {
209+
Map<String, Object> results = (Map<String, Object>) requestOutput.getData();
190210
assertThat(results).containsKey("results");
191211

192212
Map<String, Object> queryResult = (Map<String, Object>) results.get("results");
@@ -201,7 +221,7 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
201221

202222
this.inputChannel.send(
203223
MessageBuilder
204-
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap()))
224+
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap(), null, executionId))
205225
.build()
206226
);
207227

@@ -383,6 +403,12 @@ AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
383403
return new AnnotatedControllerConfigurer();
384404
}
385405

406+
@Bean
407+
IdGenerator idGenerator() {
408+
409+
return new AlternativeJdkIdGenerator();
410+
}
411+
386412
}
387413

388414
static class QueryResult {

0 commit comments

Comments
 (0)