|
1 | 1 | /*
|
2 |
| - * Copyright 2021-2022 the original author or authors. |
| 2 | + * Copyright 2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.integration.graphql.outbound;
|
18 | 18 |
|
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; |
29 | 19 | import org.springframework.graphql.GraphQlService;
|
30 | 20 | import org.springframework.graphql.RequestInput;
|
31 |
| -import org.springframework.integration.expression.ExpressionUtils; |
32 |
| -import org.springframework.integration.expression.SupplierExpression; |
33 | 21 | import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
34 | 22 | import org.springframework.messaging.Message;
|
35 |
| -import org.springframework.util.Assert; |
| 23 | + |
| 24 | +import graphql.ExecutionResult; |
| 25 | +import reactor.core.publisher.Mono; |
36 | 26 |
|
37 | 27 | /**
|
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. |
39 | 29 | *
|
40 | 30 | * @author Daniel Frey
|
41 |
| - * @since 6.0 |
42 | 31 | */
|
43 | 32 | public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler {
|
44 | 33 |
|
45 | 34 | private final GraphQlService graphQlService;
|
46 | 35 |
|
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 |
| - |
59 | 36 | public GraphQlMessageHandler(final GraphQlService graphQlService) {
|
60 |
| - Assert.notNull(graphQlService, "'graphQlService' must not be null"); |
61 |
| - |
62 | 37 | this.graphQlService = graphQlService;
|
63 | 38 | setAsync(true);
|
64 | 39 | }
|
65 | 40 |
|
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 |
| - |
133 | 41 | @Override
|
134 | 42 | protected Object handleRequestMessage(Message<?> requestMessage) {
|
135 |
| - RequestInput requestInput = null; |
136 | 43 |
|
137 | 44 | if (requestMessage.getPayload() instanceof RequestInput) {
|
138 | 45 |
|
139 |
| - requestInput = (RequestInput) requestMessage.getPayload(); |
| 46 | + Mono<ExecutionResult> result = this.graphQlService |
| 47 | + .execute((RequestInput) requestMessage.getPayload()); |
| 48 | + |
| 49 | + return result; |
140 | 50 | }
|
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'"); |
148 | 53 | }
|
149 |
| - |
150 |
| - return this.graphQlService |
151 |
| - .execute(requestInput); |
152 |
| - |
153 | 54 | }
|
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 |
| - |
170 | 55 | }
|
0 commit comments