|
26 | 26 |
|
27 | 27 | import com.apollographql.federation.graphqljava.Federation;
|
28 | 28 | import com.apollographql.federation.graphqljava.SchemaTransformer;
|
| 29 | +import graphql.language.Argument; |
| 30 | +import graphql.language.BooleanValue; |
| 31 | +import graphql.language.Directive; |
29 | 32 | import graphql.language.TypeDefinition;
|
30 | 33 | import graphql.schema.DataFetcher;
|
31 | 34 | import graphql.schema.GraphQLSchema;
|
@@ -190,19 +193,31 @@ public SchemaTransformer createSchemaTransformer(TypeDefinitionRegistry registry
|
190 | 193 | private void checkEntityMappings(TypeDefinitionRegistry registry) {
|
191 | 194 | List<String> unmappedEntities = new ArrayList<>();
|
192 | 195 | for (TypeDefinition<?> type : registry.types().values()) {
|
193 |
| - type.getDirectives().forEach((directive) -> { |
194 |
| - boolean isEntityType = directive.getName().equalsIgnoreCase("key"); |
195 |
| - if (isEntityType && !this.handlerMethods.containsKey(type.getName())) { |
196 |
| - unmappedEntities.add(type.getName()); |
197 |
| - } |
198 |
| - }); |
| 196 | + if (isEntityMappingExpected(type) && !this.handlerMethods.containsKey(type.getName())) { |
| 197 | + unmappedEntities.add(type.getName()); |
| 198 | + } |
199 | 199 | }
|
200 | 200 | if (!unmappedEntities.isEmpty()) {
|
201 | 201 | throw new IllegalStateException("Unmapped entity types: " +
|
202 | 202 | unmappedEntities.stream().collect(Collectors.joining("', '", "'", "'")));
|
203 | 203 | }
|
204 | 204 | }
|
205 | 205 |
|
| 206 | + /** |
| 207 | + * Determine if a handler method is expected for this type: there is at least one '@key' directive |
| 208 | + * whose 'resolvable' argument resolves to true (either explicitly, or if the argument is not set). |
| 209 | + * @param type the type to inspect. |
| 210 | + * @return true if a handler method is expected for this type |
| 211 | + */ |
| 212 | + private boolean isEntityMappingExpected(TypeDefinition<?> type) { |
| 213 | + List<Directive> keyDirectives = type.getDirectives("key"); |
| 214 | + return !keyDirectives.isEmpty() && keyDirectives.stream() |
| 215 | + .anyMatch((keyDirective) -> { |
| 216 | + Argument resolvableArg = keyDirective.getArgument("resolvable"); |
| 217 | + return resolvableArg == null || |
| 218 | + (resolvableArg.getValue() instanceof BooleanValue) && ((BooleanValue) resolvableArg.getValue()).isValue(); |
| 219 | + }); |
| 220 | + } |
206 | 221 |
|
207 | 222 | public record EntityMappingInfo(String typeName, HandlerMethod handlerMethod) {
|
208 | 223 |
|
|
0 commit comments