Skip to content

Exempt unresolvable federated entities from @EntityMapping checks #1225

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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import com.apollographql.federation.graphqljava.Federation;
import com.apollographql.federation.graphqljava.SchemaTransformer;
import graphql.language.Argument;
import graphql.language.BooleanValue;
import graphql.language.Directive;
import graphql.language.TypeDefinition;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
Expand Down Expand Up @@ -190,19 +193,31 @@ public SchemaTransformer createSchemaTransformer(TypeDefinitionRegistry registry
private void checkEntityMappings(TypeDefinitionRegistry registry) {
List<String> unmappedEntities = new ArrayList<>();
for (TypeDefinition<?> type : registry.types().values()) {
type.getDirectives().forEach((directive) -> {
boolean isEntityType = directive.getName().equalsIgnoreCase("key");
if (isEntityType && !this.handlerMethods.containsKey(type.getName())) {
unmappedEntities.add(type.getName());
}
});
if (isEntityMappingExpected(type) && !this.handlerMethods.containsKey(type.getName())) {
unmappedEntities.add(type.getName());
}
}
if (!unmappedEntities.isEmpty()) {
throw new IllegalStateException("Unmapped entity types: " +
unmappedEntities.stream().collect(Collectors.joining("', '", "'", "'")));
}
}

/**
* Determine if a handler method is expected for this type: there is at least one '@key' directive
* whose 'resolvable' argument resolves to true (either explicitly, or if the argument is not set).
* @param type the type to inspect.
* @return true if a handler method is expected for this type
*/
private boolean isEntityMappingExpected(TypeDefinition<?> type) {
List<Directive> keyDirectives = type.getDirectives("key");
return !keyDirectives.isEmpty() && keyDirectives.stream()
.anyMatch((keyDirective) -> {
Argument resolvableArg = keyDirective.getArgument("resolvable");
return resolvableArg == null ||
(resolvableArg.getValue() instanceof BooleanValue) && ((BooleanValue) resolvableArg.getValue()).isValue();
});
}

public record EntityMappingInfo(String typeName, HandlerMethod handlerMethod) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key", "@extends", "@external"] )

type Book @key(fields: "id") @extends {
id: ID! @external
author: Author
publisher: Publisher
}

type Author {
id: ID
firstName: String
lastName: String
}

type Publisher @key(fields: "id", resolvable: false) {
id: ID! @external
}