hasura / hasura/graphql-engine

Delete mutations returning empty nested relations

Open
#9,432 0 comments 0 reactions 0 assignees View on GitHub
k/bug t/native-dbs
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: v2.12.1
CLI Version (for CLI related issue): -

### Environment

run on local machine in Docker

### What is the current behaviour?

When executing a delete mutation and trying to include relations under the `returning` clause, the relations returned are empty.

This affects both delete mutations:
- one row by PK
- many rows using a where clause

### What is the expected behaviour?

A delete mutation should return all relations specified under the `returning` clause.

### How to reproduce the issue?

1. Create an [Author table](https://github.com/hasura/article-author-schema/blob/main/hasura/migrations/default/1632999721726_create_table_public_author/up.sql) and an [Article table](https://github.com/hasura/article-author-schema/blob/main/hasura/migrations/default/1632999801197_create_table_public_article/up.sql), but change the foreign key constraint between the two tables to `ON DELETE CASCADE`
2. Track the tables in Hasura
3. Insert some test data for an author with 1 or 2 articles
4. Try to delete it again using the Console or the API:
```gql
mutation MyMutation {
delete_authors_by_pk(id: 1) { # ensure using an existing ID, obviously
name
articles {
title
}
articles_aggregate {
aggregate {
count
}
}
}
}

```
5. Observe that the `name` is correct, but `articles` are empty and `articles_aggregate` shows a `count` of 0:
```json
{
"data": {
"delete_authors_by_pk": {
"name": "testing",
"articles": [],
"articles_aggregate": {
"aggregate": {
"count": 0
}
}
}
}
}
```

### Screenshots or Screencast

N/A

### Please provide any traces or logs that could help here.

```
{"detail":{"http_info":{"content_encoding":"gzip","http_version":"HTTP/1.1","ip":"172.18.0.1","method":"POST","status":200,"url":"/v1/graphql"},"operation":{"query_execution_time":1.2061694e-2,"request_id":"19515095-00f4-4965-92f4-32bcb0194ba3","request_mode":"single","request_read_time":3.223e-6,"response_size":105,"user_vars":{"x-hasura-role":"admin"}},"request_id":"19515095-00f4-4965-92f4-32bcb0194ba3"},"level":"info","timestamp":"2023-02-14T04:42:44.658+0000","type":"http-log"}
```

### Any possible solutions/workarounds you're aware of?

A workaround might be to run an update and delete mutation together in single mutation (transaction) for the same record(s). If the update mutation fetches the relations of the record being deleted in the second mutation, the combined information of both mutations would be equivalent to the desired outcome.

### Keywords

- delete
- mutation
- nested
- returning

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the linked Author and Article migrations, including the ON DELETE CASCADE constraint, then run the shown delete_authors_by_pk mutation and compare it with the many-row delete using a where clause. Done means both delete mutations return the requested nested articles and articles_aggregate data rather than empty relations.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql
Domain
api, backend-api-design, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.