Is it possible to upsert using graph-js http client


I am trying to create an upsert with http client using the following code

import { dgraphClient } from "./index";
const dgraph = require("dgraph-js-http");

const req = new dgraph.Request();

  const query = `query {
    q(func: eq(Node.title, "${title}")) {
      p as uid

  const mu = new dgraph.Mutation();
  mu.setSetNquads( `_:page <Node.title>  "${title}" .`);
  mu.setCond(`@if(eq(len(p), 0)`);


  const res = await dgraphClient.newTxn().doRequest(req);

  return res;

The client is properly initialized and I am able to make other queries, but when I run this code (which I basically copied from here–mutation I get the following error:

TypeError: dgraph.Request is not a constructor

Any guidance on the right way of doing this would be highly appreciated. Thank you :pray:

The problem seems to stem from the second line,

const dgraph = require("dgraph-js-http");

The example on dgraph-js github, uses

const dgraph = require("dgraph-js");

dgraph-js-http and dgraph-js are different dgraph clients, one is meant for browser support while the other is for supporting javascript with gRPC.


  1. dgraph-js: GitHub - dgraph-io/dgraph-js: Official Dgraph JavaScript client
  2. dgraph-js-http: GitHub - dgraph-io/dgraph-js-http: A JavaScript HTTP client for Dgraph
1 Like

:man_facepalming: You are correct about that part.

However, I want to call this from a browser so I am not able to use the gRPC client. Is there a way to achieve this using the HTTP client?

I see this closed PR but I am not sure what is not right about it and why it got closed.

Thanks for pointing that out. We have noted this issue.

As a workaround, you may carry out an upsert by running a query and mutation separately. There is an example of this in tryUpsert function of dgraph-js-http/acctUpsert.spec.ts at master · dgraph-io/dgraph-js-http · GitHub

1 Like

Thank you @rajas for the answer :pray: I was able to make it work using the test example.

This however does not illustrate how to implement a conditional upsert directly in DQL (without relying on javascript), as it is the case with the example you provided.