|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
26 | 26 |
|
27 | 27 | import org.springframework.core.MethodParameter;
|
28 | 28 | import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
|
29 |
| -import org.springframework.util.Assert; |
30 | 29 | import org.springframework.util.ClassUtils;
|
| 30 | +import org.springframework.util.StringUtils; |
31 | 31 |
|
32 | 32 | /**
|
33 | 33 | * Resolver for the source/parent of a field, obtained via
|
@@ -66,10 +66,21 @@ private static boolean isExcludedSimpleValueType(Class<?> type) {
|
66 | 66 | @Override
|
67 | 67 | public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) {
|
68 | 68 | Object source = environment.getSource();
|
69 |
| - Assert.isInstanceOf(parameter.getParameterType(), source, |
70 |
| - "The declared parameter of type '" + parameter.getParameterType() + "' " + |
71 |
| - "does not match the type of the source Object '" + source.getClass() + "'."); |
| 69 | + if (source == null) { |
| 70 | + throw new IllegalStateException(formatArgumentError(parameter, |
| 71 | + " was not recognized by any resolver and there is no source/parent either. " + |
| 72 | + "Please, refer to the documentation for the full list of supported parameters.")); |
| 73 | + } |
| 74 | + if (!parameter.getParameterType().isInstance(source)) { |
| 75 | + throw new IllegalStateException(formatArgumentError(parameter, |
| 76 | + " does not match the source Object type '" + source.getClass() + "'.")); |
| 77 | + } |
72 | 78 | return source;
|
73 | 79 | }
|
74 | 80 |
|
| 81 | + private static String formatArgumentError(MethodParameter param, String message) { |
| 82 | + return "Parameter [" + param.getParameterIndex() + "] in " + |
| 83 | + param.getExecutable().toGenericString() + (StringUtils.hasText(message) ? ": " + message : ""); |
| 84 | + } |
| 85 | + |
75 | 86 | }
|
0 commit comments