2017 © Pedro Peláez
 

library graphql-language

A GraphQL parser written in PHP

image

hansott/graphql-language

A GraphQL parser written in PHP

  • Thursday, December 14, 2017
  • by hansott
  • Repository
  • 1 Watchers
  • 5 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 7 Versions
  • 6 % Grown

The README.md

GraphQL Language

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads], (*1)

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. 🙈, (*2)

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

Install

Via Composer, (*4)

``` bash $ composer require hansott/graphql-language, (*5)


## Usage ### Parsing a query to a [HansOtt\GraphQL\Query\Document](src/Query/Document.php) ``` php 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., (*6)

Testing

bash $ composer test, (*7)

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*8)

Security

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

Credits

License

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

The Versions

14/12 2017

dev-master

9999999-dev https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

01/12 2017

dev-merge-parsers

dev-merge-parsers https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

23/09 2017

1.3.1

1.3.1.0 https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

23/09 2017

1.3.0

1.3.0.0 https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

17/09 2017

1.2.0

1.2.0.0 https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

17/09 2017

1.1.0

1.1.0.0 https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott

16/09 2017

1.0.0

1.0.0.0 https://github.com/hansott/graphql-language

A GraphQL parser written in PHP

  Sources   Download

MIT

The Development Requires

parser graphql hansott