Skip to content

Commit 022c962

Browse files
committed
New language features (NamedType, directives rethinking)
1 parent 698b2cb commit 022c962

27 files changed

+366
-223
lines changed

src/Error.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Error extends \Exception
4141
* @param array|null $nodes
4242
* @return Error
4343
*/
44-
public static function createLocatedError($error, array $nodes = null)
44+
public static function createLocatedError($error, $nodes = null)
4545
{
4646
if ($error instanceof \Exception) {
4747
$message = $error->getMessage();
@@ -56,11 +56,11 @@ public static function createLocatedError($error, array $nodes = null)
5656

5757
/**
5858
* @param Error $error
59-
* @return FormattedError
59+
* @return array
6060
*/
6161
public static function formatError(Error $error)
6262
{
63-
return new FormattedError($error->getMessage(), $error->getLocations());
63+
return FormattedError::create($error->getMessage(), $error->getLocations());
6464
}
6565

6666
/**
@@ -69,12 +69,17 @@ public static function formatError(Error $error)
6969
* @param Source $source
7070
* @param null $positions
7171
*/
72-
public function __construct($message, array $nodes = null, \Exception $previous = null, Source $source = null, $positions = null)
72+
public function __construct($message, $nodes = null, \Exception $previous = null, Source $source = null, $positions = null)
7373
{
7474
parent::__construct($message, 0, $previous);
7575

76+
if ($nodes instanceof \Traversable) {
77+
$nodes = iterator_to_array($nodes);
78+
}
79+
7680
$this->nodes = $nodes;
7781
$this->source = $source;
82+
$this->positions = $positions;
7883
}
7984

8085
/**

src/FormattedError.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
<?php
22
namespace GraphQL;
33

4+
use GraphQL\Language\SourceLocation;
5+
46
class FormattedError
57
{
68
/**
7-
* @var string
8-
*/
9-
public $message;
10-
11-
/**
12-
* @var array<Language\SourceLocation>
13-
*/
14-
public $locations;
15-
16-
/**
17-
* @param $message
18-
* @param array<Language\SourceLocation> $locations
19-
*/
20-
public function __construct($message, $locations = [])
21-
{
22-
$this->message = $message;
23-
$this->locations = array_map(function($loc) { return $loc->toArray();}, $locations);
24-
}
25-
26-
/**
9+
* @param $error
10+
* @param SourceLocation[] $locations
2711
* @return array
2812
*/
29-
public function toArray()
13+
public static function create($error, array $locations = [])
3014
{
31-
return [
32-
'message' => $this->message,
33-
'locations' => $this->locations
15+
$formatted = [
16+
'message' => $error
3417
];
18+
19+
if (!empty($locations)) {
20+
$formatted['locations'] = array_map(function($loc) { return $loc->toArray();}, $locations);
21+
}
22+
23+
return $formatted;
3524
}
3625
}

src/Language/AST/Argument.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
<?php
22
namespace GraphQL\Language\AST;
33

4-
class Argument extends Node
4+
class Argument extends NamedType
55
{
66
public $kind = Node::ARGUMENT;
77

8-
/**
9-
* @var Name
10-
*/
11-
public $name;
12-
138
/**
149
* @var Value
1510
*/

src/Language/AST/Directive.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Directive extends Node
1111
public $name;
1212

1313
/**
14-
* @var Value
14+
* @var Argument[]
1515
*/
16-
public $value;
16+
public $arguments;
1717
}

src/Language/AST/Field.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace GraphQL\Language\AST;
33

4-
class Field extends Node
4+
class Field extends NamedType
55
{
66
public $kind = Node::FIELD;
77

@@ -10,11 +10,6 @@ class Field extends Node
1010
*/
1111
public $alias;
1212

13-
/**
14-
* @var Name
15-
*/
16-
public $name;
17-
1813
/**
1914
* @var array<Argument>|null
2015
*/

src/Language/AST/FragmentDefinition.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
namespace GraphQL\Language\AST;
33

44

5-
class FragmentDefinition extends Node implements Definition
5+
class FragmentDefinition extends NamedType implements Definition
66
{
77
public $kind = Node::FRAGMENT_DEFINITION;
88

99
/**
10-
* @var Name
11-
*/
12-
public $name;
13-
14-
/**
15-
* @var Name
10+
* @var NamedType
1611
*/
1712
public $typeCondition;
1813

src/Language/AST/FragmentSpread.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
<?php
22
namespace GraphQL\Language\AST;
33

4-
class FragmentSpread extends Node
4+
class FragmentSpread extends NamedType
55
{
66
public $kind = Node::FRAGMENT_SPREAD;
77

8-
/**
9-
* @var Name
10-
*/
11-
public $name;
12-
138
/**
149
* @var array<Directive>
1510
*/

src/Language/AST/InlineFragment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class InlineFragment extends Node
66
public $kind = Node::INLINE_FRAGMENT;
77

88
/**
9-
* @var Name
9+
* @var NamedType
1010
*/
1111
public $typeCondition;
1212

src/Language/AST/ArrayValue.php renamed to src/Language/AST/ListValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace GraphQL\Language\AST;
33

4-
class ArrayValue extends Node implements Value
4+
class ListValue extends Node implements Value
55
{
6-
public $kind = Node::ARR;
6+
public $kind = Node::LST;
77

88
/**
99
* @var array<Value>

src/Language/AST/NamedType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace GraphQL\Language\AST;
3+
4+
class NamedType extends Node
5+
{
6+
public $kind = Node::NAMED_TYPE;
7+
8+
/**
9+
* @var Name
10+
*/
11+
public $name;
12+
}

0 commit comments

Comments
 (0)