From 46816a7cdad856ac310c721c0d5c37de8dc54e23 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Thu, 8 Feb 2018 15:30:30 +0100 Subject: [PATCH] Uniform parsing of queries with short-hand syntax with regular queries --- src/Language/Parser.php | 2 +- tests/Language/ParserTest.php | 77 ++++++++++++++++++++++++++- tests/Language/kitchen-sink-noloc.ast | 1 + tests/Language/kitchen-sink.ast | 1 + 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/Language/Parser.php b/src/Language/Parser.php index 2a7c7f0..b15b048 100644 --- a/src/Language/Parser.php +++ b/src/Language/Parser.php @@ -370,7 +370,7 @@ class Parser return new OperationDefinitionNode([ 'operation' => 'query', 'name' => null, - 'variableDefinitions' => null, + 'variableDefinitions' => new NodeList([]), 'directives' => new NodeList([]), 'selectionSet' => $this->parseSelectionSet(), 'loc' => $this->loc($start) diff --git a/tests/Language/ParserTest.php b/tests/Language/ParserTest.php index d901bf6..1a872c0 100644 --- a/tests/Language/ParserTest.php +++ b/tests/Language/ParserTest.php @@ -276,7 +276,7 @@ fragment $fragmentName on Type { 'loc' => $loc(0, 40), 'operation' => 'query', 'name' => null, - 'variableDefinitions' => null, + 'variableDefinitions' => [], 'directives' => [], 'selectionSet' => [ 'kind' => NodeKind::SELECTION_SET, @@ -350,6 +350,81 @@ fragment $fragmentName on Type { $this->assertEquals($expected, $this->nodeToArray($result)); } + /** + * @it creates ast from nameless query without variables + */ + public function testParseCreatesAstFromNamelessQueryWithoutVariables() + { + $source = new Source('query { + node { + id + } +} +'); + $result = Parser::parse($source); + + $loc = function($start, $end) use ($source) { + return [ + 'start' => $start, + 'end' => $end + ]; + }; + + $expected = [ + 'kind' => NodeKind::DOCUMENT, + 'loc' => $loc(0, 30), + 'definitions' => [ + [ + 'kind' => NodeKind::OPERATION_DEFINITION, + 'loc' => $loc(0, 29), + 'operation' => 'query', + 'name' => null, + 'variableDefinitions' => [], + 'directives' => [], + 'selectionSet' => [ + 'kind' => NodeKind::SELECTION_SET, + 'loc' => $loc(6, 29), + 'selections' => [ + [ + 'kind' => NodeKind::FIELD, + 'loc' => $loc(10, 27), + 'alias' => null, + 'name' => [ + 'kind' => NodeKind::NAME, + 'loc' => $loc(10, 14), + 'value' => 'node' + ], + 'arguments' => [], + 'directives' => [], + 'selectionSet' => [ + 'kind' => NodeKind::SELECTION_SET, + 'loc' => $loc(15, 27), + 'selections' => [ + [ + 'kind' => NodeKind::FIELD, + 'loc' => $loc(21, 23), + 'alias' => null, + 'name' => [ + 'kind' => NodeKind::NAME, + 'loc' => $loc(21, 23), + 'value' => 'id' + ], + 'arguments' => [], + 'directives' => [], + 'selectionSet' => null + ] + ] + ] + ] + ] + ] + ] + ] + ]; + + $this->assertEquals($expected, $this->nodeToArray($result)); + } + /** * @it allows parsing without source location information */ diff --git a/tests/Language/kitchen-sink-noloc.ast b/tests/Language/kitchen-sink-noloc.ast index 0f37560..3236586 100644 --- a/tests/Language/kitchen-sink-noloc.ast +++ b/tests/Language/kitchen-sink-noloc.ast @@ -575,6 +575,7 @@ { "kind": "OperationDefinition", "operation": "query", + "variableDefinitions": [], "directives": [], "selectionSet": { "kind": "SelectionSet", diff --git a/tests/Language/kitchen-sink.ast b/tests/Language/kitchen-sink.ast index 9c89af7..c0128f1 100644 --- a/tests/Language/kitchen-sink.ast +++ b/tests/Language/kitchen-sink.ast @@ -1127,6 +1127,7 @@ "end": 1086 }, "operation": "query", + "variableDefinitions": [], "directives": [], "selectionSet": { "kind": "SelectionSet",