Skip to content

Commit 1d46168

Browse files
authored
Add scalar Null (#15)
1 parent f955521 commit 1d46168

19 files changed

+209
-48
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## v5.1.0
11+
12+
### Added
13+
14+
- Add scalar `Null`
15+
1016
## v5.0.1
1117

1218
### Fixed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,25 @@ A [RFC 5321](https://tools.ietf.org/html/rfc5321) compliant email.
2424

2525
### [JSON](src/JSON.php)
2626

27-
Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org/.
27+
Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org.
2828

2929
### [Mixed](src/MixedScalar.php)
3030

3131
Loose type that allows any value. Be careful when passing in large `Int` or `Float` literals,
3232
as they may not be parsed correctly on the server side. Use `String` literals if you are
3333
dealing with really large numbers to be on the safe side.
3434

35+
### [Null](src/NullScalar.php)
36+
37+
Always `null`. Strictly validates value is non-null, no coercion.
38+
3539
### [Regex](src/Regex.php)
3640

3741
The `Regex` class allows you to define a custom scalar that validates that the given
3842
value matches a regular expression.
3943

4044
The quickest way to define a custom scalar is the `make` factory method. Just provide
41-
a name and a regular expression and you will receive a ready-to-use custom regex scalar.
45+
a name and a regular expression, you will receive a ready-to-use custom regex scalar.
4246

4347
```php
4448
use MLL\GraphQLScalars\Regex;
@@ -78,7 +82,7 @@ DESCRIPTION;
7882
### [StringScalar](src/StringScalar.php)
7983

8084
The `StringScalar` encapsulates all the boilerplate associated with creating a string-based Scalar type.
81-
It does the proper string checking for you and let's you focus on the minimal logic that is specific to your use case.
85+
It performs basic checks and coercion, you can focus on the minimal logic that is specific to your use case.
8286

8387
All you have to specify is a function that checks if the given string is valid.
8488
Use the factory method `make` to generate an instance on the fly.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"require": {
1717
"php": "^7.4 || ^8",
1818
"ext-json": "*",
19-
"egulias/email-validator": "^2 || ^3",
19+
"egulias/email-validator": "^2.1.17 || ^3",
2020
"spatie/regex": "^1.4",
2121
"thecodingmachine/safe": "^1.3",
22-
"webonyx/graphql-php": "^0.13 || ^14"
22+
"webonyx/graphql-php": "^0.13.9 || ^14"
2323
},
2424
"require-dev": {
2525
"ergebnis/composer-normalize": "^2.16",

src/Email.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

src/JSON.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

@@ -12,7 +10,7 @@
1210
class JSON extends ScalarType
1311
{
1412
public $description /** @lang Markdown */
15-
= 'Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org/.';
13+
= 'Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org.';
1614

1715
public function serialize($value): string
1816
{

src/MixedScalar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

src/NullScalar.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\GraphQLScalars;
4+
5+
use GraphQL\Error\Error;
6+
use GraphQL\Error\InvariantViolation;
7+
use GraphQL\Language\AST\NullValueNode;
8+
use GraphQL\Type\Definition\ScalarType;
9+
use GraphQL\Utils\Utils;
10+
11+
class NullScalar extends ScalarType
12+
{
13+
public $name = 'Null';
14+
15+
public $description /** @lang Markdown */
16+
= 'Always `null`. Strictly validates value is non-null, no coercion.';
17+
18+
public function serialize($value)
19+
{
20+
if (null !== $value) {
21+
throw new InvariantViolation(static::notNullMessage($value));
22+
}
23+
24+
return null;
25+
}
26+
27+
public function parseValue($value)
28+
{
29+
if (null !== $value) {
30+
throw new Error(static::notNullMessage($value));
31+
}
32+
33+
return null;
34+
}
35+
36+
public function parseLiteral($valueNode, ?array $variables = null)
37+
{
38+
if (! $valueNode instanceof NullValueNode) {
39+
// Intentionally without message, as all information is already present in the wrapped error
40+
throw new Error('');
41+
}
42+
43+
return null;
44+
}
45+
46+
/**
47+
* @param mixed $value any non-null value
48+
*/
49+
public static function notNullMessage($value): string
50+
{
51+
$notNull = Utils::printSafe($value);
52+
53+
return "Expected null, got: {$notNull}";
54+
}
55+
}

src/Regex.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

src/StringScalar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

src/Utils.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
namespace MLL\GraphQLScalars;
64

0 commit comments

Comments
 (0)