Skip to content

Support for BatchHandlerMethodArgumentResolver #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spring-graphql-docs/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,14 @@ Batch mapping methods support the following arguments:
| `List<K>`
| The source/parent objects.

| `@Argument`
| For access to a named field argument converted to a higher-level, typed Object.
See <<controllers-schema-mapping-argument>>.

| `@Arguments`
| For access to all field arguments converted to a higher-level, typed Object.
See <<controllers-schema-mapping-arguments>>.

| `java.security.Principal`
| Obtained from Spring Security context, if available.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.data.method;

import graphql.schema.DataFetchingEnvironment;
import org.dataloader.BatchLoaderEnvironment;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.Map;

/**
* Strategy interface for resolving method parameters into argument values in
* the context of a given {@link BatchLoaderEnvironment} and parent/source list.
*
* <p>Most implementations will be synchronous, simply resolving values from the
* {@code BatchLoaderEnvironment} and parent/source list. However, a resolver may
* also return a {@link reactor.core.publisher.Mono} if it needs to be asynchronous.
*
* @author Genkui Du
* @since 1.0.0
*/
public interface BatchHandlerMethodArgumentResolver {

/**
* Whether this resolver supports the given {@link MethodParameter}.
*
* @param parameter the method parameter
*/
boolean supportsParameter(MethodParameter parameter);

/**
* Resolve a method parameter into an argument value.
*
* @param parameter the method parameter to resolve. This parameter must
* have previously checked via {@link #supportsParameter}.
* @param keys the list of keys to load
* @param keyContexts keys and their context objects map
* @param environment the environment to use to resolve the value
* @param <K> the type of parent/source
* @return the resolved value, which may be {@code null} if not resolved;
* the value may also be a {@link reactor.core.publisher.Mono} if it
* requires asynchronous resolution.
* @throws Exception in case of errors with the preparation of argument values
*/
@Nullable
<K> Object resolveArgument(MethodParameter parameter,
Collection<K> keys,
Map<K, ? extends DataFetchingEnvironment> keyContexts,
BatchLoaderEnvironment environment) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.data.method;

import graphql.schema.DataFetchingEnvironment;
import org.dataloader.BatchLoaderEnvironment;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Resolves method parameters by delegating to a list of registered
* {@link BatchHandlerMethodArgumentResolver BatchHandlerMethodArgumentResolvers}.
* Previously resolved method parameters are cached for faster lookups.
*
* @author Genkui Du
* @since 1.0.0
*/
public class BatchHandlerMethodArgumentResolverComposite implements BatchHandlerMethodArgumentResolver {

private final List<BatchHandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();

private final Map<MethodParameter, BatchHandlerMethodArgumentResolver> argumentResolverCache =
new ConcurrentHashMap<>(256);


/**
* Return a read-only list with the contained resolvers, or an empty list.
*/
public List<BatchHandlerMethodArgumentResolver> getArgumentResolvers() {
return Collections.unmodifiableList(argumentResolvers);
}

/**
* Add the given {@link BatchHandlerMethodArgumentResolver BatchHandlerMethodArgumentResolvers}.
*/
public BatchHandlerMethodArgumentResolverComposite addResolvers(
@Nullable List<? extends BatchHandlerMethodArgumentResolver> resolvers) {

if (resolvers != null) {
this.argumentResolvers.addAll(resolvers);
}
return this;
}

/**
* Clear the list of configured resolvers and the resolver cache.
*/
public void clear() {
this.argumentResolvers.clear();
this.argumentResolverCache.clear();
}

@Override
public boolean supportsParameter(MethodParameter parameter) {
return this.getArgumentResolver(parameter) != null;
}

@Override
public <K> Object resolveArgument(MethodParameter parameter,
Collection<K> keys,
Map<K, ? extends DataFetchingEnvironment> keyContexts,
BatchLoaderEnvironment environment) throws Exception {

BatchHandlerMethodArgumentResolver argumentResolver = getArgumentResolver(parameter);
if (argumentResolver == null) {
throw new IllegalArgumentException("Unsupported parameter type [" +
parameter.getParameterType().getName() + "]. supportsParameter should be called first.");
}
return argumentResolver.resolveArgument(parameter, keys, keyContexts, environment);

}

/**
* Find a registered {@link BatchHandlerMethodArgumentResolver} that supports
* the given method parameter.
*/
@Nullable
private BatchHandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
BatchHandlerMethodArgumentResolver result = argumentResolverCache.get(parameter);
if (result == null) {
for (BatchHandlerMethodArgumentResolver argumentResolver : this.argumentResolvers) {
if (argumentResolver.supportsParameter(parameter)) {
result = argumentResolver;
this.argumentResolverCache.put(parameter, argumentResolver);
break;
}
}
}

return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,18 @@ public void addResolver(HandlerMethodArgumentResolver resolver) {
this.argumentResolvers.add(resolver);
}

/**
* Add the given {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}.
*/
public HandlerMethodArgumentResolverComposite addResolvers(
@Nullable List<? extends HandlerMethodArgumentResolver> resolvers) {

if (resolvers != null) {
this.argumentResolvers.addAll(resolvers);
}
return this;
}

/**
* Return a read-only list with the contained resolvers, or an empty list.
*/
Expand Down
Loading