Uniform parsing of queries with short-hand syntax with regular queries

This commit is contained in:
Daniel Tschinder 2018-02-08 15:30:30 +01:00
parent eb9ac66af8
commit 46816a7cda
4 changed files with 79 additions and 2 deletions

View File

@ -370,7 +370,7 @@ class Parser
return new OperationDefinitionNode([ return new OperationDefinitionNode([
'operation' => 'query', 'operation' => 'query',
'name' => null, 'name' => null,
'variableDefinitions' => null, 'variableDefinitions' => new NodeList([]),
'directives' => new NodeList([]), 'directives' => new NodeList([]),
'selectionSet' => $this->parseSelectionSet(), 'selectionSet' => $this->parseSelectionSet(),
'loc' => $this->loc($start) 'loc' => $this->loc($start)

View File

@ -276,7 +276,7 @@ fragment $fragmentName on Type {
'loc' => $loc(0, 40), 'loc' => $loc(0, 40),
'operation' => 'query', 'operation' => 'query',
'name' => null, 'name' => null,
'variableDefinitions' => null, 'variableDefinitions' => [],
'directives' => [], 'directives' => [],
'selectionSet' => [ 'selectionSet' => [
'kind' => NodeKind::SELECTION_SET, 'kind' => NodeKind::SELECTION_SET,
@ -350,6 +350,81 @@ fragment $fragmentName on Type {
$this->assertEquals($expected, $this->nodeToArray($result)); $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 * @it allows parsing without source location information
*/ */

View File

@ -575,6 +575,7 @@
{ {
"kind": "OperationDefinition", "kind": "OperationDefinition",
"operation": "query", "operation": "query",
"variableDefinitions": [],
"directives": [], "directives": [],
"selectionSet": { "selectionSet": {
"kind": "SelectionSet", "kind": "SelectionSet",

View File

@ -1127,6 +1127,7 @@
"end": 1086 "end": 1086
}, },
"operation": "query", "operation": "query",
"variableDefinitions": [],
"directives": [], "directives": [],
"selectionSet": { "selectionSet": {
"kind": "SelectionSet", "kind": "SelectionSet",