Skip to content

Commit

Permalink
Deprecated callbacks in "type" option of field/argument definitions (…
Browse files Browse the repository at this point in the history
…see #35)
  • Loading branch information
vladar committed Oct 22, 2016
1 parent 9964c88 commit c11f257
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 115 deletions.
16 changes: 11 additions & 5 deletions src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GraphQL\Type\Definition;

use GraphQL\Error\InvariantViolation;
use GraphQL\Utils;

/*
Expand Down Expand Up @@ -191,14 +192,19 @@ public static function getNamedType($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"',
Utils::getVariableType($type)
);
if (!$type instanceof Type) {
throw new InvariantViolation(sprintf(
'Expecting instance of ' . __CLASS__ . ', got "%s"',
Utils::getVariableType($type)
));
}
return $type;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Introspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public static function _type()
}
],
'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);
Expand Down
28 changes: 14 additions & 14 deletions tests/Executor/ExecutorSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public function testExecutesUsingASchema()

$BlogAuthor = new ObjectType([
'name' => 'Author',
'fields' => [
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
'type' => $BlogImage,
'resolve' => function ($obj, $args) {
return $obj['pic']($args['width'], $args['height']);
}
],
'recentArticle' => ['type' => function () use (&$BlogArticle) {
return $BlogArticle;
}]
]
'fields' => function() use (&$BlogArticle, &$BlogImage) {
return [
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
'type' => $BlogImage,
'resolve' => function ($obj, $args) {
return $obj['pic']($args['width'], $args['height']);
}
],
'recentArticle' => $BlogArticle
];
}
]);

$BlogArticle = new ObjectType([
Expand Down
74 changes: 38 additions & 36 deletions tests/Executor/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,25 @@ public function testExecutesArbitraryCode()
$deepDataType = null;
$dataType = new ObjectType([
'name' => 'DataType',
'fields' => [
'a' => [ 'type' => Type::string() ],
'b' => [ 'type' => Type::string() ],
'c' => [ 'type' => Type::string() ],
'd' => [ 'type' => Type::string() ],
'e' => [ 'type' => Type::string() ],
'f' => [ 'type' => Type::string() ],
'pic' => [
'args' => [ 'size' => ['type' => Type::int() ] ],
'type' => Type::string(),
'resolve' => function($obj, $args) {
return $obj['pic']($args['size']);
}
],
'promise' => ['type' => function() use (&$dataType) {return $dataType;}],
'deep' => [ 'type' => function() use(&$deepDataType) {return $deepDataType; }],
]
'fields' => function() use (&$dataType, &$deepDataType) {
return [
'a' => [ 'type' => Type::string() ],
'b' => [ 'type' => Type::string() ],
'c' => [ 'type' => Type::string() ],
'd' => [ 'type' => Type::string() ],
'e' => [ 'type' => Type::string() ],
'f' => [ 'type' => Type::string() ],
'pic' => [
'args' => [ 'size' => ['type' => Type::int() ] ],
'type' => Type::string(),
'resolve' => function($obj, $args) {
return $obj['pic']($args['size']);
}
],
'promise' => ['type' => $dataType],
'deep' => ['type' => $deepDataType],
];
}
]);

$deepDataType = new ObjectType([
Expand Down Expand Up @@ -170,25 +172,25 @@ public function testMergesParallelFragments()

$Type = new ObjectType([
'name' => 'Type',
'fields' => [
'a' => ['type' => Type::string(), 'resolve' => function () {
return 'Apple';
}],
'b' => ['type' => Type::string(), 'resolve' => function () {
return 'Banana';
}],
'c' => ['type' => Type::string(), 'resolve' => function () {
return 'Cherry';
}],
'deep' => [
'type' => function () use (&$Type) {
return $Type;
},
'resolve' => function () {
return [];
}
]
]
'fields' => function() use (&$Type) {
return [
'a' => ['type' => Type::string(), 'resolve' => function () {
return 'Apple';
}],
'b' => ['type' => Type::string(), 'resolve' => function () {
return 'Banana';
}],
'c' => ['type' => Type::string(), 'resolve' => function () {
return 'Cherry';
}],
'deep' => [
'type' => $Type,
'resolve' => function () {
return [];
}
]
];
}
]);
$schema = new Schema(['query' => $Type]);
$expected = [
Expand Down
18 changes: 8 additions & 10 deletions tests/Executor/NonNullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ public function setUp()

$dataType = new ObjectType([
'name' => 'DataType',
'fields' => [
'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);
}]
]
'fields' => function() use (&$dataType) {
return [
'sync' => ['type' => Type::string()],
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
'nest' => $dataType,
'nonNullNest' => Type::nonNull($dataType)
];
}
]);

$this->schema = new Schema(['query' => $dataType]);
Expand Down
20 changes: 11 additions & 9 deletions tests/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ public function setUp()

$this->blogAuthor = new ObjectType([
'name' => 'Author',
'fields' => [
'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;}],
],
'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' => $this->blogArticle,
];
},
]);

$this->blogArticle = new ObjectType([
Expand Down
16 changes: 10 additions & 6 deletions tests/Type/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,11 @@ public function testRejectsASchemaThatUsesAnInputTypeAsAField()
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]);
Expand Down Expand Up @@ -545,9 +547,11 @@ private function schemaWithFieldArgOfType($argType)
{
$someIncorrectInputType = new InputObjectType([
'name' => 'SomeIncorrectInputType',
'fields' => [
'val' => ['type' => function() use ($argType) {return $argType;} ]
]
'fields' => function() use ($argType) {
return [
'val' => ['type' => $argType ]
];
}
]);

$queryType = new ObjectType([
Expand Down
30 changes: 15 additions & 15 deletions tests/Validator/QuerySecuritySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,24 @@ public static function buildHumanType()
self::$humanType = new ObjectType(
[
'name' => 'Human',
'fields' => [
'firstName' => ['type' => Type::nonNull(Type::string())],
'dogs' => [
'type' => function () {
return Type::nonNull(
'fields' => function() {
return [
'firstName' => ['type' => Type::nonNull(Type::string())],
'dogs' => [
'type' => Type::nonNull(
Type::listOf(
Type::nonNull(self::buildDogType())
)
);
},
'complexity' => function ($childrenComplexity, $args) {
$complexity = isset($args['name']) ? 1 : 10;

return $childrenComplexity + $complexity;
},
'args' => ['name' => ['type' => Type::string()]],
],
],
),
'complexity' => function ($childrenComplexity, $args) {
$complexity = isset($args['name']) ? 1 : 10;

return $childrenComplexity + $complexity;
},
'args' => ['name' => ['type' => Type::string()]],
],
];
},
]
);

Expand Down
42 changes: 23 additions & 19 deletions tests/Validator/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ public static function getDefaultSchema()
$Cat = new ObjectType([
'name' => 'Cat',
'isTypeOf' => function() {return true;},
'fields' => [
'name' => [
'type' => Type::string(),
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
],
'nickname' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
'meowVolume' => ['type' => Type::int()],
'furColor' => ['type' => function() use (&$FurColor) {return $FurColor;}]
],
'fields' => function() use (&$FurColor) {
return [
'name' => [
'type' => Type::string(),
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
],
'nickname' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
'meowVolume' => ['type' => Type::int()],
'furColor' => $FurColor
];
},
'interfaces' => [$Being, $Pet]
]);

Expand All @@ -128,15 +130,17 @@ public static function getDefaultSchema()
'name' => 'Human',
'isTypeOf' => function() {return true;},
'interfaces' => [$Being, $Intelligent],
'fields' => [
'name' => [
'type' => Type::string(),
'args' => ['surname' => ['type' => Type::boolean()]]
],
'pets' => ['type' => Type::listOf($Pet)],
'relatives' => ['type' => function() use (&$Human) {return Type::listOf($Human); }],
'iq' => ['type' => Type::int()]
]
'fields' => function() use (&$Human, $Pet) {
return [
'name' => [
'type' => Type::string(),
'args' => ['surname' => ['type' => Type::boolean()]]
],
'pets' => ['type' => Type::listOf($Pet)],
'relatives' => ['type' => Type::listOf($Human)],
'iq' => ['type' => Type::int()]
];
}
]);

$Alien = new ObjectType([
Expand Down

0 comments on commit c11f257

Please sign in to comment.