Skip to main content

Official Dgraph client implementation for Python

Project description


Official Dgraph client implementation for Python (Python >= v2.7 and >= v3.5), using grpc.

This client follows the Dgraph Go client closely.

Before using this client, we highly recommend that you go through, and understand how to run and work with Dgraph.

Table of contents


Install using pip:

pip install pydgraph

Supported Versions

Depending on the version of Dgraph that you are connecting to, you will have to use a different version of this client.

Dgraph version pydgraph version
1.0.X <= 1.2.0
1.1.X >= 2.0.0
1.2.X >= 2.0.0


Build and run the simple project in the examples folder, which contains an end-to-end example of using the Dgraph python client. Follow the instructions in the README of that project.

Using a client

Creating a Client

You can initialize a DgraphClient object by passing it a list of DgraphClientStub clients as variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better distribution of workload.

The following code snippet shows just one connection.

import pydgraph

client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

Altering the Database

To set the schema, create an Operation object, set the schema and pass it to DgraphClient#alter(Operation) method.

schema = 'name: string @index(exact) .'
op = pydgraph.Operation(schema=schema)

Starting Dgraph version 20.03.0, indexes can be computed in the background. You can set run_in_background field of the pydgraph.Operation to True before passing it to the Alter function. You can find more details here.

schema = 'name: string @index(exact) .'
op = pydgraph.Operation(schema=schema, run_in_background=True)

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.

# Drop all data including schema from the Dgraph instance. This is a useful
# for small examples such as this since it puts Dgraph into a clean state.
op = pydgraph.Operation(drop_all=True)

Creating a Transaction

To create a transaction, call 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.

txn = client.txn()
  # Do something here
  # ...
  # ...

To create a read-only transaction, call DgraphClient#txn(read_only=True). Read-only transactions are ideal for transactions which only involve queries. Mutations and commits are not allowed.

txn = client.txn(read_only=True)
  # Do some queries here
  # ...
  # ...

To create a read-only transaction that executes best-effort queries, call 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 cannot guarantee to return the latest data. Best-effort queries are only supported by read-only transactions.

Running a Mutation

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.

