|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 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 | +package org.springframework.graphql.data.method; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.dataloader.BatchLoaderEnvironment; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import org.springframework.core.CollectionFactory; |
| 26 | +import org.springframework.core.MethodParameter; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * An extension of {@link HandlerMethod} for annotated handler methods adapted |
| 32 | + * to a batch loader function with a list of values and {@link BatchLoaderEnvironment} |
| 33 | + * as their input. |
| 34 | + * |
| 35 | + * @author Rossen Stoyanchev |
| 36 | + * @since 1.0.0 |
| 37 | + */ |
| 38 | +public class BatchLoadHandlerMethod extends InvocableHandlerMethodSupport { |
| 39 | + |
| 40 | + |
| 41 | + public BatchLoadHandlerMethod(HandlerMethod handlerMethod) { |
| 42 | + super(handlerMethod); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Invoke the underlying batch loading method, resolving its arguments from |
| 48 | + * the given keys for batch loading and the {@link BatchLoaderEnvironment}. |
| 49 | + * |
| 50 | + * @param keys the batch loading keys |
| 51 | + * @param environment the environment available to batch loaders |
| 52 | + * @return a {@code Flux} of values or a {@code Mono} with map of key-value pairs. |
| 53 | + */ |
| 54 | + @Nullable |
| 55 | + public <K> Object invoke(Collection<K> keys, BatchLoaderEnvironment environment) { |
| 56 | + |
| 57 | + MethodParameter[] parameters = getMethodParameters(); |
| 58 | + Assert.notEmpty(parameters, "Batch loading methods should have at least " + |
| 59 | + "one argument with the List of parent objects: " + getBridgedMethod().toGenericString()); |
| 60 | + |
| 61 | + Object[] args = new Object[parameters.length]; |
| 62 | + for (int i = 0; i < parameters.length; i++) { |
| 63 | + args[i] = resolveArgument(parameters[i], keys, environment); |
| 64 | + } |
| 65 | + |
| 66 | + Object result; |
| 67 | + try { |
| 68 | + result = doInvoke(args); |
| 69 | + } |
| 70 | + catch (Exception ex) { |
| 71 | + throw new IllegalStateException("...", ex); |
| 72 | + } |
| 73 | + |
| 74 | + if (result != null) { |
| 75 | + if (result instanceof Collection) { |
| 76 | + return Flux.fromIterable((Collection<?>) result); |
| 77 | + } |
| 78 | + else if (result instanceof Map) { |
| 79 | + return Mono.just(result); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + public <K> Object resolveArgument( |
| 87 | + MethodParameter parameter, Collection<K> keys, BatchLoaderEnvironment environment) { |
| 88 | + |
| 89 | + Class<?> parameterType = parameter.getParameterType(); |
| 90 | + |
| 91 | + if (Collection.class.isAssignableFrom(parameterType)) { |
| 92 | + if (parameterType.isInstance(keys)) { |
| 93 | + return keys; |
| 94 | + } |
| 95 | + Class<?> elementType = parameter.nested().getNestedParameterType(); |
| 96 | + Collection<K> collection = CollectionFactory.createCollection(parameterType, elementType, keys.size()); |
| 97 | + collection.addAll(keys); |
| 98 | + return collection; |
| 99 | + } |
| 100 | + |
| 101 | + if (parameterType.isInstance(environment)) { |
| 102 | + return environment; |
| 103 | + } |
| 104 | + |
| 105 | + throw new IllegalStateException(formatArgumentError(parameter, "Unexpected argument type.")); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments