We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
The official JavaScript HTTP client can be found
here. Follow the install
instructions to get it
up and running.
Supported versions
More details on the supported versions can be found at this link.Quickstart
Build and run the simple project, which contains an end-to-end example of using the Dgraph JavaScript HTTP client. Follow the instructions in the README of that project.Using a client
You can find a simple
example
project, which contains an end-to-end working example of how to use the
JavaScript HTTP client, for Node.js >= v6.
Create a client
ADgraphClient 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 into Dgraph
If your Dgraph server has Access Control Lists enabled (Dgraph v1.1 or above), the clientStub must be logged in for accessing data: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:
Configure access tokens
Some Dgraph configurations require extra access tokens. Alpha servers can be configured with Secure Alter Operations. In this case the token needs to be set on the client instance:Create https connection
If your cluster is using TLS/mTLS you can pass a nodehttps.Agent configured
with you certificates as follows:
Alter the database
To set the schema, pass the schema toDgraphClient#alter(Operation) method.
NOTE: Many of the examples here use theawaitkeyword which requiresasync/awaitsupport which isn’t available in all javascript environments. For unsupported environments, the expressions followingawaitcan be used just like normalPromiseinstances.
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.
Create a transaction
To create a transaction, callDgraphClient#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:
Run a mutation
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.
Run a query
You can run a query by callingTxn#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:
Commit a transaction
A transaction can be committed using theTxn#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.
Check request latency
To see the server latency information for requests, check theextensions.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.
Debug mode
Debug mode can be used to print helpful debug messages while performing alters, queries and mutations. It can be set using theDgraphClient#setDebugMode(boolean?) method.