I have an update User mutation where the name, ema...
# orm-help
j
I have an update User mutation where the name, email and body are requried:
Copy code
return ctx.db.mutation.updateUser(
  {
    where: { id },
    data: {
      name,
      email,
      body,
    },
  },
  info,
);
Is there a smart way to make the name, email and body optional, and only update them if a value has been passed? I could do it with some if statements but it makes my soul feel dirty:
Copy code
if (name) {
	ctx.db.mutation.updateUser(
	  {
	    where: { id },
	    data: {
	      name,
	    },
	  },
	  info,
	);
}
if (email) {
	ctx.db.mutation.updateUser(
	  {
	    where: { id },
	    data: {
	      email,
	    },
	  },
	  info,
	);
}
if (body) {
	ctx.db.mutation.updateUser(
	  {
	    where: { id },
	    data: {
	      email,
	    },
	  },
	  info,
	);
}
j
Afaik when the value provided to data is undefined it's ignored and not updated.
Copy code
const f = (root, { id, ...rest }, { db }, info) =>
    db.mutation.updateUser({
        where: {
            id
        },
        data: {
            ...rest
        }
    }, info);
now you can have
Copy code
type Mutation {
    f(
        id: ID!
        name: String
        email: String
        body: String
    ): User
}
👍 2