|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.graphql.outbound; |
| 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 | +import org.springframework.graphql.GraphQlService; |
| 30 | +import org.springframework.graphql.RequestInput; |
| 31 | +import org.springframework.integration.expression.ExpressionUtils; |
| 32 | +import org.springframework.integration.expression.SupplierExpression; |
| 33 | +import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; |
| 34 | +import org.springframework.messaging.Message; |
| 35 | +import org.springframework.util.Assert; |
| 36 | + |
| 37 | +/** |
| 38 | + * A {@link org.springframework.messaging.MessageHandler} capable of fielding GraphQL Query, Mutation and Subscription requests. |
| 39 | + * |
| 40 | + * @author Daniel Frey |
| 41 | + * @since 6.0 |
| 42 | + */ |
| 43 | +public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler { |
| 44 | + |
| 45 | + private final GraphQlService graphQlService; |
| 46 | + |
| 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 | + public GraphQlMessageHandler(final GraphQlService graphQlService) { |
| 60 | + Assert.notNull(graphQlService, "'graphQlService' must not be null"); |
| 61 | + |
| 62 | + this.graphQlService = graphQlService; |
| 63 | + setAsync(true); |
| 64 | + } |
| 65 | + |
| 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 | + @Override |
| 134 | + protected Object handleRequestMessage(Message<?> requestMessage) { |
| 135 | + RequestInput requestInput = null; |
| 136 | + |
| 137 | + if (requestMessage.getPayload() instanceof RequestInput) { |
| 138 | + |
| 139 | + requestInput = (RequestInput) requestMessage.getPayload(); |
| 140 | + } |
| 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); |
| 148 | + } |
| 149 | + |
| 150 | + return this.graphQlService |
| 151 | + .execute(requestInput); |
| 152 | + |
| 153 | + } |
| 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 | +} |
0 commit comments