diff --git a/tavern/graphql/job_test.go b/tavern/graphql/job_test.go index 03b637234..50f44ddc3 100644 --- a/tavern/graphql/job_test.go +++ b/tavern/graphql/job_test.go @@ -159,11 +159,21 @@ func TestCreateJob(t *testing.T) { func newCreateJobTest(gqlClient *client.Client, sessionIDs []int, input ent.CreateJobInput, checks ...func(t *testing.T, id int, err error)) func(t *testing.T) { return func(t *testing.T) { // Define the mutatation for testing, taking the input as a variable - mut := `mutation newCreateJobTest($sessionIDs: [ID!]!, $input: CreateJobInput!) { createJob(sessionIDs:$sessionIDs, input:$input) { id } }` + mut := `mutation newCreateJobTest($sessionIDs: [ID!]!, $input: CreateJobInput!) { createJob(sessionIDs:$sessionIDs, input:$input) { + id + tasks { + id + } + } }` // Make our request to the GraphQL API var resp struct { - CreateJob struct{ ID string } + CreateJob struct { + ID string + Tasks []struct { + ID string + } `json:"tasks"` + } } err := gqlClient.Post(mut, &resp, client.Var("sessionIDs", sessionIDs), diff --git a/tavern/graphql/mutation.resolvers.go b/tavern/graphql/mutation.resolvers.go index 6d0d5ee2f..7c3e35e59 100644 --- a/tavern/graphql/mutation.resolvers.go +++ b/tavern/graphql/mutation.resolvers.go @@ -83,6 +83,12 @@ func (r *mutationResolver) CreateJob(ctx context.Context, sessionIDs []int, inpu return nil, rollback(tx, fmt.Errorf("failed to commit transaction: %w", err)) } + // 9. Load the job with our non transactional client (cannot use transaction after commit) + job, err = r.client.Job.Get(ctx, job.ID) + if err != nil { + return nil, fmt.Errorf("failed to load created job: %w", err) + } + return job, nil } @@ -167,7 +173,7 @@ func (r *mutationResolver) ClaimTasks(ctx context.Context, input models.ClaimTas // 7. Update all ClaimedAt timestamps to claim tasks // ** Note: If one fails to update, we roll back the transaction and return the error - result := make([]*ent.Task, 0, len(tasks)) + taskIDs := make([]int, 0, len(tasks)) for _, t := range tasks { _, err := client.Task.UpdateOne(t). SetClaimedAt(time.Now()). @@ -175,7 +181,7 @@ func (r *mutationResolver) ClaimTasks(ctx context.Context, input models.ClaimTas if err != nil { return nil, rollback(tx, fmt.Errorf("failed to update task %d: %w", t.ID, err)) } - result = append(result, t) + taskIDs = append(taskIDs, t.ID) } // 8. Commit the transaction @@ -183,7 +189,17 @@ func (r *mutationResolver) ClaimTasks(ctx context.Context, input models.ClaimTas return nil, rollback(tx, fmt.Errorf("failed to commit transaction: %w", err)) } - // 9. Return claimed tasks + // 9. Load the tasks with our non transactional client (cannot use transaction after commit) + result := make([]*ent.Task, 0, len(taskIDs)) + for _, taskID := range taskIDs { + updatedTask, err := r.client.Task.Get(ctx, taskID) + if err != nil { + return nil, fmt.Errorf("failed to load updated task (but they were still updated) %d: %w", taskID, err) + } + result = append(result, updatedTask) + } + + // 10. Return claimed tasks return result, nil } diff --git a/tavern/graphql/session_test.go b/tavern/graphql/session_test.go index 4671fca0e..a1baea8d6 100644 --- a/tavern/graphql/session_test.go +++ b/tavern/graphql/session_test.go @@ -74,14 +74,22 @@ func TestSessionMutations(t *testing.T) { mut := ` mutation newClaimTasksTest($input: ClaimTasksInput!) { claimTasks(input: $input) { - id + id + job { + id + } } }` // Create a closure to execute the mutation claimTasks := func(input map[string]any) ([]int, error) { // Make our request to the GraphQL API var resp struct { - ClaimTasks []struct{ ID string } + ClaimTasks []struct { + ID string + Job struct { + ID string + } `json:"job"` + } } err := gqlClient.Post(mut, &resp, client.Var("input", input)) if err != nil { @@ -163,7 +171,7 @@ mutation newClaimTasksTest($input: ClaimTasksInput!) { mut := ` mutation newSubmitTaskResultTest($input: SubmitTaskResultInput!) { submitTaskResult(input: $input) { - id + id } }` // Create a closure to execute the mutation