|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ConversionException; |
|
6
|
|
|
use Digia\GraphQL\Error\InvalidTypeException; |
|
7
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
8
|
|
|
use Digia\GraphQL\Language\Node\ListTypeNode; |
|
9
|
|
|
use Digia\GraphQL\Language\Node\NamedTypeNode; |
|
10
|
|
|
use Digia\GraphQL\Language\Node\NonNullTypeNode; |
|
11
|
|
|
use Digia\GraphQL\Language\Node\TypeNodeInterface; |
|
12
|
|
|
use Digia\GraphQL\Schema\SchemaInterface; |
|
13
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
|
14
|
|
|
use function Digia\GraphQL\Type\newList; |
|
15
|
|
|
use function Digia\GraphQL\Type\newNonNull; |
|
16
|
|
|
|
|
17
|
|
|
class TypeASTConverter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Given a Schema and an AST node describing a type, return a GraphQLType |
|
21
|
|
|
* definition which applies to that type. For example, if provided the parsed |
|
22
|
|
|
* AST node for `[User]`, a GraphQLList instance will be returned, containing |
|
23
|
|
|
* the type called "User" found in the schema. If a type called "User" is not |
|
24
|
|
|
* found in the schema, then undefined will be returned. |
|
25
|
|
|
* |
|
26
|
|
|
* @param SchemaInterface $schema |
|
27
|
|
|
* @param TypeNodeInterface $typeNode |
|
28
|
|
|
* @return TypeInterface|null |
|
29
|
|
|
* @throws InvariantException |
|
30
|
|
|
* @throws ConversionException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function convert(SchemaInterface $schema, TypeNodeInterface $typeNode): ?TypeInterface |
|
33
|
|
|
{ |
|
34
|
|
|
$innerType = null; |
|
35
|
|
|
|
|
36
|
|
|
if ($typeNode instanceof ListTypeNode) { |
|
37
|
|
|
$innerType = $this->convert($schema, $typeNode->getType()); |
|
38
|
|
|
return null !== $innerType ? newList($innerType) : null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($typeNode instanceof NonNullTypeNode) { |
|
42
|
|
|
$innerType = $this->convert($schema, $typeNode->getType()); |
|
43
|
|
|
return null !== $innerType ? newNonNull($innerType) : null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($typeNode instanceof NamedTypeNode) { |
|
47
|
|
|
return $schema->getType($typeNode->getNameValue()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
throw new ConversionException(sprintf('Unexpected type kind: %s', $typeNode->getKind())); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|