Skip to content

zymawy/laravel-dgraph

Repository files navigation

Fluint API for dgraph

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require zymawy/dgraph

You can publish and run the migrations with:

php artisan vendor:publish --tag="dgraph-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="dgraph-config"

This is the contents of the published config file:

return [
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="dgraph-views"

Usage

Create a Client

use Zymawy\Dgraph\DgraphClient;

/** @var DgraphClient $client */
$client = new DgraphClient('http://localhost:8080');

Define and Alter Schema

use Zymawy\Dgraph\Api\Operation;
use Zymawy\Dgraph\Exceptions\DgraphException;
use Zymawy\Dgraph\Types\StringType;
use Zymawy\Dgraph\Types\IntType;
use Zymawy\Dgraph\DgraphClient;

/** @var DgraphClient $client */
$client = new DgraphClient('http://localhost:8080');

// Define the schema using the Operation class
$operation = new Operation();
$operation
  ->addField("name", new StringType(["index(term)"]))
  ->addField("age", new IntType(["index(int)"]))
  ->addType("person", ["name", "age"]);

try {
    $response = $client->alter($operation);
    if ($response->hasErrors()) {
        throw new DgraphException("Schema alteration error: " . json_encode($response->getErrors()));
    } else {
        echo "Schema successfully altered.";
    }
} catch (DgraphException $e) {
    echo "Error: " . $e->getMessage();
}

Add Initial Data

use Zymawy\Dgraph\DgraphClient;
use Zymawy\Dgraph\Txn;
use Zymawy\Dgraph\Responses\DgraphResponse;
use Zymawy\Dgraph\Exceptions\DgraphException;
use Zymawy\Dgraph\Types\StringType;
use Zymawy\Dgraph\Types\IntType;
/** @var DgraphClient $client */
$client = new DgraphClient("http://localhost:8080");

use Zymawy\Dgraph\Api\Mutation;
use Zymawy\Dgraph\Exceptions\DgraphException;

/** @var DgraphClient $client */
$client = new DgraphClient("http://localhost:8080");

$mutation = new Mutation();
$mutation->set([
  [
    "uid" => "_:account1",
    "name" => "Hamza",
    "balance" => 1000.0
  ],
  [
    "uid" => "_:account2",
    "name" => "Zymawy",
    "balance" => 500.0
  ]
]);

try {
  $response = $client->mutate($mutation, true); // commitNow set to true
  if ($response->hasErrors()) {
    throw new DgraphException(
      "Mutation error: " . json_encode($response->getErrors())
    );
  } else {
    print_r($response->getData());
  }
} catch (DgraphException $e) {
  echo "Error: " . $e->getMessage();
}

Run a Basic Query

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published