Skip to content

Commit

Permalink
🐛 Fix missing rootDoc for GraphQL queries involving only non-mapped f…
Browse files Browse the repository at this point in the history
…ields at path level 2
  • Loading branch information
ujibang committed Oct 20, 2023
1 parent 2fd0803 commit a42a058
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
*/
package org.restheart.graphql.models.builder;

import java.util.HashMap;
import java.util.Map;

import org.bson.BsonDocument;
import org.bson.BsonInvalidOperationException;
import org.restheart.graphql.GraphQLIllegalAppDefinitionException;
import org.restheart.graphql.models.AppDescriptor;
import org.restheart.graphql.models.GraphQLApp;
import org.restheart.graphql.models.ObjectMapping;
import org.restheart.graphql.models.TypeMapping;
import org.restheart.utils.BsonUtils;

Expand Down Expand Up @@ -71,6 +73,8 @@ public static final GraphQLApp build(BsonDocument appDef) throws GraphQLIllegalA
throw new GraphQLIllegalAppDefinitionException(errorMSg, schemaProblem);
}



if (appDef.containsKey("mappings")) {
if (appDef.get("mappings").isDocument()) {
var mappings = appDef.getDocument("mappings");
Expand All @@ -82,15 +86,19 @@ public static final GraphQLApp build(BsonDocument appDef) throws GraphQLIllegalA
throw new GraphQLIllegalAppDefinitionException("'Mappings' field must be an Object but was " + appDef.get("mappings").getBsonType());
}
} else {
objectsMappings = null;
objectsMappings = new HashMap<>();
}

// Provide a default field mappings for Objects that are not explicitly mapped.
// see ObjectsMappings.defaultObjectFieldMappings() javadoc for more information.
typeDefinitionRegistry.types().entrySet().stream()
.filter(e -> objectsMappings == null || !objectsMappings.containsKey(e.getKey()))
.filter(e -> !objectsMappings.containsKey(e.getKey()))
.filter(e -> e.getValue() instanceof ObjectTypeDefinition)
.forEach(e -> ObjectsMappings.defaultObjectFieldMappings(e.getKey(), typeDefinitionRegistry, new BsonDocument()));
.forEach(e -> {
var objectFieldMappings = ObjectsMappings.defaultObjectFieldMappings(e.getKey(), typeDefinitionRegistry, new BsonDocument());
var objectMapping = new ObjectMapping(e.getKey(), objectFieldMappings);
objectsMappings.put(e.getKey(), objectMapping);
});

try {
return GraphQLApp.newBuilder().appDescriptor(descriptor).schema(schema)
Expand Down

0 comments on commit a42a058

Please sign in to comment.