Search code examples
reactjsgraphqlapolloapollo-clienthasura

Apollo Client: Mutation optional parameter without changing db entries of none set variables to null


I'm using Apollo Client with (hasura as backend if that matters) and have the following mutation:

  mutation SetArbitrarySubset(
    $id: String!
    $foo: String
    $bar: String
  ) {
    update_table(
      where: { id: { _eq: $id } }
      _set: {
        foo: $foo
        bar: $bar
      }
    ) {
      affected_rows
    }
  }

if i won't set foo or bar the mutation results in setting unset variables to null in the DB is there any way to preserve the previous entry without writing permutations for each case?


Solution

  • Unfortunately if you don't leave the values blank they are going to be set to null based on the definition for your mutation. There are few options you could consider:

    • Make individual mutations
    • If you have access to the original values for $foo and $bar when you are calling the mutation you can pass those in instead of leaving them blank
    • You could create a Postgres function that handles the nulls properly and expose it as a Hasura mutation