# Create data.
p = {
    'name': 'Alice',

# Run mutation.

# If you want to use a mutation object, use this instead:
# mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
# txn.mutate(mu)

# If you want to use N-Quads, use this instead:
# txn.mutate(set_nquads='_:alice <name> "Alice" .')
# Delete data.

query = """query all($a: string)
   all(func: eq(name, $a))

variables = {'$a': 'Bob'}

res = txn.query(query, variables=variables)
ppl = json.loads(res.json)

# For a mutation to delete a node, use this:

For a complete example with multiple fields and relationships, look at the simple project in the 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.

mutation = txn.create_mutation(set_nquads='_:alice <name> "Alice" .')
request = txn.create_request(mutations=[mutation], commit_now=True)

Committing a Transaction

A transaction can be committed using the 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 is raised if another transaction(s) modify the same data concurrently that was modified in the current transaction. It is up to the user to retry transactions when they fail.

txn = client.txn()
  # ...
  # Perform any number of queries and mutations
  # ...
  # and finally...
except Exception as e:
  if isinstance(e, pydgraph.AbortedError):
    # Retry or handle exception.
    raise e
  # Clean up. Calling this after txn.commit() is a no-op
  # and hence safe.

Running a Query

You can run a query by calling Txn#query(string). You will need to pass in a GraphQL+- 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 response would contain the field json, which returns the response JSON.

Let’s run a query with a variable $a, deserialize the result from JSON and print it out:

# Run query.
query = """query all($a: string) {
  all(func: eq(name, $a))
variables = {'$a': 'Alice'}

res = txn.query(query, variables=variables)

# If not doing a mutation in the same transaction, simply use:
# res = client.txn(read_only=True).query(query, variables=variables)

ppl = json.loads(res.json)

# Print results.
print('Number of people named "Alice": {}'.format(len(ppl['all'])))
for person in ppl['all']:

This should print:

Number of people named "Alice": 1

You can also use txn.do_request function to run the query.

request = txn.create_request(query=query)

Running an Upsert: Query + Mutation

The txn.do_request function allows you to run upserts consisting of one query and one mutation. Query variables could be defined and can then be used in the mutation.

To know more about upsert, we highly recommend going through the docs at

query = """{
  u as var(func: eq(name, "Alice"))
nquad = """
  uid(u) <name> "Alice" .
  uid(u) <age> "25" .
mutation = txn.create_mutation(set_nquads=nquad)
request = txn.create_request(query=query, mutations=[mutation], commit_now=True)

Running a Conditional Upsert

The upsert block also allows specifying a conditional mutation block using an @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 Upsert Here.

query = """
    user as var(func: eq(email, ""))
cond = "@if(eq(len(user), 1))"
nquads = """
  uid(user) <email> "" .
mutation = txn.create_mutation(cond=cond, set_nquads=nquads)
request = txn.create_request(mutations=[mutation], query=query, commit_now=True)

Cleaning Up Resources

To clean up resources, you have to call DgraphClientStub#close() individually for all the instances of DgraphClientStub.

SERVER_ADDR = "localhost:9080"

# Create instances of DgraphClientStub.
stub1 = pydgraph.DgraphClientStub(SERVER_ADDR)
stub2 = pydgraph.DgraphClientStub(SERVER_ADDR)

# Create an instance of DgraphClient.
client = pydgraph.DgraphClient(stub1, stub2)

# ...
# Use client
# ...

# Clean up resources by closing all client stubs.

Setting Metadata Headers

Metadata headers such as authentication tokens can be set through the metadata of gRPC methods. Below is an example of how to set a header named "auth-token".

# The following piece of code shows how one can set metadata with
# auth-token, to allow Alter operation, if the server requires it.
# metadata is a list of arbitrary key-value pairs.
metadata = [("auth-token", "the-auth-token-value")]
dg.alter(op, metadata=metadata)

Setting a timeout.

A timeout value representing the number of seconds can be passed to the 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)

Passing credentials

A CallCredentials object can be passed to the login, alter, query, and mutate methods using the credentials keyword argument.

Async methods.

The alter method in the client has an asyncronous 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.

alter_future = self.client.async_alter(pydgraph.Operation(
	schema="name: string @index(term) ."))
response = pydgraph.DgraphClient.handle_alter_future(alter_future)

The 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:

txn = client.txn()
query = "query body here"
future = txn.async_query()
response = pydgraph.Txn.handle_query_future(future)

A working example can be found in the 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:

client = DgraphClient(client_stubs) # client_stubs is a list of gRPC stubs.
alter_future = client.async_alter()
    response = alter_future.result()
except Exception as e:
	# You can use this function in the util package to check for JWT
    # expired errors.
    if pydgraph.util.is_jwt_expired(e):
        # retry your request here.


  • simple: Quickstart example of using pydgraph.


Building the source

python install
# To install for the current user, use this instead:
# python install --user

If you have made changes to the pydgraph/proto/api.proto file, you need need to regenerate the source files generated by Protocol Buffer tools. To do that, install the grpcio-tools library and then run the following command:

python scripts/

The generated file needs to be changed in recent versions of python. The required change is outlined below as a diff.

-import api_pb2 as api__pb2
+from . import api_pb2 as api__pb2

Running tests

To run the tests in your local machine, you can run the script scripts/ This script assumes Dgraph and dgo (Go client) are already built on the local machine and that their code is in $GOPATH/src. It also requires that docker and docker-compose are installed in your machine.

The script will take care of bringing up a Dgraph cluster and bringing it down after the tests are executed. The script uses the port 9180 by default to prevent interference with clusters running on the default port. Docker and docker-compose need to be installed before running the script. Refer to the official Docker documentation for instructions on how to install those packages.

The script downloads and installs Dgraph. It is meant for use by our CI systems and using it for local development is not recommended.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pydgraph, version 20.3.1
Filename, size File type Python version Upload date Hashes
Filename, size pydgraph-20.3.1-py2.py3-none-any.whl (25.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size pydgraph-20.3.1.tar.gz (28.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page