Skip to content

Commit

Permalink
Removed callbacks in field types (previously deprecated in #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Aug 13, 2017
1 parent 6845b28 commit f9eb148
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 123 deletions.
3 changes: 2 additions & 1 deletion src/Error/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ final class Warning
const CONFIG_WARNING = 4;
const RESOLVE_TYPE_WARNING = 8;
const CONFIG_DEPRECATION_WARNING = 16;
const NOT_A_TYPE = 32;

const ALL = 23;
const ALL = 63;

static $enableWarnings = self::ALL;

Expand Down
12 changes: 2 additions & 10 deletions src/Type/Definition/FieldArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ class FieldArgument
*/
public $config;

/**
* @var InputType|callable
*/
private $type;

/**
* @var InputType
*/
private $resolvedType;
private $type;

/**
* @var bool
Expand Down Expand Up @@ -95,10 +90,7 @@ public function __construct(array $def)
*/
public function getType()
{
if (null === $this->resolvedType) {
$this->resolvedType = Type::resolve($this->type);
}
return $this->resolvedType;
return $this->type;
}

/**
Expand Down
13 changes: 2 additions & 11 deletions src/Type/Definition/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@ class FieldDefinition
*/
public $config;

/**
* @var OutputType|callable
*/
private $type;

/**
* @var OutputType
*/
private $resolvedType;
private $type;

private static $def;

Expand Down Expand Up @@ -216,11 +211,7 @@ public function getArg($name)
*/
public function getType()
{
if (null === $this->resolvedType) {
// TODO: deprecate types as callbacks - instead just allow field definitions to be callbacks
$this->resolvedType = Type::resolve($this->type);
}
return $this->resolvedType;
return $this->type;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Definition/InputObjectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(array $opts)
*/
public function getType()
{
return Type::resolve($this->type);
return $this->type;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Definition/ListOfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($type)
*/
public function toString()
{
$type = Type::resolve($this->ofType);
$type = $this->ofType;
$str = $type instanceof Type ? $type->toString() : (string) $type;
return '[' . $str . ']';
}
Expand All @@ -44,7 +44,7 @@ public function toString()
*/
public function getWrappedType($recurse = false)
{
$type = Type::resolve($this->ofType);
$type = $this->ofType;
return ($recurse && $type instanceof WrappingType) ? $type->getWrappedType($recurse) : $type;
}
}
2 changes: 1 addition & 1 deletion src/Type/Definition/NonNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct($type)
*/
public function getWrappedType($recurse = false)
{
$type = Type::resolve($this->ofType);
$type = $this->ofType;

Utils::invariant(
!($type instanceof NonNull),
Expand Down
22 changes: 4 additions & 18 deletions src/Type/Definition/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,7 @@ public function getInterfaces()
);
}

$this->interfaces = [];
foreach ($interfaces as $iface) {
$iface = Type::resolve($iface);
if (!$iface instanceof InterfaceType) {
throw new InvariantViolation(sprintf(
'%s may only implement Interface types, it cannot implement %s',
$this->name,
Utils::printSafe($iface)
));
}
// TODO: return interfaceMap vs interfaces. Possibly breaking change?
$this->interfaces[] = $iface;
$this->interfaceMap[$iface->name] = $iface;
}
$this->interfaces = $interfaces;
}
return $this->interfaces;
}
Expand All @@ -186,9 +173,8 @@ private function getInterfaceMap()
*/
public function implementsInterface($iface)
{
$iface = Type::resolve($iface);
$this->getInterfaces();
return isset($this->interfaceMap[$iface->name]);
$map = $this->getInterfaceMap();
return isset($map[$iface->name]);
}

/**
Expand Down Expand Up @@ -239,7 +225,7 @@ public function assertValid()
foreach ($this->getInterfaces() as $iface) {
Utils::invariant(
$iface instanceof InterfaceType,
"{$this->name} may only implement Interface types, it cannot implement: %s.",
"{$this->name} may only implement Interface types, it cannot implement %s.",
Utils::printSafe($iface)
);
Utils::invariant(
Expand Down
23 changes: 0 additions & 23 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,6 @@ public static function getNamedType($type)
while ($type instanceof WrappingType) {
$type = $type->getWrappedType();
}
return self::resolve($type);
}

/**
* @param $type
* @return mixed
*/
public static function resolve($type)
{
if (is_callable($type)) {
trigger_error(
'Passing type as closure is deprecated (see https://github.com/webonyx/graphql-php/issues/35 for alternatives)',
E_USER_DEPRECATED
);
$type = $type();
}

if (!$type instanceof Type) {
throw new InvariantViolation(sprintf(
'Expecting instance of ' . __CLASS__ . ', got "%s"',
Utils::getVariableType($type)
));
}
return $type;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ public function getTypes()
);
}

$this->types = [];
foreach ($types as $type) {
$this->types[] = Type::resolve($type);
}
$this->types = $types;
}
return $this->types;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/TypeInfo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GraphQL\Utils;

use GraphQL\Error\Warning;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
Expand Down Expand Up @@ -103,6 +104,14 @@ public static function extractTypes($type, array $typeMap = null)
if ($type instanceof WrappingType) {
return self::extractTypes($type->getWrappedType(true), $typeMap);
}
if (!$type instanceof Type) {
Warning::warnOnce(
'One of schema types is not a valid type definition instance. Ignoring it. '.
'Try running $schema->assertValid() to find out the cause of this warning.',
Warning::NOT_A_TYPE
);
return $typeMap;
}

if (!empty($typeMap[$type->name])) {
Utils::invariant(
Expand Down
84 changes: 32 additions & 52 deletions tests/Type/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public function setUp()
]);

$this->notInputTypes[] = $this->String;

Warning::suppress(Warning::NOT_A_TYPE);
}

public function tearDown()
{
parent::tearDown();
Warning::enable(Warning::NOT_A_TYPE);
}

public function testRejectsTypesWithoutNames()
Expand Down Expand Up @@ -572,11 +580,9 @@ public function testRejectsAnObjectTypeWithIncorrectlyTypedFields()
]
]));

// FIXME: this exception message is caused by old mechanism of type resolution (see #35),
// this has to be changed as soon as we drop this mechanism
$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "stdClass'
'SomeObject.field field type must be Output Type but got: instance of stdClass'
);
$schema->assertValid();
}
Expand Down Expand Up @@ -719,10 +725,9 @@ public function testRejectsAnObjectTypeWithIncorrectlyTypedFieldArgs()
]
]));

// FIXME
$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"'
'SomeObject.badField(0:) Must be named. Unexpected name: 0'
);

$schema->assertValid();
Expand Down Expand Up @@ -1023,7 +1028,7 @@ public function testRejectsAnInputObjectTypeWithIncorrectlyTypedFields()

$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"'
'SomeInputObject.0: Must be named. Unexpected name: 0'
);
$schema->assertValid();
}
Expand Down Expand Up @@ -1737,7 +1742,7 @@ public function testRejectsAnEmptyObjectFieldType()

$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"'
'BadObject.badField field type must be Output Type but got: null'
);

$schema->assertValid();
Expand All @@ -1755,12 +1760,10 @@ public function testRejectsANonOutputTypeAsAnObjectFieldType()
$schema->assertValid();
$this->fail('Expected exception not thrown for ' . Utils::printSafe($type));
} catch (InvariantViolation $e) {
// FIXME
if ($type !== 'TestString') {
$this->assertEquals('BadObject.badField field type must be Output Type but got: ' . $type, $e->getMessage());
} else {
$this->assertEquals('Expecting instance of GraphQL\Type\Definition\Type, got "string"', $e->getMessage());
}
$this->assertEquals(
'BadObject.badField field type must be Output Type but got: ' . Utils::printSafe($type),
$e->getMessage()
);
}
}
}
Expand Down Expand Up @@ -1844,7 +1847,7 @@ public function testRejectsAnObjectImplementingANonInterfaceType()
$this->fail('Exepected exception not thrown for type ' . $type);
} catch (InvariantViolation $e) {
$this->assertEquals(
'BadObject may only implement Interface types, it cannot implement ' . $type,
'BadObject may only implement Interface types, it cannot implement ' . $type . '.',
$e->getMessage()
);
}
Expand Down Expand Up @@ -1914,7 +1917,7 @@ public function testRejectsAnEmptyInterfaceFieldType()

$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"' // FIXME
'BadInterface.badField field type must be Output Type but got: null'
);

$schema->assertValid();
Expand All @@ -1932,18 +1935,10 @@ public function testRejectsANonOutputTypeAsAnInterfaceFieldType()
$schema->assertValid();
$this->fail('Expected exception not thrown for type ' . $type);
} catch (InvariantViolation $e) {
if ($type !== 'TestString') {
$this->assertEquals(
'BadInterface.badField field type must be Output Type but got: ' . $type,
$e->getMessage()
);
} else {
// FIXME
$this->assertEquals(
'Expecting instance of GraphQL\Type\Definition\Type, got "string"',
$e->getMessage()
);
}
$this->assertEquals(
'BadInterface.badField field type must be Output Type but got: ' . Utils::printSafe($type),
$e->getMessage()
);
}
}
}
Expand Down Expand Up @@ -1974,7 +1969,7 @@ public function testRejectsAnEmptyFieldArgType()
$this->fail('Expected exception not thrown');
} catch (InvariantViolation $e) {
$this->assertEquals(
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"',
'BadObject.badField(badArg): argument type must be Input Type but got: null',
$e->getMessage()
);
}
Expand All @@ -1991,17 +1986,10 @@ public function testRejectsANonInputTypeAsAFieldArgType()
$schema->assertValid();
$this->fail('Expected exception not thrown for type ' . $type);
} catch (InvariantViolation $e) {
if ($type !== 'TestString') {
$this->assertEquals(
'BadObject.badField(badArg): argument type must be Input Type but got: ' . $type,
$e->getMessage()
);
} else {
$this->assertEquals(
'Expecting instance of GraphQL\Type\Definition\Type, got "string"',
$e->getMessage()
);
}
$this->assertEquals(
'BadObject.badField(badArg): argument type must be Input Type but got: ' . Utils::printSafe($type),
$e->getMessage()
);
}
}
}
Expand All @@ -2027,10 +2015,9 @@ public function testRejectsAnEmptyInputFieldType()
{
$schema = $this->schemaWithInputFieldOfType(null);

// FIXME
$this->setExpectedException(
InvariantViolation::class,
'Expecting instance of GraphQL\Type\Definition\Type, got "NULL"'
'BadInputObject.badField field type must be Input Type but got: null.'
);
$schema->assertValid();
}
Expand All @@ -2046,17 +2033,10 @@ public function testRejectsANonInputTypeAsAnInputFieldType()
$schema->assertValid();
$this->fail('Expected exception not thrown for type ' . $type);
} catch (InvariantViolation $e) {
if ($type !== 'TestString') {
$this->assertEquals(
"BadInputObject.badField field type must be Input Type but got: $type.",
$e->getMessage()
);
} else {
$this->assertEquals(
'Expecting instance of GraphQL\Type\Definition\Type, got "string"',
$e->getMessage()
);
}
$this->assertEquals(
"BadInputObject.badField field type must be Input Type but got: " . Utils::printSafe($type) . ".",
$e->getMessage()
);
}
}
}
Expand Down

0 comments on commit f9eb148

Please sign in to comment.