Nested Hasura GraphQL Upsert mutation is there a way to stop nesting on conflict?

Viewed 506

I use Hasura and I have a social-network like situation. In which I have a "User" object and a "Feed" object. Every user has a feed. I have a relationship from user.id to feed.id.

The relevant mutation is UpsertUserDetails as follows:

  mutation UserDetailsUpsert(
    $email: String!
    $picture: String
  ) {
    insert_users_one(
      object: {
        email: $email
        feed: { data: {} }
        picture: $picture
      }
      on_conflict: { constraint: users_tid_email_key, update_columns: [picture] }
    ) {
      id
    }
  }

So when I create a new user it also creates a feed for it.

But when I only update user details I don't want it to create a new feed. I would like to stop the upsert from going through to relationships instead of the above default behavior.

and according to this manual I don't see if its even possible: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/upsert.html#upsert-in-nested-mutations

To allow upserting in nested cases, set update_columns: []. By doing this, in case of a conflict, the conflicted column/s will be updated with the new value (which is the same values as they had before and hence will effectively leave them unchanged) and will allow the upsert to go through.

Thanks!

1 Answers

I'd recommend that you design your schema such that bad data cannot be entered in the first place. You can put partial unique indices on the feed table in order to prevent duplicate feeds from ever being created. Since you have both users and groups you can implement it with 2 partial indices.

CREATE UNIQUE INDEX unique_feed_per_user ON feed (user_id)
    WHERE user_id IS NOT NULL;

CREATE UNIQUE INDEX unique_feed_per_group ON feed (group_id)
    WHERE group_id IS NOT NULL;
Related