Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
- PHP version required: 7.4+
- Propagate error message and stack trace for why leaf value serialization failed
- Do not throw client safe `Error` when failing to serialize an Enum type
- Use native PHP types for properties of `Type` and its subclasses

### Added

Expand Down
2 changes: 1 addition & 1 deletion docs/type-definitions/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class EmailType extends ScalarType
{
// Note: name can be omitted. In this case it will be inferred from class name
// (suffix "Type" will be dropped)
public $name = 'Email';
public string $name = 'Email';

public function serialize($value)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Definition/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

class BooleanType extends ScalarType
{
/** @var string */
public $name = Type::BOOLEAN;
public string $name = Type::BOOLEAN;

/** @var string */
public $description = 'The `Boolean` scalar type represents `true` or `false`.';
public ?string $description = 'The `Boolean` scalar type represents `true` or `false`.';

/**
* Serialize the given value to a boolean.
Expand Down
15 changes: 7 additions & 8 deletions src/Type/Definition/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Utils\MixedStore;
use GraphQL\Utils\Utils;

Expand All @@ -23,29 +24,27 @@
class EnumType extends Type implements InputType, OutputType, LeafType, NullableType, NamedType
{
/** @var EnumTypeDefinitionNode|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/**
* Lazily initialized.
*
* @var EnumValueDefinition[]
* @var array<int, EnumValueDefinition>
*/
private $values;
private array $values;

/**
* Lazily initialized.
*
* Actually a MixedStore<mixed, EnumValueDefinition>, PHPStan won't let us type it that way.
*
* @var MixedStore
*/
private $valueLookup;
private MixedStore $valueLookup;

/** @var ArrayObject<string, EnumValueDefinition> */
private $nameLookup;
private ArrayObject $nameLookup;

/** @var array<int, EnumTypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

public function __construct($config)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Definition/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

class FloatType extends ScalarType
{
/** @var string */
public $name = Type::FLOAT;
public string $name = Type::FLOAT;

/** @var string */
public $description =
public ?string $description =
'The `Float` scalar type represents signed double-precision fractional
values as specified by
[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ';
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Definition/IDType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

class IDType extends ScalarType
{
/** @var string */
public $name = 'ID';
public string $name = 'ID';

/** @var string */
public $description =
public ?string $description =
'The `ID` scalar type represents a unique identifier, often used to
refetch an object or as key for a cache. The ID type appears in a JSON
response as a String; however, it is not intended to be human-readable.
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Definition/InputObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Utils\Utils;

use function count;
Expand All @@ -18,17 +19,17 @@
class InputObjectType extends Type implements InputType, NullableType, NamedType
{
/** @var InputObjectTypeDefinitionNode|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/**
* Lazily initialized.
*
* @var InputObjectField[]
* @var array<string, InputObjectField>
*/
private $fields;
private array $fields;

/** @var array<int, InputObjectTypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

/**
* @param mixed[] $config
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Definition/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ class IntType extends ScalarType
private const MAX_INT = 2147483647;
private const MIN_INT = -2147483648;

/** @var string */
public $name = Type::INT;
public string $name = Type::INT;

/** @var string */
public $description =
public ?string $description =
'The `Int` scalar type represents non-fractional signed whole numeric
values. Int can represent values between -(2^31) and 2^31 - 1. ';

Expand Down
9 changes: 5 additions & 4 deletions src/Type/Definition/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;

Expand All @@ -19,24 +20,24 @@
class InterfaceType extends TypeWithFields implements AbstractType, OutputType, CompositeType, NullableType, NamedType, ImplementingType
{
/** @var InterfaceTypeDefinitionNode|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/** @var array<int, InterfaceTypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

/**
* Lazily initialized.
*
* @var array<int, InterfaceType>
*/
private $interfaces;
private array $interfaces;

/**
* Lazily initialized.
*
* @var array<string, InterfaceType>
*/
private $interfaceMap;
private array $interfaceMap;

/**
* @param mixed[] $config
Expand Down
11 changes: 6 additions & 5 deletions src/Type/Definition/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;

Expand Down Expand Up @@ -59,27 +60,27 @@
class ObjectType extends TypeWithFields implements OutputType, CompositeType, NullableType, NamedType, ImplementingType
{
/** @var ObjectTypeDefinitionNode|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/** @var array<int, ObjectTypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

/** @var ?callable */
/** @var callable|null */
public $resolveFieldFn;

/**
* Lazily initialized.
*
* @var array<int, InterfaceType>
*/
private $interfaces;
private array $interfaces;

/**
* Lazily initialized.
*
* @var array<string, InterfaceType>
*/
private $interfaceMap;
private array $interfaceMap;

/**
* @param mixed[] $config
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Definition/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeExtensionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Utils\Utils;

use function is_string;
Expand All @@ -31,18 +32,18 @@
abstract class ScalarType extends Type implements OutputType, InputType, LeafType, NullableType, NamedType
{
/** @var ScalarTypeDefinitionNode|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/** @var ScalarTypeExtensionNode[] */
public $extensionASTNodes;
/** @var array<ScalarTypeExtensionNode> */
public array $extensionASTNodes;

/**
* @param mixed[] $config
*/
public function __construct(array $config = [])
{
$this->name = $config['name'] ?? $this->tryInferName();
$this->description = $config['description'] ?? $this->description;
$this->description = $config['description'] ?? $this->description ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->extensionASTNodes = $config['extensionASTNodes'] ?? [];
$this->config = $config;
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Definition/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

class StringType extends ScalarType
{
/** @var string */
public $name = Type::STRING;
public string $name = Type::STRING;

/** @var string */
public $description =
public ?string $description =
'The `String` scalar type represents textual data, represented as UTF-8
character sequences. The String type is most often used by GraphQL to
represent free-form human-readable text.';
Expand Down
21 changes: 11 additions & 10 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ abstract class Type implements JsonSerializable
public const ID = 'ID';

/** @var array<string, ScalarType> */
protected static $standardTypes;
protected static array $standardTypes;

/** @var array<string, Type> */
private static $builtInTypes;
private static array $builtInTypes;

/** @var string */
public $name;
/**
* Not set in wrapping types.
*/
public string $name;

/** @var string|null */
public $description;
public ?string $description;

/** @var (Node&TypeDefinitionNode)|null */
public $astNode;
public ?TypeDefinitionNode $astNode;

/** @var array<string, mixed> */
public $config;
public array $config;

/** @var array<Node&TypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

/**
* @api
Expand Down Expand Up @@ -153,7 +154,7 @@ public static function isBuiltInType(Type $type): bool
*/
public static function getAllBuiltInTypes(): array
{
if (self::$builtInTypes === null) {
if (! isset(self::$builtInTypes)) {
self::$builtInTypes = array_merge(
Introspection::getTypes(),
self::getStandardTypes()
Expand Down
13 changes: 7 additions & 6 deletions src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace GraphQL\Type\Definition;

use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Type\Schema;
Expand All @@ -17,25 +18,25 @@

class UnionType extends Type implements AbstractType, OutputType, CompositeType, NullableType, NamedType
{
/** @var UnionTypeDefinitionNode */
public $astNode;
/** @var UnionTypeDefinitionNode|null */
public ?TypeDefinitionNode $astNode;

/**
* Lazily initialized.
*
* @var ObjectType[]
* @var array<ObjectType>
*/
private $types;
private array $types;

/**
* Lazily initialized.
*
* @var array<string, bool>
*/
private $possibleTypeNames;
private array $possibleTypeNames;

/** @var array<int, UnionTypeExtensionNode> */
public $extensionASTNodes;
public array $extensionASTNodes;

/**
* @param mixed[] $config
Expand Down
8 changes: 2 additions & 6 deletions src/Type/Introspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,11 @@ public static function _type()
],
'name' => [
'type' => Type::string(),
'resolve' => static function ($obj) {
return $obj->name;
},
'resolve' => static fn (Type $obj): ?string => $obj->name ?? null,
],
'description' => [
'type' => Type::string(),
'resolve' => static function ($obj) {
return $obj->description;
},
'resolve' => static fn (Type $obj): ?string => $obj->description,
],
'fields' => [
'type' => Type::listOf(Type::nonNull(self::_field())),
Expand Down
Loading