|
4 | 4 | import graphql.ExecutionResult; |
5 | 5 | import graphql.GraphQL; |
6 | 6 | import graphql.Scalars; |
| 7 | +import graphql.execution.directives.QueryAppliedDirective; |
| 8 | +import graphql.execution.directives.QueryAppliedDirectiveArgument; |
| 9 | +import graphql.execution.directives.QueryDirectives; |
7 | 10 | import graphql.schema.DataFetcher; |
8 | 11 | import graphql.schema.DataFetcherFactories; |
9 | 12 | import graphql.schema.DataFetchingEnvironment; |
|
20 | 23 | import java.time.LocalDateTime; |
21 | 24 | import java.time.format.DateTimeFormatter; |
22 | 25 | import java.util.HashMap; |
| 26 | +import java.util.List; |
23 | 27 | import java.util.Map; |
24 | 28 |
|
25 | 29 | @SuppressWarnings({"Convert2Lambda", "unused", "ClassCanBeStatic"}) |
@@ -171,4 +175,26 @@ public static void main(String[] args) { |
171 | 175 | // data['default'] == '08-10-1969' |
172 | 176 | // data['usa'] == '10-08-1969' |
173 | 177 | } |
| 178 | + |
| 179 | + DataFetcher<?> cacheDataFetcher = new DataFetcher<Object>() { |
| 180 | + @Override |
| 181 | + public Object get(DataFetchingEnvironment env) { |
| 182 | + QueryDirectives queryDirectives = env.getQueryDirectives(); |
| 183 | + List<QueryAppliedDirective> cacheDirectives = queryDirectives |
| 184 | + .getImmediateAppliedDirective("cache"); |
| 185 | + // We get a List, because we could have |
| 186 | + // repeatable directives |
| 187 | + if (cacheDirectives.size() > 0) { |
| 188 | + QueryAppliedDirective cache = cacheDirectives.get(0); |
| 189 | + QueryAppliedDirectiveArgument maxAgeArgument |
| 190 | + = cache.getArgument("maxAge"); |
| 191 | + int maxAge = maxAgeArgument.getValue(); |
| 192 | + |
| 193 | + // Now we know the max allowed cache time and |
| 194 | + // can make use of it |
| 195 | + // Your logic goes here |
| 196 | + } |
| 197 | + return "your logic here"; |
| 198 | + } |
| 199 | + }; |
174 | 200 | } |
0 commit comments