Skip to content

Commit

Permalink
Fix public types (webonyx#702)
Browse files Browse the repository at this point in the history
* make type private

* use getters

* add deprecation notices

* add tests

* stanning

* use static

* wakey wakey GitHub Actions

* add same for InputObjectField
  • Loading branch information
shmax authored Aug 3, 2020
1 parent d6fe861 commit 5042bed
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 4 deletions.
48 changes: 47 additions & 1 deletion src/Type/Definition/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
Expand Down Expand Up @@ -60,7 +61,7 @@ class FieldDefinition
public $config;

/** @var OutputType&Type */
public $type;
private $type;

/** @var callable|string */
private $complexityFn;
Expand Down Expand Up @@ -194,6 +195,51 @@ public function getType() : Type
return $this->type;
}

public function __isset(string $name) : bool
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public getter for 'type' on FieldDefinition has been deprecated and will be removed" .
" in the next major version. Please update your code to use the 'getType' method.",
Warning::WARNING_CONFIG_DEPRECATION
);

return isset($this->type);
}

return false;
}

public function __get(string $name)
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public getter for 'type' on FieldDefinition has been deprecated and will be removed" .
" in the next major version. Please update your code to use the 'getType' method.",
Warning::WARNING_CONFIG_DEPRECATION
);

return $this->getType();
}

return null;
}

public function __set(string $name, $value)
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public setter for 'type' on FieldDefinition has been deprecated and will be removed" .
' in the next major version.',
Warning::WARNING_CONFIG_DEPRECATION
);
$this->type = $value;
}
}

/**
* @return bool
*/
Expand Down
48 changes: 47 additions & 1 deletion src/Type/Definition/InputObjectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
Expand All @@ -24,7 +25,7 @@ class InputObjectField
public $description;

/** @var Type&InputType */
public $type;
private $type;

/** @var InputValueDefinitionNode|null */
public $astNode;
Expand Down Expand Up @@ -54,6 +55,51 @@ public function __construct(array $opts)
$this->config = $opts;
}

public function __isset(string $name) : bool
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public getter for 'type' on InputObjectField has been deprecated and will be removed" .
" in the next major version. Please update your code to use the 'getType' method.",
Warning::WARNING_CONFIG_DEPRECATION
);

return isset($this->type);
}

return false;
}

public function __get(string $name)
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public getter for 'type' on InputObjectField has been deprecated and will be removed" .
" in the next major version. Please update your code to use the 'getType' method.",
Warning::WARNING_CONFIG_DEPRECATION
);

return $this->getType();
}

return null;
}

public function __set(string $name, $value)
{
switch ($name) {
case 'type':
Warning::warnOnce(
"The public setter for 'type' on InputObjectField has been deprecated and will be removed" .
' in the next major version.',
Warning::WARNING_CONFIG_DEPRECATION
);
$this->type = $value;
}
}

/**
* @return Type&InputType
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/SchemaExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected static function extendInputFieldMap(InputObjectType $type) : array
foreach ($oldFieldMap as $fieldName => $field) {
$newFieldMap[$fieldName] = [
'description' => $field->description,
'type' => static::extendType($field->type),
'type' => static::extendType($field->getType()),
'astNode' => $field->astNode,
];

Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static function ($enumValue) : string {
sprintf(
'Field %s of required type %s was not provided',
$fieldPath,
$field->type->toString()
$field->getType()->toString()
),
$blameNode
)
Expand Down
92 changes: 92 additions & 0 deletions tests/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace GraphQL\Tests\Type;

use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Tests\PHPUnit\ArraySubsetAsserts;
use GraphQL\Tests\Type\TestClasses\MyCustomType;
use GraphQL\Tests\Type\TestClasses\OtherCustom;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ListOfType;
Expand All @@ -18,6 +21,7 @@
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Schema;
use PHPUnit\Framework\Error\Warning as PhpUnitWarning;
use PHPUnit\Framework\TestCase;
use stdClass;
use function count;
Expand Down Expand Up @@ -205,6 +209,94 @@ public function testDefinesAQueryOnlySchema() : void
self::assertSame($this->blogArticle, $feedFieldType->getWrappedType());
}

public function testFieldDefinitionPublicTypeGetDeprecation() : void
{
$fieldDef = FieldDefinition::create([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public getter for \'type\' on FieldDefinition has been deprecated and will be removed in the next major version. Please update your code to use the \'getType\' method.');
});

// @phpstan-ignore-next-line type is private, but we're allowing its access temporarily via a magic method
$type = $fieldDef->type;
}

public function testFieldDefinitionPublicTypeSetDeprecation() : void
{
$fieldDef = FieldDefinition::create([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public setter for \'type\' on FieldDefinition has been deprecated and will be removed in the next major version.');
});

// @phpstan-ignore-next-line type is private, but we're allowing its access temporarily via a magic method
$fieldDef->type = Type::int();
}

public function testFieldDefinitionPublicTypeIssetDeprecation() : void
{
$fieldDef = FieldDefinition::create([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public getter for \'type\' on FieldDefinition has been deprecated and will be removed in the next major version. Please update your code to use the \'getType\' method.');
});

isset($fieldDef->type);
}

public function testInputObjectFieldPublicTypeGetDeprecation() : void
{
$fieldDef = new InputObjectField([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public getter for \'type\' on InputObjectField has been deprecated and will be removed in the next major version. Please update your code to use the \'getType\' method.');
});

// @phpstan-ignore-next-line type is private, but we're allowing its access temporarily via a magic method
$type = $fieldDef->type;
}

public function testInputObjectFieldPublicTypeSetDeprecation() : void
{
$fieldDef = new InputObjectField([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public setter for \'type\' on InputObjectField has been deprecated and will be removed in the next major version.');
});

// @phpstan-ignore-next-line type is private, but we're allowing its access temporarily via a magic method
$fieldDef->type = Type::int();
}

public function testInputObjectFieldPublicTypeIssetDeprecation() : void
{
$fieldDef = new InputObjectField([
'type' => Type::string(),
'name' => 'GenericField',
]);

Warning::setWarningHandler(static function ($message) : void {
self::assertEquals($message, 'The public getter for \'type\' on InputObjectField has been deprecated and will be removed in the next major version. Please update your code to use the \'getType\' method.');
});

isset($fieldDef->type);
}

/**
* @see it('defines a mutation schema')
*/
Expand Down

0 comments on commit 5042bed

Please sign in to comment.