juststeveking/neo4j-http-adapter

A slim psr compliant http client

v1.0 2020-05-28 14:13 UTC

This package is auto-updated.

Last update: 2024-10-19 22:52:48 UTC


README

Latest Version on Packagist run-tests Quality Score Total Downloads

This package is still a work in progress, what I have tested works so far but should not be used in a production environment yet

The purpose of this package is to provide a clean and easy way to work with the neo4j HTTP API v4 to run Cypher queries and return data.

Installation

Using composer:

$ composer require juststeveking/neo4j-http-adapter

You are then free to use it as needed within your projects.

Usage

Using this library is relatively simple.

Prepare your Adapter

The adapter build method accepts 2 parameters, connection string for your neo4j database and a database name that may be null.

Please note that an Exception will be thrown if your connection string does not start with either http or https

<?php

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$adapter = HttpAdapter::build(
    'http://neo4j:password@localhost:7474',
    'my_database'
);

Selecting a database to query against

<?php

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$adapter = HttpAdapter::build(
    'http://neo4j:password@localhost:7474',
    'my_database'
);

$database = $adapter->on('database-name');

Starting to build up a transaction

Once you have an active adapter, and have chosen your database, you will want to start running queries against it.

<?php

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$adapter = HttpAdapter::build(
    'http://neo4j:password@localhost:7474',
    'my_database'
);

$database = $adapter->on('database-name');

// Add a query to the transaction pipeline.
$database->query('MATCH (person:Person) WHERE person.name = "Tom Hanks" RETURN person');

// Add another query to the transaction pipeline
$database->query('MATCH (film:Film) WHERE film.name = "Forrest Gump" RETURN film');

From here we are free to send this transaction to the server

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$adapter = HttpAdapter::build(
    'http://neo4j:password@localhost:7474',
    'my_database'
);

$database = $adapter->on('database-name');

// Add a query to the transaction pipeline.
$database->query('MATCH (person:Person) WHERE person.name = "Tom Hanks" RETURN person');

$response = $database->send();

A cleaner approach

The point with this package was to allow you to build up your queries and send them as you see fit, not how I think they should be handled. Here is an alternative method for sending queries:

<?php

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$adapter = HttpAdapter::build(
    'http://neo4j:password@localhost:7474',
    null
);

$actorsDatabase = $adapter->on('actors');

$tomHanks = $actorsDatabase->query('MATCH (person:Person) WHERE person.name = "Tom Hanks" RETURN person')->send();

Or if you would prefer to chain parts of the process:

<?php

use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$tomHanks = HttpsAdapter::build(
    'http://neo4j:password@localhost:7474',
    'actors'
)->query('MATCH (person:Person) WHERE person.name = "Tom Hanks" RETURN person')->send();

If you want to use my other packages also, you can query like below:

<?php

use JustSteveKing\Graph\Builder\Cypher;
use JustSteveKing\Graph\Connection\ConnectionManager;
use JustSteveKing\Graph\Connection\Adapters\Neo4j\Adapters\Http\HttpAdapter;

$connection = ConnectionManager::create(HttpAdapter::build('http://neo4j:password@localhost:7474', null));
$query = Cypher::query()->match('Person', 'person')->where('person', 'name', '=', 'Tom Hanks')->return('person');
$connection->use('neo-http')->on('neo4j')->query($query)->send();

Tests

There is a composer script available to run the tests:

$ composer run test

However, if you are unable to run this please use the following command:

$ ./vendor/bin/phpunit --testdox

Security

If you discover any security related issues, please email juststevemcd@gmail.com instead of using the issue tracker.