|
7 | 7 | import graphql.schema.DataFetchingEnvironment; |
8 | 8 |
|
9 | 9 | import java.util.ArrayList; |
10 | | -import java.util.Collections; |
11 | 10 | import java.util.LinkedHashMap; |
12 | 11 | import java.util.List; |
13 | 12 | import java.util.Map; |
14 | 13 |
|
15 | 14 | import static graphql.Assert.assertNotNull; |
16 | | -import static graphql.Assert.assertTrue; |
17 | 15 | import static graphql.util.FpKit.map; |
18 | 16 |
|
19 | 17 | /** |
|
31 | 29 | @PublicApi |
32 | 30 | public class CacheControl { |
33 | 31 |
|
| 32 | + public static final String CACHE_CONTROL_EXTENSION_KEY = "cacheControl"; |
| 33 | + |
34 | 34 | /** |
35 | 35 | * If the scope is set to PRIVATE, this indicates anything under this path should only be cached per-user, |
36 | 36 | * unless the value is overridden on a sub path. PUBLIC is the default and means anything under this path |
@@ -170,26 +170,18 @@ public static CacheControl newCacheControl() { |
170 | 170 | * @return a new execution result with the hints in the extensions map. |
171 | 171 | */ |
172 | 172 | public ExecutionResult addTo(ExecutionResult executionResult) { |
173 | | - assertTrue(executionResult instanceof ExecutionResultImpl, "You must pass in an ExecutionResult based on graphql.ExecutionResultImpl"); |
174 | | - Map<Object, Object> currentExtensions = executionResult.getExtensions(); |
175 | | - if (currentExtensions == null) { |
176 | | - currentExtensions = Collections.emptyMap(); |
177 | | - } |
178 | | - currentExtensions = new LinkedHashMap<>(currentExtensions); |
179 | | - |
180 | | - putHintsInExtensionsMap(currentExtensions); |
181 | | - |
182 | | - return ExecutionResultImpl.newExecutionResult().from(executionResult) |
183 | | - .extensions(currentExtensions).build(); |
| 173 | + return ExecutionResultImpl.newExecutionResult() |
| 174 | + .from(executionResult) |
| 175 | + .addExtension(CACHE_CONTROL_EXTENSION_KEY, hintsToCacheControlProperties()) |
| 176 | + .build(); |
184 | 177 | } |
185 | 178 |
|
186 | | - private void putHintsInExtensionsMap(Map<Object, Object> extensions) { |
| 179 | + private Map<String, Object> hintsToCacheControlProperties() { |
187 | 180 | List<Map<String, Object>> recordedHints = map(hints, Hint::toMap); |
188 | 181 |
|
189 | 182 | Map<String, Object> cacheControl = new LinkedHashMap<>(); |
190 | 183 | cacheControl.put("version", 1); |
191 | 184 | cacheControl.put("hints", recordedHints); |
192 | | - |
193 | | - extensions.put("cacheControl", cacheControl); |
| 185 | + return cacheControl; |
194 | 186 | } |
195 | 187 | } |
0 commit comments