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 1ea7bb5096..8fc9056d29 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 @@ -50,7 +50,7 @@ * Invoked by GraphQL Java to fetch/mutate data from Elide. */ @Slf4j -public class PersistentResourceFetcher implements DataFetcher { +public class PersistentResourceFetcher implements DataFetcher { private final ElideSettings settings; public PersistentResourceFetcher(ElideSettings settings) { @@ -166,7 +166,7 @@ private Object fetchObjects(Environment context) { * @param generateTotals True if page totals should be generated for this type, false otherwise * @return {@link PersistentResource} object(s) */ - public ConnectionContainer fetchObject(Environment context, RequestScope requestScope, Class entityClass, + public ConnectionContainer fetchObject(Environment context, RequestScope requestScope, Class entityClass, Optional> ids, Optional sort, Optional offset, Optional first, Optional filters, boolean generateTotals) { @@ -207,7 +207,7 @@ public ConnectionContainer fetchObject(Environment context, RequestScope request * @return persistence resource object(s) */ public Object fetchRelationship(Environment context, - PersistentResource parentResource, + PersistentResource parentResource, String fieldName, Optional> ids, Optional offset, @@ -216,7 +216,7 @@ public Object fetchRelationship(Environment context, Optional filters, boolean generateTotals) { EntityDictionary dictionary = parentResource.getRequestScope().getDictionary(); - Class entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName); + Class entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName); String typeName = dictionary.getJsonAliasFor(entityClass); Optional pagination = buildPagination(first, offset, generateTotals); @@ -255,7 +255,7 @@ private ConnectionContainer updateObjects(Environment context) { * @return Connection object. */ private ConnectionContainer upsertOrUpdateObjects(Environment context, - Executor updateFunc, + Executor updateFunc, RelationshipOp operation) { /* sanity check for id and data argument w UPSERT/UPDATE */ if (context.ids.isPresent()) { @@ -298,10 +298,10 @@ private ConnectionContainer upsertOrUpdateObjects(Environment context, /* fixup relationships */ for (Entity entity : entitySet) { graphWalker(entity, this::updateRelationship); - PersistentResource childResource = entity.toPersistentResource(); - if (!context.isRoot() && childResource.isNewlyCreated()) { + PersistentResource childResource = entity.toPersistentResource(); + if (!context.isRoot() && (childResource.isNewlyCreated() || context.parentResource.isNewlyCreated())) { /* add relation between parent and nested entity */ - context.parentResource.addRelation(context.field.getName(), entity.toPersistentResource()); + context.parentResource.addRelation(context.field.getName(), childResource); } } @@ -328,8 +328,8 @@ private interface Executor { * @param function Function to process nodes * @return set of {@link PersistentResource} objects */ - private void graphWalker(Entity entity, Executor function) { - Queue toVisit = new ArrayDeque(); + private void graphWalker(Entity entity, Executor function) { + Queue toVisit = new ArrayDeque<>(); Set visited = new LinkedHashSet<>(); toVisit.add(entity); @@ -337,9 +337,8 @@ private void graphWalker(Entity entity, Executor function) { Entity currentEntity = toVisit.remove(); if (visited.contains(currentEntity)) { continue; - } else { - visited.add(currentEntity); } + visited.add(currentEntity); function.execute(currentEntity); Set relationshipEntities = currentEntity.getRelationships(); /* loop over relationships */ @@ -354,9 +353,9 @@ private void graphWalker(Entity entity, Executor function) { * @param entity Resource entity * @return {@link PersistentResource} object */ - private PersistentResource updateRelationship(Entity entity) { + private PersistentResource updateRelationship(Entity entity) { Set relationshipEntities = entity.getRelationships(); - PersistentResource resource = entity.toPersistentResource(); + PersistentResource resource = entity.toPersistentResource(); Set toUpdate; /* loop over each relationship */ @@ -376,12 +375,12 @@ private PersistentResource updateRelationship(Entity entity) { * @param entity Resource entity * @return {@link PersistentResource} object */ - private PersistentResource upsertObject(Environment context, Entity entity) { + private PersistentResource upsertObject(Environment context, Entity entity) { Set attributes = entity.getAttributes(); Optional id = entity.getId(); RequestScope requestScope = entity.getRequestScope(); - PersistentResource upsertedResource; - PersistentResource parentResource; + PersistentResource upsertedResource; + PersistentResource parentResource; if (!entity.getParentResource().isPresent()) { parentResource = null; } else { @@ -418,24 +417,23 @@ private PersistentResource upsertObject(Environment context, Entity entity) { return updateAttributes(upsertedResource, entity, attributes); } - private PersistentResource updateObject(Environment context, Entity entity) { + private PersistentResource updateObject(Environment context, Entity entity) { Set attributes = entity.getAttributes(); Optional id = entity.getId(); RequestScope requestScope = entity.getRequestScope(); - PersistentResource updatedResource; + PersistentResource updatedResource; if (!id.isPresent()) { throw new BadRequestException("UPDATE data objects must include ids"); - } else { - Set loadedResource = fetchObject(context, requestScope, entity.getEntityClass(), - Optional.of(Arrays.asList(id.get())), - Optional.empty(), - Optional.empty(), - Optional.empty(), - Optional.empty(), - false).getPersistentResources(); - updatedResource = loadedResource.iterator().next(); } + Set loadedResource = fetchObject(context, requestScope, entity.getEntityClass(), + Optional.of(Arrays.asList(id.get())), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty(), + false).getPersistentResources(); + updatedResource = loadedResource.iterator().next(); return updateAttributes(updatedResource, entity, attributes); } @@ -447,7 +445,7 @@ private PersistentResource updateObject(Environment context, Entity entity) { * @param attributes Set of entity attributes * @return Persistence Resource object */ - private PersistentResource updateAttributes(PersistentResource toUpdate, + private PersistentResource updateAttributes(PersistentResource toUpdate, Entity entity, Set attributes) { EntityDictionary dictionary = entity.getRequestScope().getDictionary();