-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNullScalar.php
45 lines (34 loc) · 1.04 KB
/
NullScalar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php declare(strict_types=1);
namespace MLL\GraphQLScalars;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Type\Definition\ScalarType;
class NullScalar extends ScalarType
{
public const ONLY_NULL_IS_ALLOWED = 'Only null is allowed.';
public string $name = 'Null';
public ?string $description /** @lang Markdown */
= 'Always `null`. Strictly validates value is non-null, no coercion.';
public function serialize($value)
{
if ($value !== null) {
throw new InvariantViolation(self::ONLY_NULL_IS_ALLOWED);
}
return null;
}
public function parseValue($value)
{
if ($value !== null) {
throw new Error(self::ONLY_NULL_IS_ALLOWED);
}
return null;
}
public function parseLiteral($valueNode, ?array $variables = null)
{
if (! $valueNode instanceof NullValueNode) {
throw new Error(self::ONLY_NULL_IS_ALLOWED);
}
return null;
}
}