DgraphClient
object 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_namespace()
, which allows the users to login
to a specific namespace.
In order to create a python client, and make the client login into namespace
123
:
123
using username
groot
and password password
. Once logged in, the client can perform all the
operations allowed to the groot
user of namespace 123
.
Operation
object, set the schema and pass it to
DgraphClient#alter(Operation)
method.
run_in_background
field of pydgraph.Operation
to True
before passing it to the Alter
function. You can find more details
here.
Operation
contains other fields as well, including the drop
predicate and
drop all
. Drop all is useful if you wish to discard all the data, and start
with a clean slate, without bringing the instance down.
DgraphClient#txn()
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.
DgraphClient#txn(read_only=True)
.
Read-only transactions are ideal for transactions which only involve queries.
Mutations and commits arenβt allowed.
DgraphClient#txn(read_only=True, best_effort=True)
. Best-effort queries are
faster than normal queries because they bypass the normal consensus protocol.
For this same reason, best-effort queries canβt guarantee to return the latest
data. Best-effort queries are only supported by read-only transactions.
Txn#mutate(mu=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.
Txn#mutate()
provides convenience keyword arguments set_obj
and del_obj
for setting JSON values and set_nquads
and del_nquads
for setting N-Quad
values. See examples below for usage.
We define a person object to represent a person and use it in a transaction.
examples
folder.
Sometimes, you only want to commit a mutation, without querying anything
further. In such cases, you can set the keyword argument commit_now=True
to
indicate that the mutation must be immediately committed.
A mutation can be executed using txn.do_request
as well.
Txn#commit()
method. If your
transaction consist solely of Txn#query
or Txn#queryWithVars
calls, and no
calls to Txn#mutate
, then calling Txn#commit()
isnβt necessary.
An error is raised if another transaction modifies the same data concurrently
that was modified in the current transaction. It is up to the user to retry
transactions when they fail.
Txn#query(string)
. You need to pass in a
DQL query string. If you want to pass an additional
dictionary of any variables that you might want to set in the query, call
Txn#query(string, variables=d)
with the variables dictionary d
.
The query response contains the json
field, which returns the JSON response.
Letβs run a query with a variable $a
, deserialize the result from JSON and
print it out:
txn.do_request
function to run the query.
txn.do_request
function allows you to use upsert blocks. An upsert block
contains one query block and one or more mutation blocks, so it lets you perform
queries and mutations in a single request. Variables defined in the query block
can be used in the mutation blocks using the uid
and val
functions
implemented by DQL.
To learn more about upsert blocks, see the
Upsert Block documentation.
@if
directive. The mutation is executed only when the specified condition is
true. If the condition is false, the mutation is silently ignored.
See more about Conditional Upserts
here.
DgraphClientStub#close()
individually
for all the instances of DgraphClientStub
.
auth-token
.
login
,
alter
, query
, and mutate
methods using the timeout
keyword argument.
For example, the following alters the schema with a timeout of ten seconds:
dg.alter(op, timeout=10)
CallCredentials
object can be passed to the login
, alter
, query
, and
mutate
methods using the credentials
keyword argument.
authorization
.
alter
method in the client has an asynchronous version called
async_alter
. The async methods return a future. You can directly call the
result
method on the future. However. The DgraphClient class provides a static
method handle_alter_future
to handle any possible exception.
query
and mutate
methods int the Txn
class also have async versions
called async_query
and async_mutation
respectively. These functions work
just like async_alter
.
You can use the handle_query_future
and handle_mutate_future
static methods
in the Txn
class to retrieve the result. A short example is given below:
test_asycn.py
test file.
Keep in mind that due to the nature of async calls, the async functions cannot
retry the request if the login is invalid. You will have to check for this error
and retry the login (with the function retry_login
in both the Txn
and
Client
classes). A short example is given below: