Deprecated callbacks in "type" option of field/argument definitions (see #35)

This commit is contained in:
vladar 2016-10-23 00:49:25 +07:00
parent 9964c88f32
commit c11f25794a
9 changed files with 130 additions and 114 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace GraphQL\Type\Definition; namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Utils; use GraphQL\Utils;
/* /*
@ -191,14 +192,19 @@ abstract class Type
public static function resolve($type) public static function resolve($type)
{ {
if (is_callable($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(); $type = $type();
} }
Utils::invariant( if (!$type instanceof Type) {
$type instanceof Type, throw new InvariantViolation(sprintf(
'Expecting instance of ' . __CLASS__ . ' (or callable returning instance of that type), got "%s"', 'Expecting instance of ' . __CLASS__ . ', got "%s"',
Utils::getVariableType($type) Utils::getVariableType($type)
); ));
}
return $type; return $type;
} }

View File

@ -498,7 +498,7 @@ EOD;
} }
], ],
'possibleTypes' => [ 'possibleTypes' => [
'type' => Type::listOf(Type::nonNull([__CLASS__, '_type'])), 'type' => Type::listOf(Type::nonNull(self::_type())),
'resolve' => function ($type, $args, $context, ResolveInfo $info) { 'resolve' => function ($type, $args, $context, ResolveInfo $info) {
if ($type instanceof InterfaceType || $type instanceof UnionType) { if ($type instanceof InterfaceType || $type instanceof UnionType) {
return $info->schema->getPossibleTypes($type); return $info->schema->getPossibleTypes($type);

View File

@ -29,20 +29,20 @@ class ExecutorSchemaTest extends \PHPUnit_Framework_TestCase
$BlogAuthor = new ObjectType([ $BlogAuthor = new ObjectType([
'name' => 'Author', 'name' => 'Author',
'fields' => [ 'fields' => function() use (&$BlogArticle, &$BlogImage) {
'id' => ['type' => Type::string()], return [
'name' => ['type' => Type::string()], 'id' => ['type' => Type::string()],
'pic' => [ 'name' => ['type' => Type::string()],
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]], 'pic' => [
'type' => $BlogImage, 'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
'resolve' => function ($obj, $args) { 'type' => $BlogImage,
return $obj['pic']($args['width'], $args['height']); 'resolve' => function ($obj, $args) {
} return $obj['pic']($args['width'], $args['height']);
], }
'recentArticle' => ['type' => function () use (&$BlogArticle) { ],
return $BlogArticle; 'recentArticle' => $BlogArticle
}] ];
] }
]); ]);
$BlogArticle = new ObjectType([ $BlogArticle = new ObjectType([

View File

@ -116,23 +116,25 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
$deepDataType = null; $deepDataType = null;
$dataType = new ObjectType([ $dataType = new ObjectType([
'name' => 'DataType', 'name' => 'DataType',
'fields' => [ 'fields' => function() use (&$dataType, &$deepDataType) {
'a' => [ 'type' => Type::string() ], return [
'b' => [ 'type' => Type::string() ], 'a' => [ 'type' => Type::string() ],
'c' => [ 'type' => Type::string() ], 'b' => [ 'type' => Type::string() ],
'd' => [ 'type' => Type::string() ], 'c' => [ 'type' => Type::string() ],
'e' => [ 'type' => Type::string() ], 'd' => [ 'type' => Type::string() ],
'f' => [ 'type' => Type::string() ], 'e' => [ 'type' => Type::string() ],
'pic' => [ 'f' => [ 'type' => Type::string() ],
'args' => [ 'size' => ['type' => Type::int() ] ], 'pic' => [
'type' => Type::string(), 'args' => [ 'size' => ['type' => Type::int() ] ],
'resolve' => function($obj, $args) { 'type' => Type::string(),
return $obj['pic']($args['size']); 'resolve' => function($obj, $args) {
} return $obj['pic']($args['size']);
], }
'promise' => ['type' => function() use (&$dataType) {return $dataType;}], ],
'deep' => [ 'type' => function() use(&$deepDataType) {return $deepDataType; }], 'promise' => ['type' => $dataType],
] 'deep' => ['type' => $deepDataType],
];
}
]); ]);
$deepDataType = new ObjectType([ $deepDataType = new ObjectType([
@ -170,25 +172,25 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
$Type = new ObjectType([ $Type = new ObjectType([
'name' => 'Type', 'name' => 'Type',
'fields' => [ 'fields' => function() use (&$Type) {
'a' => ['type' => Type::string(), 'resolve' => function () { return [
return 'Apple'; 'a' => ['type' => Type::string(), 'resolve' => function () {
}], return 'Apple';
'b' => ['type' => Type::string(), 'resolve' => function () { }],
return 'Banana'; 'b' => ['type' => Type::string(), 'resolve' => function () {
}], return 'Banana';
'c' => ['type' => Type::string(), 'resolve' => function () { }],
return 'Cherry'; 'c' => ['type' => Type::string(), 'resolve' => function () {
}], return 'Cherry';
'deep' => [ }],
'type' => function () use (&$Type) { 'deep' => [
return $Type; 'type' => $Type,
}, 'resolve' => function () {
'resolve' => function () { return [];
return []; }
} ]
] ];
] }
]); ]);
$schema = new Schema(['query' => $Type]); $schema = new Schema(['query' => $Type]);
$expected = [ $expected = [

View File

@ -58,16 +58,14 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
$dataType = new ObjectType([ $dataType = new ObjectType([
'name' => 'DataType', 'name' => 'DataType',
'fields' => [ 'fields' => function() use (&$dataType) {
'sync' => ['type' => Type::string()], return [
'nonNullSync' => ['type' => Type::nonNull(Type::string())], 'sync' => ['type' => Type::string()],
'nest' => ['type' => function () use (&$dataType) { 'nonNullSync' => ['type' => Type::nonNull(Type::string())],
return $dataType; 'nest' => $dataType,
}], 'nonNullNest' => Type::nonNull($dataType)
'nonNullNest' => ['type' => function () use (&$dataType) { ];
return Type::nonNull($dataType); }
}]
]
]); ]);
$this->schema = new Schema(['query' => $dataType]); $this->schema = new Schema(['query' => $dataType]);

View File

@ -92,15 +92,17 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
$this->blogAuthor = new ObjectType([ $this->blogAuthor = new ObjectType([
'name' => 'Author', 'name' => 'Author',
'fields' => [ 'fields' => function() {
'id' => ['type' => Type::string()], return [
'name' => ['type' => Type::string()], 'id' => ['type' => Type::string()],
'pic' => [ 'type' => $this->blogImage, 'args' => [ 'name' => ['type' => Type::string()],
'width' => ['type' => Type::int()], 'pic' => [ 'type' => $this->blogImage, 'args' => [
'height' => ['type' => Type::int()] 'width' => ['type' => Type::int()],
]], 'height' => ['type' => Type::int()]
'recentArticle' => ['type' => function() {return $this->blogArticle;}], ]],
], 'recentArticle' => $this->blogArticle,
];
},
]); ]);
$this->blogArticle = new ObjectType([ $this->blogArticle = new ObjectType([

View File

@ -398,9 +398,11 @@ class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
foreach ($kinds as $kind) { foreach ($kinds as $kind) {
$someOutputType = new $kind([ $someOutputType = new $kind([
'name' => 'SomeOutputType', 'name' => 'SomeOutputType',
'fields' => [ 'fields' => function() {
'sneaky' => ['type' => function() {return $this->someInputObjectType;}] return [
] 'sneaky' => $this->someInputObjectType
];
}
]); ]);
$schema = new Schema(['query' => $someOutputType]); $schema = new Schema(['query' => $someOutputType]);
@ -545,9 +547,11 @@ class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
{ {
$someIncorrectInputType = new InputObjectType([ $someIncorrectInputType = new InputObjectType([
'name' => 'SomeIncorrectInputType', 'name' => 'SomeIncorrectInputType',
'fields' => [ 'fields' => function() use ($argType) {
'val' => ['type' => function() use ($argType) {return $argType;} ] return [
] 'val' => ['type' => $argType ]
];
}
]); ]);
$queryType = new ObjectType([ $queryType = new ObjectType([

View File

@ -59,24 +59,24 @@ class QuerySecuritySchema
self::$humanType = new ObjectType( self::$humanType = new ObjectType(
[ [
'name' => 'Human', 'name' => 'Human',
'fields' => [ 'fields' => function() {
'firstName' => ['type' => Type::nonNull(Type::string())], return [
'dogs' => [ 'firstName' => ['type' => Type::nonNull(Type::string())],
'type' => function () { 'dogs' => [
return Type::nonNull( 'type' => Type::nonNull(
Type::listOf( Type::listOf(
Type::nonNull(self::buildDogType()) Type::nonNull(self::buildDogType())
) )
); ),
}, 'complexity' => function ($childrenComplexity, $args) {
'complexity' => function ($childrenComplexity, $args) { $complexity = isset($args['name']) ? 1 : 10;
$complexity = isset($args['name']) ? 1 : 10;
return $childrenComplexity + $complexity; return $childrenComplexity + $complexity;
}, },
'args' => ['name' => ['type' => Type::string()]], 'args' => ['name' => ['type' => Type::string()]],
], ],
], ];
},
] ]
); );

View File

@ -94,16 +94,18 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
$Cat = new ObjectType([ $Cat = new ObjectType([
'name' => 'Cat', 'name' => 'Cat',
'isTypeOf' => function() {return true;}, 'isTypeOf' => function() {return true;},
'fields' => [ 'fields' => function() use (&$FurColor) {
'name' => [ return [
'type' => Type::string(), 'name' => [
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ] 'type' => Type::string(),
], 'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
'nickname' => ['type' => Type::string()], ],
'meows' => ['type' => Type::boolean()], 'nickname' => ['type' => Type::string()],
'meowVolume' => ['type' => Type::int()], 'meows' => ['type' => Type::boolean()],
'furColor' => ['type' => function() use (&$FurColor) {return $FurColor;}] 'meowVolume' => ['type' => Type::int()],
], 'furColor' => $FurColor
];
},
'interfaces' => [$Being, $Pet] 'interfaces' => [$Being, $Pet]
]); ]);
@ -128,15 +130,17 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
'name' => 'Human', 'name' => 'Human',
'isTypeOf' => function() {return true;}, 'isTypeOf' => function() {return true;},
'interfaces' => [$Being, $Intelligent], 'interfaces' => [$Being, $Intelligent],
'fields' => [ 'fields' => function() use (&$Human, $Pet) {
'name' => [ return [
'type' => Type::string(), 'name' => [
'args' => ['surname' => ['type' => Type::boolean()]] 'type' => Type::string(),
], 'args' => ['surname' => ['type' => Type::boolean()]]
'pets' => ['type' => Type::listOf($Pet)], ],
'relatives' => ['type' => function() use (&$Human) {return Type::listOf($Human); }], 'pets' => ['type' => Type::listOf($Pet)],
'iq' => ['type' => Type::int()] 'relatives' => ['type' => Type::listOf($Human)],
] 'iq' => ['type' => Type::int()]
];
}
]); ]);
$Alien = new ObjectType([ $Alien = new ObjectType([