mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
Deprecated callbacks in "type" option of field/argument definitions (see #35)
This commit is contained in:
parent
9964c88f32
commit
c11f25794a
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace GraphQL\Type\Definition;
|
||||
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Utils;
|
||||
|
||||
/*
|
||||
@ -191,14 +192,19 @@ abstract class Type
|
||||
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();
|
||||
}
|
||||
|
||||
Utils::invariant(
|
||||
$type instanceof Type,
|
||||
'Expecting instance of ' . __CLASS__ . ' (or callable returning instance of that type), got "%s"',
|
||||
if (!$type instanceof Type) {
|
||||
throw new InvariantViolation(sprintf(
|
||||
'Expecting instance of ' . __CLASS__ . ', got "%s"',
|
||||
Utils::getVariableType($type)
|
||||
);
|
||||
));
|
||||
}
|
||||
return $type;
|
||||
}
|
||||
|
||||
|
@ -498,7 +498,7 @@ EOD;
|
||||
}
|
||||
],
|
||||
'possibleTypes' => [
|
||||
'type' => Type::listOf(Type::nonNull([__CLASS__, '_type'])),
|
||||
'type' => Type::listOf(Type::nonNull(self::_type())),
|
||||
'resolve' => function ($type, $args, $context, ResolveInfo $info) {
|
||||
if ($type instanceof InterfaceType || $type instanceof UnionType) {
|
||||
return $info->schema->getPossibleTypes($type);
|
||||
|
@ -29,7 +29,8 @@ class ExecutorSchemaTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$BlogAuthor = new ObjectType([
|
||||
'name' => 'Author',
|
||||
'fields' => [
|
||||
'fields' => function() use (&$BlogArticle, &$BlogImage) {
|
||||
return [
|
||||
'id' => ['type' => Type::string()],
|
||||
'name' => ['type' => Type::string()],
|
||||
'pic' => [
|
||||
@ -39,10 +40,9 @@ class ExecutorSchemaTest extends \PHPUnit_Framework_TestCase
|
||||
return $obj['pic']($args['width'], $args['height']);
|
||||
}
|
||||
],
|
||||
'recentArticle' => ['type' => function () use (&$BlogArticle) {
|
||||
return $BlogArticle;
|
||||
}]
|
||||
]
|
||||
'recentArticle' => $BlogArticle
|
||||
];
|
||||
}
|
||||
]);
|
||||
|
||||
$BlogArticle = new ObjectType([
|
||||
|
@ -116,7 +116,8 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
|
||||
$deepDataType = null;
|
||||
$dataType = new ObjectType([
|
||||
'name' => 'DataType',
|
||||
'fields' => [
|
||||
'fields' => function() use (&$dataType, &$deepDataType) {
|
||||
return [
|
||||
'a' => [ 'type' => Type::string() ],
|
||||
'b' => [ 'type' => Type::string() ],
|
||||
'c' => [ 'type' => Type::string() ],
|
||||
@ -130,9 +131,10 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
|
||||
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([
|
||||
@ -170,7 +172,8 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$Type = new ObjectType([
|
||||
'name' => 'Type',
|
||||
'fields' => [
|
||||
'fields' => function() use (&$Type) {
|
||||
return [
|
||||
'a' => ['type' => Type::string(), 'resolve' => function () {
|
||||
return 'Apple';
|
||||
}],
|
||||
@ -181,14 +184,13 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
|
||||
return 'Cherry';
|
||||
}],
|
||||
'deep' => [
|
||||
'type' => function () use (&$Type) {
|
||||
return $Type;
|
||||
},
|
||||
'type' => $Type,
|
||||
'resolve' => function () {
|
||||
return [];
|
||||
}
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
]);
|
||||
$schema = new Schema(['query' => $Type]);
|
||||
$expected = [
|
||||
|
@ -58,16 +58,14 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$dataType = new ObjectType([
|
||||
'name' => 'DataType',
|
||||
'fields' => [
|
||||
'fields' => function() use (&$dataType) {
|
||||
return [
|
||||
'sync' => ['type' => Type::string()],
|
||||
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
|
||||
'nest' => ['type' => function () use (&$dataType) {
|
||||
return $dataType;
|
||||
}],
|
||||
'nonNullNest' => ['type' => function () use (&$dataType) {
|
||||
return Type::nonNull($dataType);
|
||||
}]
|
||||
]
|
||||
'nest' => $dataType,
|
||||
'nonNullNest' => Type::nonNull($dataType)
|
||||
];
|
||||
}
|
||||
]);
|
||||
|
||||
$this->schema = new Schema(['query' => $dataType]);
|
||||
|
@ -92,15 +92,17 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->blogAuthor = new ObjectType([
|
||||
'name' => 'Author',
|
||||
'fields' => [
|
||||
'fields' => function() {
|
||||
return [
|
||||
'id' => ['type' => Type::string()],
|
||||
'name' => ['type' => Type::string()],
|
||||
'pic' => [ 'type' => $this->blogImage, 'args' => [
|
||||
'width' => ['type' => Type::int()],
|
||||
'height' => ['type' => Type::int()]
|
||||
]],
|
||||
'recentArticle' => ['type' => function() {return $this->blogArticle;}],
|
||||
],
|
||||
'recentArticle' => $this->blogArticle,
|
||||
];
|
||||
},
|
||||
]);
|
||||
|
||||
$this->blogArticle = new ObjectType([
|
||||
|
@ -398,9 +398,11 @@ class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
foreach ($kinds as $kind) {
|
||||
$someOutputType = new $kind([
|
||||
'name' => 'SomeOutputType',
|
||||
'fields' => [
|
||||
'sneaky' => ['type' => function() {return $this->someInputObjectType;}]
|
||||
]
|
||||
'fields' => function() {
|
||||
return [
|
||||
'sneaky' => $this->someInputObjectType
|
||||
];
|
||||
}
|
||||
]);
|
||||
|
||||
$schema = new Schema(['query' => $someOutputType]);
|
||||
@ -545,9 +547,11 @@ class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$someIncorrectInputType = new InputObjectType([
|
||||
'name' => 'SomeIncorrectInputType',
|
||||
'fields' => [
|
||||
'val' => ['type' => function() use ($argType) {return $argType;} ]
|
||||
]
|
||||
'fields' => function() use ($argType) {
|
||||
return [
|
||||
'val' => ['type' => $argType ]
|
||||
];
|
||||
}
|
||||
]);
|
||||
|
||||
$queryType = new ObjectType([
|
||||
|
@ -59,16 +59,15 @@ class QuerySecuritySchema
|
||||
self::$humanType = new ObjectType(
|
||||
[
|
||||
'name' => 'Human',
|
||||
'fields' => [
|
||||
'fields' => function() {
|
||||
return [
|
||||
'firstName' => ['type' => Type::nonNull(Type::string())],
|
||||
'dogs' => [
|
||||
'type' => function () {
|
||||
return Type::nonNull(
|
||||
'type' => Type::nonNull(
|
||||
Type::listOf(
|
||||
Type::nonNull(self::buildDogType())
|
||||
)
|
||||
);
|
||||
},
|
||||
),
|
||||
'complexity' => function ($childrenComplexity, $args) {
|
||||
$complexity = isset($args['name']) ? 1 : 10;
|
||||
|
||||
@ -76,7 +75,8 @@ class QuerySecuritySchema
|
||||
},
|
||||
'args' => ['name' => ['type' => Type::string()]],
|
||||
],
|
||||
],
|
||||
];
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -94,7 +94,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
$Cat = new ObjectType([
|
||||
'name' => 'Cat',
|
||||
'isTypeOf' => function() {return true;},
|
||||
'fields' => [
|
||||
'fields' => function() use (&$FurColor) {
|
||||
return [
|
||||
'name' => [
|
||||
'type' => Type::string(),
|
||||
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
|
||||
@ -102,8 +103,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
'nickname' => ['type' => Type::string()],
|
||||
'meows' => ['type' => Type::boolean()],
|
||||
'meowVolume' => ['type' => Type::int()],
|
||||
'furColor' => ['type' => function() use (&$FurColor) {return $FurColor;}]
|
||||
],
|
||||
'furColor' => $FurColor
|
||||
];
|
||||
},
|
||||
'interfaces' => [$Being, $Pet]
|
||||
]);
|
||||
|
||||
@ -128,15 +130,17 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
'name' => 'Human',
|
||||
'isTypeOf' => function() {return true;},
|
||||
'interfaces' => [$Being, $Intelligent],
|
||||
'fields' => [
|
||||
'fields' => function() use (&$Human, $Pet) {
|
||||
return [
|
||||
'name' => [
|
||||
'type' => Type::string(),
|
||||
'args' => ['surname' => ['type' => Type::boolean()]]
|
||||
],
|
||||
'pets' => ['type' => Type::listOf($Pet)],
|
||||
'relatives' => ['type' => function() use (&$Human) {return Type::listOf($Human); }],
|
||||
'relatives' => ['type' => Type::listOf($Human)],
|
||||
'iq' => ['type' => Type::int()]
|
||||
]
|
||||
];
|
||||
}
|
||||
]);
|
||||
|
||||
$Alien = new ObjectType([
|
||||
|
Loading…
Reference in New Issue
Block a user