Skip to content
This repository was archived by the owner on Apr 24, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[/tests/parsing/*]
indent_size = 2
36 changes: 36 additions & 0 deletions bin/graphql-parser
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php

use HansOtt\GraphQL\Query\ParserFactory;

foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}

if (isset($argv[1]) === false) {
echo "Pass GraphQL code or a path to a file.\n";
exit(1);
}

$graphql = $argv[1];

if (file_exists($graphql) && is_file($graphql)) {
$graphql = file_get_contents($graphql);
}

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

try {
$document = $parser->parse($graphql);
} catch (\HansOtt\GraphQL\Shared\SyntaxError $e) {
echo "Syntax Error: {$e->getMessage()}\n";
exit(1);
} catch (\HansOtt\GraphQL\Shared\ParseError $e) {
echo "Parse Error: {$e->getMessage()}\n";
exit(1);
}

var_dump($document);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
},
"config": {
"sort-packages": true
}
},
"bin": ["bin/graphql-parser"]
}
20 changes: 12 additions & 8 deletions src/Query/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,25 @@ private function str()

private function integerPart()
{
$number = $this->scanner->next();
if ($this->scanner->eof()) {
throw $this->getError('Expected an integer or a float but instead reached end');
}

$location = $this->getLocation();
if ($number === '-') {
$next = $this->scanner->peek();
$number = '';
if ($next === '-') {
$number .= $next;
$this->scanner->next();
if ($this->scanner->eof()) {
throw $this->getError('Expected a digit but instead reached end');
}
$next = $this->scanner->peek();
if (ctype_digit($next) === false) {
throw $this->getError("Expected a digit but instead found \"{$next}\"");
throw $this->getError('Expected an integer or a float but instead reached end');
}
}

$next = $this->scanner->peek();
if ($next === '0') {
$number .= $this->scanner->next();
$number .= $next;
$this->scanner->next();
return array($number, $location);
}

Expand Down
16 changes: 14 additions & 2 deletions src/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,12 @@ private function parseDefinition()

if ($this->is(Token::T_MUTATION)) {
$location = $this->expect(Token::T_MUTATION)->location;
$name = $this->expect(Token::T_NAME)->value;

$name = null;
if ($this->is(Token::T_NAME)) {
$name = $this->expect(Token::T_NAME)->value;
}

$variables = $this->parseVariableDefinitionList();
$directives = $this->parseDirectiveList();
$selectionSet = $this->parseSelectionSet();
Expand All @@ -379,7 +384,12 @@ private function parseDefinition()

if ($this->is(Token::T_SUBSCRIPTION)) {
$location = $this->expect(Token::T_SUBSCRIPTION)->location;
$name = $this->expect(Token::T_NAME)->value;

$name = null;
if ($this->is(Token::T_NAME)) {
$name = $this->expect(Token::T_NAME)->value;
}

$variables = $this->parseVariableDefinitionList();
$directives = $this->parseDirectiveList();
$selectionSet = $this->parseSelectionSet();
Expand Down Expand Up @@ -432,6 +442,8 @@ private function parseDocument()
/**
* @param string $query
*
* @throws \HansOtt\GraphQL\Shared\SyntaxError
*
* @return Document
*/
public function parse($query)
Expand Down
40 changes: 40 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace HansOtt\GraphQL;

use PHPUnit\Framework\TestCase;

final class ParserTest extends TestCase
{
public function getParseTests()
{
$files = glob(__DIR__.'/parsing/*');
$cases = array();
foreach ($files as $file) {
$contents = file_get_contents($file);
$parts = explode('----', $contents);
if (count($parts) !== 2) {
$this->fail("{$file} is not a valid test case");
}
list ($graphql, $expectedOutput) = $parts;
$pathInfo = pathinfo($file);
$cases[] = array($graphql, trim($expectedOutput), $pathInfo['filename']);
}

return $cases;
}

/**
* @dataProvider getParseTests
*
* @param string $graphql
* @param string $expectedOutput
* @param string $message
*/
public function test_it_parses_graphql($graphql, $expectedOutput, $message)
{
$graphql = escapeshellarg($graphql);
$output = shell_exec(__DIR__."/../bin/graphql-parser {$graphql}");
$this->assertEquals($expectedOutput, trim($output), $message);
}
}
163 changes: 163 additions & 0 deletions tests/parsing/test_it_parses_a_mutation_like_story
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
mutation {
likeStory(storyID: 12345) {
story {
likeCount
}
}
}
----
object(HansOtt\GraphQL\Query\Document)#47 (2) {
["definitions"]=>
array(1) {
[0]=>
object(HansOtt\GraphQL\Query\OperationMutation)#46 (5) {
["name"]=>
NULL
["variables"]=>
array(0) {
}
["directives"]=>
array(0) {
}
["selectionSet"]=>
object(HansOtt\GraphQL\Query\SelectionSet)#45 (2) {
["selections"]=>
array(1) {
[0]=>
object(HansOtt\GraphQL\Query\SelectionField)#44 (6) {
["name"]=>
string(9) "likeStory"
["alias"]=>
NULL
["arguments"]=>
array(1) {
[0]=>
object(HansOtt\GraphQL\Query\Argument)#39 (3) {
["name"]=>
string(7) "storyID"
["value"]=>
object(HansOtt\GraphQL\Query\ValueInt)#38 (2) {
["value"]=>
int(12345)
["location"]=>
object(HansOtt\GraphQL\Query\Location)#18 (2) {
["line"]=>
int(2)
["column"]=>
int(22)
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#14 (2) {
["line"]=>
int(2)
["column"]=>
int(14)
}
}
}
["directives"]=>
array(0) {
}
["selectionSet"]=>
object(HansOtt\GraphQL\Query\SelectionSet)#43 (2) {
["selections"]=>
array(1) {
[0]=>
object(HansOtt\GraphQL\Query\SelectionField)#42 (6) {
["name"]=>
string(5) "story"
["alias"]=>
NULL
["arguments"]=>
array(0) {
}
["directives"]=>
array(0) {
}
["selectionSet"]=>
object(HansOtt\GraphQL\Query\SelectionSet)#41 (2) {
["selections"]=>
array(1) {
[0]=>
object(HansOtt\GraphQL\Query\SelectionField)#40 (6) {
["name"]=>
string(9) "likeCount"
["alias"]=>
NULL
["arguments"]=>
array(0) {
}
["directives"]=>
array(0) {
}
["selectionSet"]=>
NULL
["location"]=>
object(HansOtt\GraphQL\Query\Location)#28 (2) {
["line"]=>
int(4)
["column"]=>
int(8)
}
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#26 (2) {
["line"]=>
int(3)
["column"]=>
int(12)
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#24 (2) {
["line"]=>
int(3)
["column"]=>
int(6)
}
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#22 (2) {
["line"]=>
int(2)
["column"]=>
int(30)
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#10 (2) {
["line"]=>
int(2)
["column"]=>
int(4)
}
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#8 (2) {
["line"]=>
int(1)
["column"]=>
int(10)
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#6 (2) {
["line"]=>
int(1)
["column"]=>
int(1)
}
}
}
["location"]=>
object(HansOtt\GraphQL\Query\Location)#6 (2) {
["line"]=>
int(1)
["column"]=>
int(1)
}
}
Loading