Drop Type with dgraph-js client error

I am trying to drop a type that is no longer being used with the dgraph-js (grps) client.

const dropType(toDrop: string): Promise<boolean> => {
  const op = new Operation();
  op.setDropValue(toDrop);
  await dgraph.alter(op);
  return true;
}

But I am getting the error

Error: 2 UNKNOWN: Operation must have at least one field set

What am I missing?

I thought maybe it was because the type was already dropped in a previous run, but I still set it in ratel, and I even added a query to get schema for the type in this same function and it shows it exists.

The maddening part: Lack of Documentation!

There is literally no example of dropping a type in either js client. In fact the only example of dropping a type at all anywhere in the docs (I can find) is using Cloud API and the Go client.

We figured this out for my future self:

The .setDropValue is a “dumb” method that does not know that it wants to drop a type. I suppose that maybe somewhere (undocumented at this time) the .setDropValue method might drop a value that is not a type :confused: But for now, the only use I see is dropping a type. So the fix is to add another method on the op to tell is what kind of data you are dropping with the value. For this it is:

const dropType(toDrop: string): Promise<boolean> => {
  const op = new Operation();
  op.setDropOp(Operation.DropOp.Type);
  op.setDropValue(toDrop);
  await dgraph.alter(op);
  return true;
}

I really wish this class was fine-tuned to not need this extra step with maybe a new method setDropType that internally does both the setDropOp and setDropValue methods together. Ar at least, it would be better if these could be chained with something like:

const dropType(toDrop: string): Promise<boolean> => {
  const op = new Operation();
  op.setDropOp(Operation.DropOp.Type).setDropValue(toDrop);
  await dgraph.alter(op);
  return true;
}

But this ^^ does not work either as each method retuns void instead of returning the class itself to chain more.

Hi,
You can use

const op = new dgraph.Operation();
op.setDropOp(4);
op.setDropValue(typeName);

it’s work for me(with dgraph-js), i found it exploring the code source

Sad you have to explore source code for things that should be clear in the documentation.

…And that works now until the values of those properties are changed possibly in the future (highly unlikely now though). It would be safer to use the explicit property mapping to ensure future-proofing.