diff --git a/elide-graphql/src/main/java/com/yahoo/elide/graphql/Entity.java b/elide-graphql/src/main/java/com/yahoo/elide/graphql/Entity.java index 810f515f0d..67e88cf9ea 100644 --- a/elide-graphql/src/main/java/com/yahoo/elide/graphql/Entity.java +++ b/elide-graphql/src/main/java/com/yahoo/elide/graphql/Entity.java @@ -33,24 +33,22 @@ public class Entity { @Getter private RequestScope requestScope; @Getter private Set attributes; @Getter private Set relationships; - @Getter private EntityProjection projection; /** * Class constructor. * @param parentResource parent entity * @param data entity data - * @param projection projection for this entity + * @param entityClass entity class * @param requestScope the request context object */ public Entity( Optional parentResource, Map data, - EntityProjection projection, + Class entityClass, RequestScope requestScope) { this.parentResource = parentResource; this.data = data; - this.projection = projection; - this.entityClass = projection.getType(); + this.entityClass = entityClass; this.requestScope = requestScope; setAttributes(); setRelationships(); @@ -103,15 +101,6 @@ private void setRelationships() { Class relationshipClass = dictionary.getParameterizedType(this.entityClass, relationshipName); - // if this data contains a relationship that is not in the projection tree, create a temporary - // projection for that relationship - EntityProjection relationshipProjection = - projection.getRelationship(relationshipName).isPresent() - ? projection.getRelationship(relationshipName).get().getProjection() - : EntityProjection.builder() - .type(relationshipClass) - .build(); - Set relationshipEntities = new LinkedHashSet<>(); // if the relationship is ToOne, entry.getValue() should be a single map @@ -119,14 +108,14 @@ private void setRelationships() { relationshipEntities.add(new Entity( Optional.of(this), ((Map) entry.getValue()), - relationshipProjection, + relationshipClass, this.requestScope)); } else { for (Map row : (List>) entry.getValue()) { relationshipEntities.add(new Entity( Optional.of(this), row, - relationshipProjection, + relationshipClass, this.requestScope)); } } @@ -199,6 +188,14 @@ public void setId() { public PersistentResource toPersistentResource() { return this.data == null ? null - : PersistentResource.loadRecord(projection, getId().orElse(null), this.requestScope); + : PersistentResource.loadRecord(getProjection(), getId().orElse(null), this.requestScope); + } + + /** + * Get a projection for this entity class. Used for querying inserted entities. + * @return {@link EntityProjection} object + */ + public EntityProjection getProjection() { + return EntityProjection.builder().type(entityClass).build(); } } diff --git a/elide-graphql/src/main/java/com/yahoo/elide/graphql/PersistentResourceFetcher.java b/elide-graphql/src/main/java/com/yahoo/elide/graphql/PersistentResourceFetcher.java index 33f4365774..54df3eb058 100644 --- a/elide-graphql/src/main/java/com/yahoo/elide/graphql/PersistentResourceFetcher.java +++ b/elide-graphql/src/main/java/com/yahoo/elide/graphql/PersistentResourceFetcher.java @@ -15,14 +15,12 @@ import com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException; import com.yahoo.elide.core.exceptions.InvalidValueException; import com.yahoo.elide.graphql.containers.ConnectionContainer; -import com.yahoo.elide.graphql.parser.GraphQLProjectionInfo; import com.yahoo.elide.request.EntityProjection; import com.yahoo.elide.request.Relationship; import com.google.common.collect.Sets; import graphql.language.Field; -import graphql.language.SourceLocation; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLType; @@ -251,33 +249,16 @@ private ConnectionContainer upsertOrUpdateObjects( context.field.getName()); } - // Try to get projection for the entity to upsert/update - SourceLocation selectionLocation = context.field.getSourceLocation(); - GraphQLProjectionInfo projectionInfo = context.requestScope.getProjectionInfo(); - EntityProjection entityProjection; - if (selectionLocation.equals(projectionInfo.getRootLocation())) { - // if this is the root selection - entityProjection = context.requestScope.getEntityProjection(); - } else if (projectionInfo.getRelationshipMap().containsKey(selectionLocation)) { - // if this is a relationship in the projection - entityProjection = projectionInfo.getRelationshipMap().get(selectionLocation).getProjection(); - } else { - // if there is not matched projection, create a new temporary projection - entityProjection = EntityProjection.builder() - .type(entityClass) - .build(); - } - /* form entities */ Optional parentEntity; if (!context.isRoot()) { - parentEntity = Optional.of(new Entity(Optional.empty(), null, entityProjection, context.requestScope)); + parentEntity = Optional.of(new Entity(Optional.empty(), null, entityClass, context.requestScope)); } else { parentEntity = Optional.empty(); } LinkedHashSet entitySet = new LinkedHashSet<>(); for (Map input : context.data.get()) { - entitySet.add(new Entity(parentEntity, input, entityProjection, context.requestScope)); + entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope)); } /* apply function to upsert/update the object */ diff --git a/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLEntityProjectionMaker.java b/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLEntityProjectionMaker.java index fac1bf7b40..164050e365 100644 --- a/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLEntityProjectionMaker.java +++ b/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLEntityProjectionMaker.java @@ -134,7 +134,7 @@ public GraphQLProjectionInfo make(String query) { } }); - return new GraphQLProjectionInfo(rootLocation, rootProjections, relationshipMap); + return new GraphQLProjectionInfo(rootProjections, relationshipMap); } /** diff --git a/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLProjectionInfo.java b/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLProjectionInfo.java index 442b4c2c39..29bf14bffe 100644 --- a/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLProjectionInfo.java +++ b/elide-graphql/src/main/java/com/yahoo/elide/graphql/parser/GraphQLProjectionInfo.java @@ -8,10 +8,10 @@ import com.yahoo.elide.request.EntityProjection; import com.yahoo.elide.request.Relationship; - import graphql.language.SourceLocation; import lombok.AllArgsConstructor; import lombok.Getter; + import java.util.Collection; import java.util.Map; @@ -21,12 +21,7 @@ */ @AllArgsConstructor public class GraphQLProjectionInfo { - @Getter - private final SourceLocation rootLocation; - - @Getter - private final Collection projections; + @Getter private final Collection projections; - @Getter - private final Map relationshipMap; + @Getter private final Map relationshipMap; }