DgraphClient
object can be initialized by passing it a list of
DgraphClientStub
clients as arguments. Connecting to multiple Dgraph servers
in the same cluster allows for better distribution of workload.
The following code snippet shows just one connection.
login
will obtain and remember the access and refresh JWT tokens. All
subsequent operations via the logged in clientStub
will send along the stored
access token.
Access tokens expire after 6 hours, so in long-lived apps (e.g. business logic
servers) you need to login
again on a periodic basis:
https.Agent
configured
with you certificates as follows:
DgraphClient#alter(Operation)
method.
NOTE: Many of the examples here use theawait
keyword which requiresasync/await
support which isnβt available in all javascript environments. For unsupported environments, the expressions followingawait
can be used just like normalPromise
instances.
Operation
contains other fields as well, including drop predicate and drop
all. Drop all is useful if you wish to discard all the data, and start from a
clean slate, without bringing the instance down.
DgraphClient#newTxn()
method, which returns a
new Txn
object. This operation incurs no network overhead.
It is good practice to call Txn#discard()
in a finally
block after running
the transaction. Calling Txn#discard()
after Txn#commit()
is a no-op and you
can call Txn#discard()
multiple times with no additional side-effects.
options
to
DgraphClient#newTxn
. For example:
Txn#mutate(Mutation)
runs a mutation. It takes in a Mutation
object, which
provides two main ways to set data: JSON and RDF N-Quad. You can choose
whichever way is convenient.
We define a person object to represent a person and use it in a Mutation
object.
examples
folder.
For setting values using N-Quads, use the setNquads
field. For delete
mutations, use the deleteJson
and deleteNquads
fields for deletion using
JSON and N-Quads respectively.
Sometimes, you only want to commit a mutation, without querying anything
further. In such cases, you can use Mutation#commitNow = true
to indicate that
the mutation must be immediately committed.
Txn#query(string)
. You will need to pass in a
GraphQL+- query string. If you want to pass an additional map of any variables
that you might want to set in the query, call
Txn#queryWithVars(string, object)
with the variables object as the second
argument.
The response would contain the data
field, Response#data
, which returns the
response JSON.
Letβs run the following query with a variable $a:
Txn#commit()
method. If your
transaction consisted solely of calls to Txn#query
or Txn#queryWithVars
, and
no calls to Txn#mutate
, then calling Txn#commit()
is not necessary.
An error will be returned if other transactions running concurrently modify the
same data that was modified in this transaction. It is up to the user to retry
transactions when they fail.
extensions.server_latency
field from the Response object for queries or from
the Assigned object for mutations. These latencies show the amount of time the
Dgraph server took to process the entire request. It does not consider the time
over the network for the request to reach back to the client.
DgraphClient#setDebugMode(boolean?)
method.