|
9 | 9 | import graphql.ExecutionResult; |
10 | 10 | import graphql.ExecutionResultImpl; |
11 | 11 | import graphql.GraphQLError; |
| 12 | +import graphql.incremental.DelayedIncrementalPartialResult; |
| 13 | +import graphql.incremental.IncrementalPayload; |
12 | 14 | import graphql.kickstart.execution.config.ConfiguringObjectMapperProvider; |
13 | 15 | import graphql.kickstart.execution.config.GraphQLServletObjectMapperConfigurer; |
14 | 16 | import graphql.kickstart.execution.config.ObjectMapperProvider; |
|
18 | 20 | import java.io.InputStream; |
19 | 21 | import java.io.Writer; |
20 | 22 | import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
21 | 24 | import java.util.LinkedHashMap; |
22 | 25 | import java.util.List; |
23 | 26 | import java.util.Map; |
@@ -118,51 +121,71 @@ public byte[] serializeResultAsBytes(ExecutionResult executionResult) { |
118 | 121 | return getJacksonMapper().writeValueAsBytes(createResultFromExecutionResult(executionResult)); |
119 | 122 | } |
120 | 123 |
|
| 124 | + @SneakyThrows |
| 125 | + public byte[] serializeDelayedIncrementalResultsAsBytes(DelayedIncrementalPartialResult delayedIncrementalPartialResult) { |
| 126 | + return getJacksonMapper().writeValueAsBytes(createResultFromDelayedIncrementalPayloadResult(delayedIncrementalPartialResult)); |
| 127 | + } |
| 128 | + |
121 | 129 | public boolean areErrorsPresent(ExecutionResult executionResult) { |
122 | 130 | return graphQLErrorHandlerSupplier.get().errorsPresent(executionResult.getErrors()); |
123 | 131 | } |
124 | 132 |
|
125 | | - public ExecutionResult sanitizeErrors(ExecutionResult executionResult) { |
126 | | - Object data = executionResult.getData(); |
| 133 | + public boolean areExtensionsPresent(ExecutionResult executionResult) { |
127 | 134 | Map<Object, Object> extensions = executionResult.getExtensions(); |
128 | | - List<GraphQLError> errors = executionResult.getErrors(); |
| 135 | + return extensions != null && !extensions.isEmpty(); |
| 136 | + } |
129 | 137 |
|
| 138 | + public ExecutionResult sanitizeErrors(ExecutionResult executionResult) { |
130 | 139 | GraphQLErrorHandler errorHandler = graphQLErrorHandlerSupplier.get(); |
131 | | - if (errorHandler.errorsPresent(errors)) { |
132 | | - errors = errorHandler.processErrors(errors); |
133 | | - } else { |
134 | | - errors = null; |
135 | | - } |
136 | | - return new ExecutionResultImpl(data, errors, extensions); |
| 140 | + return executionResult.transform(er -> { |
| 141 | + List<GraphQLError> errors = executionResult.getErrors(); |
| 142 | + if (errorHandler.errorsPresent(errors)) { |
| 143 | + errors = errorHandler.processErrors(errors); |
| 144 | + } else { |
| 145 | + errors = List.of(); |
| 146 | + } |
| 147 | + er.errors(errors); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + public DelayedIncrementalPartialResult sanitizeErrors(DelayedIncrementalPartialResult delayedIncrementalPartialResult) { |
| 152 | + return delayedIncrementalPartialResult; |
137 | 153 | } |
138 | 154 |
|
139 | 155 | public Map<String, Object> createResultFromExecutionResult(ExecutionResult executionResult) { |
140 | 156 | ExecutionResult sanitizedExecutionResult = sanitizeErrors(executionResult); |
141 | 157 | return convertSanitizedExecutionResult(sanitizedExecutionResult); |
142 | 158 | } |
143 | 159 |
|
| 160 | + public Map<String, Object> createResultFromDelayedIncrementalPayloadResult(DelayedIncrementalPartialResult delayedIncrementalPartialResult) { |
| 161 | + DelayedIncrementalPartialResult sanitizedDelayedIncrementalPartialResult = sanitizeErrors(delayedIncrementalPartialResult); |
| 162 | + return convertSanitizedDelayedIncrementalPartialResult(sanitizedDelayedIncrementalPartialResult); |
| 163 | + } |
| 164 | + |
144 | 165 | public Map<String, Object> convertSanitizedExecutionResult(ExecutionResult executionResult) { |
145 | 166 | return convertSanitizedExecutionResult(executionResult, true); |
146 | 167 | } |
147 | 168 |
|
| 169 | + public Map<String, Object> convertSanitizedDelayedIncrementalPartialResult( |
| 170 | + DelayedIncrementalPartialResult delayedIncrementalPartialResult) { |
| 171 | + return delayedIncrementalPartialResult.toSpecification(); |
| 172 | + } |
| 173 | + |
148 | 174 | public Map<String, Object> convertSanitizedExecutionResult( |
149 | 175 | ExecutionResult executionResult, boolean includeData) { |
150 | | - final Map<String, Object> result = new LinkedHashMap<>(); |
151 | | - |
152 | | - if (areErrorsPresent(executionResult)) { |
153 | | - result.put( |
154 | | - "errors", |
155 | | - executionResult.getErrors().stream() |
156 | | - .map(GraphQLError::toSpecification) |
157 | | - .collect(toList())); |
| 176 | + final Map<String, Object> result = new HashMap<>(executionResult.toSpecification()); |
| 177 | + |
| 178 | + if (!areErrorsPresent(executionResult)) { |
| 179 | + result.remove("errors"); |
158 | 180 | } |
159 | 181 |
|
160 | | - if (executionResult.getExtensions() != null && !executionResult.getExtensions().isEmpty()) { |
161 | | - result.put("extensions", executionResult.getExtensions()); |
| 182 | + if (!includeData) { |
| 183 | + result.remove("data"); |
162 | 184 | } |
| 185 | + result.putIfAbsent("data", null); |
163 | 186 |
|
164 | | - if (includeData) { |
165 | | - result.put("data", executionResult.getData()); |
| 187 | + if (!areExtensionsPresent(executionResult)) { |
| 188 | + result.remove("extensions"); |
166 | 189 | } |
167 | 190 |
|
168 | 191 | return result; |
|
0 commit comments