hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.1k stars 2.77k forks source link

[Feature request] Allow mutations to update to a specific JSONB path #2793

Open calvinl opened 5 years ago

calvinl commented 5 years ago

It would be great to be able to update nested data in a JSONB column without overwriting the values as _set would currently do.

Ideally, this would work for both arrays and objects, with arrays using the index of the array element as the path to update. There are probably many ways to do this, but here's one idea I've documented below:

For example, given a table called users with a JSONB column info which has the following data:

{
  "name": "Billy",
  "email": "billy@foo.com",
  "contacts": [
    {
      "name": "Bob",
      "email": "bob@foo.com"
    },
    {
      "name": "Joe",
      "email": "joe@foo.com"
    }
  ]
}

Then the following query:

mutation {
  users_update(
    where: { id: { _eq: 1 } },
    _update: {
      info: {
        email: 'billy_updated@foo.com',
        contacts: {
          1: { name: 'Not Joe' }
        }
      }
    }
  ) {
    id
    info
  }
}

would update the info column to be:

{
  "name": "Billy",
  "email": "billy_updated@foo.com",
  "contacts": [
    {
      "name": "Bob",
      "email": "bob@foo.com"
    },
    {
      "name": "Not Joe",
      "email": "joe@foo.com"
    }
  ]
}
trivedi commented 1 year ago

Hi - is there any update on this? Would be super helpful. I think we're looking for the postgres equivalent of || so something like

set config = config || '{."existingKey": "newValue", "newKey": "newValue" }'::jsonb