amin
(Amin Benselim)
November 8, 2020, 1:16pm
1
Hello,
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
}
}`;
req.setQuery(query);
const mu = new dgraph.Mutation();
mu.setSetNquads( `_:page <Node.title> "${title}" .`);
mu.setCond(`@if(eq(len(p), 0)`);
req.set(mu);
req.setCommitNow(true);
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 https://github.com/dgraph-io/dgraph-js#running-an-upsert-query–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
rajas
(Rajas Vanjape)
November 8, 2020, 1:28pm
2
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.
References:
dgraph-js: GitHub - dgraph-io/dgraph-js: Official Dgraph JavaScript client
dgraph-js-http: GitHub - dgraph-io/dgraph-js-http: A JavaScript HTTP client for Dgraph
1 Like
amin
(Amin Benselim)
November 8, 2020, 1:49pm
3
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 https://github.com/dgraph-io/dgraph-js-http/pull/13 but I am not sure what is not right about it and why it got closed.
rajas
(Rajas Vanjape)
November 8, 2020, 2:26pm
4
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
amin
(Amin Benselim)
November 8, 2020, 3:46pm
6
Thank you @rajas for the answer 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.