hansott/graphql-language

This package is abandoned and no longer maintained. The author suggests using the webonyx/graphql-php package instead.

A GraphQL parser written in PHP

1.3.1 2017-09-23 19:06 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:09:51 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

A GraphQL parser written in PHP. The parser is able to parse the full spec. This package is compatible with PHP 5.3+. I'm still actively working on this project. Expect things to break. 🙈

This package is being developed together with a server package, which isn't public yet.

Install

Via Composer

$ composer require hansott/graphql-language

Usage

Parsing a query to a HansOtt\GraphQL\Query\Document

use HansOtt\GraphQL\Query\ParseError;
use HansOtt\GraphQL\Query\SyntaxError;
use HansOtt\GraphQL\Query\ParserFactory;

$factory = new ParserFactory;
$parser = $factory->create();

$query = <<<'QUERY'
    {
        author(id: 1) {
            name
        }
    }
QUERY;

try {
    $document = $parser->parse($query);
    var_dump($document); // Instance of HansOtt\GraphQL\Query\Document
} catch (SyntaxError $e) {
    echo "Syntax error in query: {$e->getMessage()}" . PHP_EOL;
} catch (ParseError $e) {
    echo "Failed to parse query: {$e->getMessage()}" . PHP_EOL;
}

Traversing a HansOtt\GraphQL\Query\Document

use HansOtt\GraphQL\Query\Node;
use HansOtt\GraphQL\Query\Traverser;
use HansOtt\GraphQL\Query\VisitorBase;
use HansOtt\GraphQL\Query\OperationQuery;

final class VisitorQueryFinder extends VisitorBase // Or implement HansOtt\GraphQL\Query\Visitor
{
    /**
     * @var OperationQuery[]
     */
    private $queries = [];

    public function enterNode(Node $node)
    {
        if ($node instanceof OperationQuery) {
            $this->queries[] = $node;
        }
    }

    public function getQueries()
    {
        return $this->queries;
    }
}

$document = $parser->parse($query);
$finder = new VisitorQueryFinder;

$traverser = new Traverser($finder);
$traverser->traverse($document);
var_dump($finder->getQueries());

// Or if you need multiple visitors
// use HansOtt\GraphQL\Query\VisitorMany

$visitors = new VisitorMany([$finder, ...]);
$traverser = new Traverser($visitors);
$traverser->traverse($document);
var_dump($finder->getQueries());

Parsing a schema declaration to a HansOtt\GraphQL\Schema\Schema

use HansOtt\GraphQL\Schema\ParseError;
use HansOtt\GraphQL\Schema\SyntaxError;
use HansOtt\GraphQL\Schema\ParserFactory;

$factory = new ParserFactory;
$parser = $factory->create();

$schema = <<<'SCHEMA'
    enum DogCommand { SIT, DOWN, HEEL }
    
    type Dog implements Pet {
        name: String!
        nickname: String
        barkVolume: Int
        doesKnowCommand(dogCommand: DogCommand!): Boolean!
        isHousetrained(atOtherHomes: Boolean): Boolean!
        owner: Human
    }
    
    interface Sentient {
        name: String!
    }
    
    interface Pet {
        name: String!
    }
    
    type Alien implements Sentient {
        name: String!
        homePlanet: String
    }
    
    type Human implements Sentient {
        name: String!
    }
    
    enum CatCommand { JUMP }
    
    type Cat implements Pet {
        name: String!
        nickname: String
        doesKnowCommand(catCommand: CatCommand!): Boolean!
        meowVolume: Int
    }
    
    union CatOrDog = Cat | Dog
    union DogOrHuman = Dog | Human
    union HumanOrAlien = Human | Alien
    
    type QueryRoot {
        dog: Dog
    }
SCHEMA;

try {
    $schema = $parser->parse($schema);
    var_dump($schema); // Instance of HansOtt\GraphQL\Schema\Schema
} catch (SyntaxError $e) {
    echo "Syntax error in query: {$e->getMessage()}" . PHP_EOL;
} catch (ParseError $e) {
    echo "Failed to parse query: {$e->getMessage()}" . PHP_EOL;
}

Change log

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

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email hansott at hotmail be instead of using the issue tracker.

Credits

License

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