Skip to content

Commit 747f78b

Browse files
committed
refactor to handle GraphQL Subscription requests
issue spring-projects#3501
1 parent afd1f49 commit 747f78b

File tree

6 files changed

+282
-191
lines changed

6 files changed

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

1717
package org.springframework.integration.graphql.outbound;
1818

19-
import java.util.Collections;
20-
import java.util.Locale;
21-
import java.util.Map;
22-
23-
import javax.annotation.Nullable;
24-
25-
import org.springframework.beans.factory.BeanFactory;
26-
import org.springframework.expression.Expression;
27-
import org.springframework.expression.common.LiteralExpression;
28-
import org.springframework.expression.spel.support.StandardEvaluationContext;
2919
import org.springframework.graphql.GraphQlService;
3020
import org.springframework.graphql.RequestInput;
31-
import org.springframework.integration.expression.ExpressionUtils;
32-
import org.springframework.integration.expression.SupplierExpression;
3321
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
3422
import org.springframework.messaging.Message;
35-
import org.springframework.util.Assert;
23+
24+
import graphql.ExecutionResult;
25+
import reactor.core.publisher.Mono;
3626

3727
/**
38-
* A {@link org.springframework.messaging.MessageHandler} capable of fielding GraphQL Query, Mutation and Subscription requests.
28+
* A <code>MessageHandler</code> capable of fielding GraphQL Query, Mutation and Subscription requests.
3929
*
4030
* @author Daniel Frey
41-
* @since 6.0
4231
*/
4332
public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler {
4433

4534
private final GraphQlService graphQlService;
4635

47-
private StandardEvaluationContext evaluationContext;
48-
49-
private Expression queryExpression;
50-
51-
private Expression operationNameExpression = new SupplierExpression<>(() -> null);
52-
53-
private Expression variablesExpression = new SupplierExpression<>(() -> Collections.emptyMap());
54-
55-
private Locale locale = null;
56-
57-
private String executionId = null;
58-
5936
public GraphQlMessageHandler(final GraphQlService graphQlService) {
60-
Assert.notNull(graphQlService, "'graphQlService' must not be null");
61-
6237
this.graphQlService = graphQlService;
6338
setAsync(true);
6439
}
6540

66-
/**
67-
* Specify a GraphQL Query.
68-
* @param query the GraphQL query to use.
69-
*/
70-
public void setQuery(String query) {
71-
Assert.hasText(query, "'query' must not be empty");
72-
setQueryExpression(new LiteralExpression(query));
73-
}
74-
75-
/**
76-
* Specify a SpEL expression to evaluate a GraphQL Query
77-
* @param queryExpression the expression to evaluate a GraphQL query.
78-
*/
79-
public void setQueryExpression(Expression queryExpression) {
80-
Assert.notNull(queryExpression, "'queryExpression' must not be null");
81-
this.queryExpression = queryExpression;
82-
}
83-
84-
/**
85-
* Set a GraphQL Operation Name to execute.
86-
* @param operationName the GraphQL Operation Name to use.
87-
*/
88-
public void setOperationName(String operationName) {
89-
setOperationNameExpression(new LiteralExpression(operationName));
90-
}
91-
92-
/**
93-
* Set a SpEL expression to evaluate a GraphQL Operation Name to execute.
94-
* @param operationNameExpression the expression to use.
95-
*/
96-
public void setOperationNameExpression(Expression operationNameExpression) {
97-
Assert.notNull(operationNameExpression, "'operationNameExpression' must not be null");
98-
this.operationNameExpression = operationNameExpression;
99-
}
100-
101-
/**
102-
* Set a SpEL expression to evaluate Variables for GraphQL Query to execute.
103-
* @param variablesExpression the expression to use.
104-
*/
105-
public void setVariablesExpression(Expression variablesExpression) {
106-
Assert.notNull(variablesExpression, "'variablesExpression' must not be null");
107-
this.variablesExpression = variablesExpression;
108-
}
109-
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-
127-
@Override
128-
protected final void doInit() {
129-
BeanFactory beanFactory = getBeanFactory();
130-
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(beanFactory);
131-
}
132-
13341
@Override
13442
protected Object handleRequestMessage(Message<?> requestMessage) {
135-
RequestInput requestInput = null;
13643

13744
if (requestMessage.getPayload() instanceof RequestInput) {
13845

139-
requestInput = (RequestInput) requestMessage.getPayload();
46+
Mono<ExecutionResult> result = this.graphQlService
47+
.execute((RequestInput) requestMessage.getPayload());
48+
49+
return result;
14050
}
141-
else {
142-
Assert.notNull(this.queryExpression, "'queryExpression' must not be null");
143-
Assert.hasText(this.executionId, "'executionId' must not be empty");
144-
String query = evaluateQueryExpression(requestMessage);
145-
String operationName = evaluateOperationNameExpression(requestMessage);
146-
Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
147-
requestInput = new RequestInput(query, operationName, variables, this.locale, this.executionId);
51+
else {
52+
throw new IllegalArgumentException("Message payload needs to be 'org.springframework.graphql.RequestInput'");
14853
}
149-
150-
return this.graphQlService
151-
.execute(requestInput);
152-
15354
}
154-
155-
private String evaluateQueryExpression(Message<?> message) {
156-
String query = this.queryExpression.getValue(this.evaluationContext, message, String.class);
157-
Assert.notNull(query, "'queryExpression' must not evaluate to null");
158-
return query;
159-
}
160-
161-
private String evaluateOperationNameExpression(Message<?> message) {
162-
return this.operationNameExpression.getValue(this.evaluationContext, message, String.class);
163-
}
164-
165-
@SuppressWarnings("unchecked")
166-
private Map<String, Object> evaluateVariablesExpression(Message<?> message) {
167-
return this.variablesExpression.getValue(this.evaluationContext, message, Map.class);
168-
}
169-
17055
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
* @author Daniel Frey
6767
*
6868
*/
69-
@SpringJUnitConfig(GraphqlMutationMessageHandlerTests.TestConfig.class)
69+
@SpringJUnitConfig(GraphQlMutationMessageHandlerTests.TestConfig.class)
7070
@DirtiesContext
71-
public class GraphqlMutationMessageHandlerTests {
71+
public class GraphQlMutationMessageHandlerTests {
7272

7373
@Autowired
7474
private FluxMessageChannel inputChannel;
@@ -83,6 +83,7 @@ public class GraphqlMutationMessageHandlerTests {
8383
UpdateRepository updateRepository;
8484

8585
@Test
86+
@SuppressWarnings("unchecked")
8687
void testHandleMessageForMutation() {
8788

8889
String fakeId = UUID.randomUUID().toString();
@@ -179,13 +180,13 @@ Mono<Update> current() {
179180
static class TestConfig {
180181

181182
@Bean
182-
GraphqlQueryMutationMessageHandler handler(GraphQlService graphQlService) {
183+
GraphQlMessageHandler handler(GraphQlService graphQlService) {
183184

184-
return new GraphqlQueryMutationMessageHandler(graphQlService);
185+
return new GraphQlMessageHandler(graphQlService);
185186
}
186187

187188
@Bean
188-
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphqlQueryMutationMessageHandler handler) {
189+
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphQlMessageHandler handler) {
189190

190191
return IntegrationFlows.from(MessageChannels.flux("inputChannel"))
191192
.handle(handler)
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
* @author Daniel Frey
6363
*
6464
*/
65-
@SpringJUnitConfig(GraphqlQueryMessageHandlerTests.TestConfig.class)
65+
@SpringJUnitConfig(GraphQlQueryMessageHandlerTests.TestConfig.class)
6666
@DirtiesContext
67-
public class GraphqlQueryMessageHandlerTests {
67+
public class GraphQlQueryMessageHandlerTests {
6868

6969
@Autowired
7070
private FluxMessageChannel inputChannel;
@@ -76,6 +76,7 @@ public class GraphqlQueryMessageHandlerTests {
7676
private PollableChannel errorChannel;
7777

7878
@Test
79+
@SuppressWarnings("unchecked")
7980
void testHandleMessageForQuery() {
8081

8182
StepVerifier verifier = StepVerifier.create(
@@ -137,13 +138,13 @@ public Mono<QueryResult> testQuery() {
137138
static class TestConfig {
138139

139140
@Bean
140-
GraphqlQueryMutationMessageHandler handler(GraphQlService graphQlService) {
141+
GraphQlMessageHandler handler(GraphQlService graphQlService) {
141142

142-
return new GraphqlQueryMutationMessageHandler(graphQlService);
143+
return new GraphQlMessageHandler(graphQlService);
143144
}
144145

145146
@Bean
146-
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphqlQueryMutationMessageHandler handler) {
147+
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphQlMessageHandler handler) {
147148

148149
return IntegrationFlows.from(MessageChannels.flux("inputChannel"))
149150
.handle(handler)

0 commit comments

Comments
 (0)