Dgraph raw upsert in javascript http client

I’ll give this a whirl.

EDIT:

That did not work. :frowning:

{
  error: TypeError: c[e].toArray is not a function
      at Function.jspb.Message.setRepeatedWrapperField (/home/node/app/node_modules/google-protobuf/google-protobuf.js:552:180)
      at proto.api.Request.setMutationsList (/home/node/app/node_modules/dgraph-js/generated/api_pb.js:729:23)
      at Txn.<anonymous> (/home/node/app/node_modules/dgraph-js/lib/txn.js:136:21)
      at step (/home/node/app/node_modules/dgraph-js/lib/txn.js:44:23)
      at Object.next (/home/node/app/node_modules/dgraph-js/lib/txn.js:25:53)
      at /home/node/app/node_modules/dgraph-js/lib/txn.js:19:71
      at new Promise (<anonymous>)
      at __awaiter (/home/node/app/node_modules/dgraph-js/lib/txn.js:15:12)
      at Txn.mutate (/home/node/app/node_modules/dgraph-js/lib/txn.js:131:16)
      at Dgraph.doUpsert (/home/node/app/libs/api/dgraph/src/services/Dgraph.ts:173:28),

The last line is our extended Dgraph class where I added the doUpsert method like you posted above:


  async doUpsert(upsert: string): Promise<Response> {
    if (!upsert.startsWith("upsert")) {
      const error = 'Upsert must start with "upsert".'
      this.logger.error(error, { data: { upsert } })
      throw new Error(error);
    }
    const txn = this.newTxn(); // this is dgraph-js default DgraphClient.newTxn
    let response: Response;

    try {
      // type casting needed because `mutation: string` is not in the Mutation class ??
      response = await txn.mutate({ mutation: upsert, commitNow: true } as unknown as Mutation);
    } catch (error) {
      throw error;
    } finally {
      await txn.discard();
    }
    return response;
  }

Not a big deal, I will just do it the other way. Just wanted to update this to see if you see an error anywhere in my snippets?