@@ -22,12 +22,13 @@ and understand how to run and work with Dgraph.
2222- [ Install] ( #install )
2323- [ Quickstart] ( #quickstart )
2424- [ Using a client] ( #using-a-client )
25- - [ Create a client] ( #create-a-client )
26- - [ Alter the database] ( #alter-the-database )
27- - [ Create a transaction] ( #create-a-transaction )
28- - [ Run a mutation] ( #run-a-mutation )
29- - [ Run a query] ( #run-a-query )
30- - [ Commit a transaction] ( #commit-a-transaction )
25+ - [ Creating a client] ( #creating-a-client )
26+ - [ Altering the database] ( #altering-the-database )
27+ - [ Creating a transaction] ( #creating-a-transaction )
28+ - [ Running a mutation] ( #running-a-mutation )
29+ - [ Running a query] ( #running-a-query )
30+ - [ Running an Upsert: Query + Mutation] ( #running-an-upsert-query--mutation )
31+ - [ Committing a transaction] ( #committing-a-transaction )
3132 - [ Cleanup Resources] ( #cleanup-resources )
3233 - [ Debug mode] ( #debug-mode )
3334- [ Examples] ( #examples )
@@ -59,9 +60,21 @@ Build and run the [simple][] project in the `examples` folder, which
5960contains an end-to-end example of using the Dgraph JavaScript client. Follow the
6061instructions in the README of that project.
6162
63+ Depending on the version of Dgraph that you are connecting to, you will have to
64+ use a different version of this client.
65+
66+ | Dgraph version | dgraph-js version |
67+ | :--------------:| :-----------------:|
68+ | 1.0.X | * 1.X.Y* |
69+ | 1.1.X | * 2.X.Y* |
70+
71+ Note: Only API breakage from * v1.X.Y* to * v2.X.Y* is in
72+ the function ` DgraphClient.newTxn().mutate() ` . This function returns a ` messages.Assigned `
73+ type in * v1.X* but a ` messages.Response ` type in * v2.X* .
74+
6275## Using a client
6376
64- ### Create a client
77+ ### Creating a client
6578
6679A ` DgraphClient ` object can be initialised by passing it a list of
6780` DgraphClientStub ` clients as variadic arguments. Connecting to multiple Dgraph
@@ -84,7 +97,7 @@ const dgraphClient = new dgraph.DgraphClient(clientStub);
8497
8598To facilitate debugging, [ debug mode] ( #debug-mode ) can be enabled for a client.
8699
87- ### Alter the database
100+ ### Altering the database
88101
89102To set the schema, create an ` Operation ` object, set the schema and pass it to
90103` DgraphClient#alter(Operation) ` method.
@@ -118,7 +131,7 @@ op.setDropAll(true);
118131await dgraphClient .alter (op);
119132```
120133
121- ### Create a transaction
134+ ### Creating a transaction
122135
123136To create a transaction, call ` DgraphClient#newTxn() ` method, which returns a
124137new ` Txn ` object. This operation incurs no network overhead.
@@ -138,7 +151,7 @@ try {
138151}
139152```
140153
141- ### Run a mutation
154+ ### Running a mutation
142155
143156` Txn#mutate(Mutation) ` runs a mutation. It takes in a ` Mutation ` object, which
144157provides two main ways to set data: JSON and RDF N-Quad. You can choose whichever
@@ -170,7 +183,20 @@ not run conflict detection over the index, which would decrease the number of
170183transaction conflicts and aborts. However, this would come at the cost of potentially
171184inconsistent upsert operations.
172185
173- ### Run a query
186+ Mutation can be run using ` txn.doRequest ` as well.
187+
188+ ``` js
189+ const mu = new dgraph.Mutation ();
190+ mu .setSetJson (q);
191+
192+ const req = new dgraph.Request ();
193+ req .setCommitNow (true );
194+ req .setMutationsList ([mu]);
195+
196+ await txn .doRequest (req);
197+ ```
198+
199+ ### Running a query
174200
175201You can run a query by calling ` Txn#query(string) ` . You will need to pass in a
176202GraphQL+- query string. If you want to pass an additional map of any variables that
@@ -218,7 +244,37 @@ Number of people named "Alice": 1
218244Alice
219245```
220246
221- ### Commit a transaction
247+ You can also use ` txn.doQuery ` function to run the query.
248+ ``` js
249+ const req = new dgraph.Request ();
250+ const vars = req .getVarsMap ();
251+ vars .set (" $a" , " Alice" );
252+ req .setQuery (query);
253+
254+ const res = await txn .doRequest (req);
255+ console .log (JSON .stringify (res .getJson ()));
256+ ```
257+
258+ ### Running an Upsert: Query + Mutation
259+ ``` js
260+ const query = `
261+ query {
262+ user as var(func: eq(email, "[email protected] ")) 263+ }`
264+
265+ const mu = new dgraph.Mutation ();
266+ mu .
setSetNquads (
` uid(user) <email> "[email protected] " .` );
267+
268+ const req = new dgraph.Request ();
269+ req .setQuery (query);
270+ req .setMutationsList ([mu]);
271+ req .setCommitNow (true );
272+
273+ // Update email only if matching uid found.
274+ await dgraphClient .newTxn ().doQuery (req);
275+ ```
276+
277+ ### Committing a transaction
222278
223279A transaction can be committed using the ` Txn#commit() ` method. If your transaction
224280consisted solely of calls to ` Txn#query ` or ` Txn#queryWithVars ` , and no calls to
0 commit comments