PHPackages                             zestic/dgraph-php-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. zestic/dgraph-php-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

zestic/dgraph-php-client
========================

A Dgraph PHP Client using GRPC

0481PHPCI failing

Since May 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/zestic/dgraph-php-client)[ Packagist](https://packagist.org/packages/zestic/dgraph-php-client)[ RSS](/packages/zestic-dgraph-php-client/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

dgraph-client
=============

[](#dgraph-client)

A PHP client for dgraph using gRPC protocol.

### Installation

[](#installation)

Before using this client, we highly recommend that you go through [tour.dgraph.io](https://tour.dgraph.io) and [docs.dgraph.io](https://docs.dgraph.io)to understand how to run and work with Dgraph.

Table of contents
-----------------

[](#table-of-contents)

- [Install](#install)
- [Using a client](#using-a-client)
    - [Create a client](#create-a-client)
    - [Alter the database](#alter-the-database)
    - [Create a transaction](#create-a-transaction)
    - [Run a mutation](#run-a-mutation)
    - [Run a query](#run-a-query)
    - [Commit a transaction](#commit-a-transaction)
- [Development](#development)
    - [Running tests](#running-tests)

Install
-------

[](#install)

You'll need PECL installed and install both the sync and grpc packages.

```
sudo pecl install grpc
sudo pecl install sync

```

In your project

```
composer require dgraph

```

Using a client
--------------

[](#using-a-client)

### Create a client

[](#create-a-client)

`Dgraph` object can be initialised by passing it a list of `DgraphClient` 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.

```
$hostname = 'localhost:9080';
$options = [
    'credentials' => Grpc\ChannelCredentials::createInsecure(),
];
$client = new DgraphClient($hostname, $options);

$dgraph = new Dgraph([$client]);
```

### Alter the database

[](#alter-the-database)

To set the schema, create an instance of `Operation` and use the `Alter` endpoint.

```
$schema = "name: string @index(exact) .";
$operation = new Operation();
$operation->setSchema($schema);

$dgraph->alter($operation);
```

`Operation` contains other fields as well, including `DropAttr` and `DropAll`. `DropAll` is useful if you wish to discard all the data, and start from a clean slate, without bringing the instance down. `DropAttr` is used to drop all the data related to a predicate.

### Create a transaction

[](#create-a-transaction)

To create a transaction, call `DgraphClient::newTxn()`, which returns a `Txn` object. This operation incurs no network overhead.

It is a good practice to call `Txn::Discard()` using a `finally` statement after it is initialized. Calling `Txn::Discard()` after `Txn::Commit()` is a no-op and you can call `Txn::Discard()` multiple times with no additional side-effects.

```
$txn = $dgraph->newTxn($dgraph);
  try {
    // Do something here
    // ...
  } finally {
    $txn->discard();
  }
```

### Run a mutation

[](#run-a-mutation)

`Txn::mutate()` 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're going to use JSON. We will set the properties and values in an array, encode it and use it in `Mutation` object.

```
// Create data
$personProperties = [
    'id' => '_alice',
    'name' => 'Alice',
];
$person = json_encode($personProperties);

// Run mutation
$mutation = (new Mutation())
    ->setSetJson($person);

$txn->mutate($mutation);

$txn->commit();
```

Sometimes, you only want to commit mutation, without querying anything further. In such cases, you can use a `CommitNow` field in `Mutation` object to indicate that the mutation must be immediately committed.

The `IgnoreIndexConflict` flag can be set to `true` on the `Mutation` object to not run conflict detection over the index, which would decrease the number of transaction conflicts and aborts. However, this would come at the cost of potentially inconsistent upsert operations.

### Run a query

[](#run-a-query)

You can run a query by calling `Txn::query($query)`. 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($query, $vars)` with the variables map as third argument.

Let's run the following query with a variable $a:

```
$query = 'query all($a: string) {
    all(func: eq(name, $a)) {
      name
    }
  }';

$response = $txn->ueryWithVars($query, ['$a' => 'Alice']);

echo $response->getJson();
```

### Commit a transaction

[](#commit-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 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.

```
$txn = dgraphClient->newTxn();
// Perform some queries and mutations.

$context = $txn->commit();
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/bd5a2c6deedf2f52379d21fa55c9f5c6d0999b0ac1ca2ca2f33293e229107bfe?d=identicon)[iampersistent](/maintainers/iampersistent)

---

Top Contributors

[![linicode](https://avatars.githubusercontent.com/u/7208146?v=4)](https://github.com/linicode "linicode (1 commits)")

### Embed Badge

![Health badge](/badges/zestic-dgraph-php-client/health.svg)

```
[![Health](https://phpackages.com/badges/zestic-dgraph-php-client/health.svg)](https://phpackages.com/packages/zestic-dgraph-php-client)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
