-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
209 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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; | ||
use GraphQL\Utils\Utils; | ||
|
||
class NullScalar extends ScalarType | ||
{ | ||
public $name = 'Null'; | ||
|
||
public $description /** @lang Markdown */ | ||
= 'Always `null`. Strictly validates value is non-null, no coercion.'; | ||
|
||
public function serialize($value) | ||
{ | ||
if (null !== $value) { | ||
throw new InvariantViolation(static::notNullMessage($value)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function parseValue($value) | ||
{ | ||
if (null !== $value) { | ||
throw new Error(static::notNullMessage($value)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function parseLiteral($valueNode, ?array $variables = null) | ||
{ | ||
if (! $valueNode instanceof NullValueNode) { | ||
// Intentionally without message, as all information is already present in the wrapped error | ||
throw new Error(''); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed $value any non-null value | ||
*/ | ||
public static function notNullMessage($value): string | ||
{ | ||
$notNull = Utils::printSafe($value); | ||
|
||
return "Expected null, got: {$notNull}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
use GraphQL\Executor\ExecutionResult; | ||
use GraphQL\GraphQL; | ||
use GraphQL\Type\Definition\ObjectType; | ||
use GraphQL\Type\Schema; | ||
use GraphQL\Type\SchemaConfig; | ||
use MLL\GraphQLScalars\NullScalar; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NullScalarTest extends TestCase | ||
{ | ||
private Schema $schema; | ||
|
||
/** | ||
* @var mixed will be returned by field mixed | ||
*/ | ||
private $return; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$null = new NullScalar(); | ||
|
||
$schemaConfig = new SchemaConfig(); | ||
$schemaConfig->setQuery( | ||
new ObjectType([ | ||
'name' => 'Query', | ||
'fields' => [ | ||
'foo' => [ | ||
'type' => $null, | ||
'resolve' => function ($root, array $args) { | ||
return reset($args); | ||
}, | ||
'args' => [ | ||
'bar' => $null, | ||
], | ||
], | ||
'mixed' => [ | ||
'type' => $null, | ||
'resolve' => function () { | ||
return $this->return; | ||
}, | ||
], | ||
], | ||
]) | ||
); | ||
|
||
$this->schema = new Schema($schemaConfig); | ||
} | ||
|
||
public function testAllowsNullArguments(): void | ||
{ | ||
$graphqlResult = $this->executeQueryWithLiteral('null'); | ||
self::assertNull($graphqlResult->data['foo']); | ||
|
||
$jsonResult = $this->executeQueryWithJsonVariable('null'); | ||
self::assertNull($jsonResult->data['foo']); | ||
} | ||
|
||
public function testForbidsNonNullArguments(): void | ||
{ | ||
$graphqlResult = $this->executeQueryWithLiteral('1'); | ||
// @phpstan-ignore-next-line graphql-php is wrong | ||
self::assertNull($graphqlResult->data); | ||
self::assertSame('Field "foo" argument "bar" requires type Null, found 1.', $graphqlResult->errors[0]->getMessage()); | ||
|
||
$jsonResult = $this->executeQueryWithJsonVariable('1'); | ||
// @phpstan-ignore-next-line graphql-php is wrong | ||
self::assertNull($jsonResult->data); | ||
self::assertSame('Variable "$var" got invalid value 1; Expected type Null; Expected null, got: 1', $jsonResult->errors[0]->getMessage()); | ||
} | ||
|
||
public function testForbidsNonNullReturn(): void | ||
{ | ||
$this->return = 1; | ||
$graphqlResult = GraphQL::executeQuery($this->schema, /** @lang GraphQL */ '{ mixed }'); | ||
self::assertSame('Expected a value of type "Null" but received: 1', $graphqlResult->errors[0]->getMessage()); | ||
self::assertSame(['mixed' => null], $graphqlResult->data); | ||
} | ||
|
||
protected function executeQueryWithLiteral(string $literal): ExecutionResult | ||
{ | ||
$query = /** @lang GraphQL */ " | ||
{ | ||
foo(bar: {$literal}) | ||
} | ||
"; | ||
|
||
return GraphQL::executeQuery( | ||
$this->schema, | ||
$query | ||
); | ||
} | ||
|
||
protected function executeQueryWithJsonVariable(string $jsonLiteral): ExecutionResult | ||
{ | ||
$query = /** @lang GraphQL */ ' | ||
query Foo($var: Null) { | ||
foo(bar: $var) | ||
} | ||
'; | ||
|
||
/** @var array{var: mixed} $json */ | ||
$json = \Safe\json_decode(/** @lang JSON */ <<<JSON | ||
{ | ||
"var": $jsonLiteral | ||
} | ||
JSON, | ||
true | ||
); | ||
|
||
return GraphQL::executeQuery( | ||
$this->schema, | ||
$query, | ||
null, | ||
null, | ||
$json | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
|