From 908f4b99b4cc49f75a90317a14283c05c58d434c Mon Sep 17 00:00:00 2001 From: JoelCourtney Date: Fri, 22 Nov 2024 10:10:38 -0800 Subject: [PATCH] WIP Fix graphql mutations --- .../GraphQLMerlinDatabaseService.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinDatabaseService.java b/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinDatabaseService.java index 77288576f9..aa0686ea41 100644 --- a/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinDatabaseService.java +++ b/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinDatabaseService.java @@ -459,8 +459,8 @@ public Map updatePlanActivityDirective //Create ids.putAll(createActivityDirectives(planId, toAdd, activityToGoalId, schedulerModel)); - modifyActivityDirectives(planId, toModify, activityToGoalId); - deleteActivityDirectives(planId, toDelete); + modifyActivityDirectives(planId, toModify); +// deleteActivityDirectives(planId, toDelete); return ids; } @@ -638,25 +638,28 @@ mutation createAllPlanActivityDirectives($activities: [activity_directive_insert return activityToDirectiveId; } - - private void modifyActivityDirectives( final PlanId planId, - final List activities, - final Map activityToGoalId + final List activities ) throws IOException, NoSuchPlanException, MerlinServiceException { ensurePlanExists(planId); final var request = new StringBuilder(); - request.append("mutation updatePlanActivityDirectives($activities: [activity_directive_set_input!]!) {"); - var index = 0; - final var activitiesObject = Json.createArrayBuilder(); + request.append("mutation updatePlanActivityDirectives("); + request.append(String.join( + ",", + activities.stream().map($ -> "$activity_%d: activity_directive_set_input!".formatted($.id().id())).toList() + )); + request.append(") {"); + final var arguments = Json.createObjectBuilder(); for (final var act : activities) { + final var id = act.id().id(); request.append(""" - update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activities[%d]) {} - """.formatted(act.id().id(), act.id().id(), planId.id(), index)); - index++; + update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activity_%d) { + affected_rows + } + """.formatted(id, id, planId.id(), id)); final var activityObject = Json .createObjectBuilder() @@ -669,13 +672,9 @@ private void modifyActivityDirectives( insertionObjectArguments.add(arg.getKey(), serializedValueP.unparse(arg.getValue())); } activityObject.add("arguments", insertionObjectArguments.build()); - activitiesObject.add(activityObject); + arguments.add("activity_%d".formatted(id), activityObject); } - final var arguments = Json - .createObjectBuilder() - .add("activities", activitiesObject.build()) - .build(); - postRequest(request.toString(), arguments).orElseThrow(() -> new NoSuchPlanException(planId)); + postRequest(request.toString(), arguments.build()).orElseThrow(() -> new NoSuchPlanException(planId)); } private void deleteActivityDirectives( @@ -689,9 +688,10 @@ private void deleteActivityDirectives( request.append("mutation deletePlanActivityDirectives {"); for (final var id : ids) { request.append(""" - delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {} + delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {affected_rows} """.formatted(id.id(), id.id(), planId.id())); } + request.append("}"); postRequest(request.toString()).orElseThrow(() -> new NoSuchPlanException(planId)); }