Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Commit

Permalink
✨ Add schema declaration parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Sep 23, 2017
1 parent 4dab093 commit 43c72e3
Show file tree
Hide file tree
Showing 37 changed files with 1,662 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Changelog

All Notable changes to `hansott/graphql-language` will be documented in this file.
See [releases](https://github.com/hansott/graphql-language/releases).
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,73 @@ $traverser->traverse($document);
var_dump($finder->getQueries());
```

### Parsing a schema declaration to a [HansOtt\GraphQL\Schema\Schema](src/Schema/Schema.php)

```php
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](CHANGELOG.md) for more information on what has changed recently.
Expand Down
24 changes: 24 additions & 0 deletions src/Schema/Argument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class Argument extends NodeBase
{
public $name;
public $type;
public $defaultValue;

/**
* @param string $name
* @param Type $type
* @param Value|null $defaultValue
* @param Location|null $location
*/
public function __construct($name, Type $type, Value $defaultValue = null, Location $location = null)
{
parent::__construct($location);
$this->name = (string) $name;
$this->type = $type;
$this->defaultValue = $defaultValue;
}
}
7 changes: 7 additions & 0 deletions src/Schema/Declaration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace HansOtt\GraphQL\Schema;

interface Declaration extends Node
{
}
21 changes: 21 additions & 0 deletions src/Schema/DeclarationEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class DeclarationEnum extends NodeBase implements Declaration
{
public $name;
public $possibleValues;

/**
* @param string $name
* @param string[] $possibleValues
* @param Location|null $location
*/
public function __construct($name, array $possibleValues, Location $location = null)
{
parent::__construct($location);
$this->name = $name;
$this->possibleValues = $possibleValues;
}
}
26 changes: 26 additions & 0 deletions src/Schema/DeclarationInputObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class DeclarationInputObject extends NodeBase implements Declaration
{
public $name;
public $fields;

/**
* @param string $name
* @param array $fields
* @param Location|null $location
*/
public function __construct($name, array $fields, Location $location = null)
{
parent::__construct($location);
$this->name = $name;
$this->fields = $fields;
}

public function getChildren()
{
return $this->fields;
}
}
26 changes: 26 additions & 0 deletions src/Schema/DeclarationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class DeclarationInterface extends NodeBase implements Declaration
{
public $name;
public $fields;

/**
* @param string $name
* @param Field[] $fields
* @param Location|null $location
*/
public function __construct($name, array $fields, Location $location = null)
{
parent::__construct($location);
$this->name = (string) $name;
$this->fields = $fields;
}

public function getChildren()
{
return $this->fields;
}
}
29 changes: 29 additions & 0 deletions src/Schema/DeclarationObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class DeclarationObject extends NodeBase implements Declaration
{
public $name;
public $fields;
public $interface;

/**
* @param string $name
* @param Field[] $fields
* @param string $interface
* @param Location|null $location
*/
public function __construct($name, array $fields, $interface = null, Location $location = null)
{
parent::__construct($location);
$this->name = (string) $name;
$this->fields = $fields;
$this->interface = $interface;
}

public function getChildren()
{
return $this->fields;
}
}
21 changes: 21 additions & 0 deletions src/Schema/DeclarationUnion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class DeclarationUnion extends NodeBase implements Declaration
{
public $name;
public $members;

/**
* @param string $name
* @param string[] $members
* @param Location|null $location
*/
public function __construct($name, array $members, Location $location = null)
{
parent::__construct($location);
$this->name = (string) $name;
$this->members = $members;
}
}
29 changes: 29 additions & 0 deletions src/Schema/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HansOtt\GraphQL\Schema;

final class Field extends NodeBase
{
public $name;
public $type;
public $arguments;

/**
* @param string $name
* @param Type $type
* @param Argument[] $arguments
* @param Location|null $location
*/
public function __construct($name, Type $type, array $arguments = array(), Location $location = null)
{
parent::__construct($location);
$this->name = (string) $name;
$this->type = $type;
$this->arguments = $arguments;
}

public function getChildren()
{
return $this->arguments;
}
}
Loading

0 comments on commit 43c72e3

Please sign in to comment.