Skip to content

Commit c11f257

Browse files
committed
Deprecated callbacks in "type" option of field/argument definitions (see #35)
1 parent 9964c88 commit c11f257

File tree

9 files changed

+131
-115
lines changed

9 files changed

+131
-115
lines changed

src/Type/Definition/Type.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\InvariantViolation;
45
use GraphQL\Utils;
56

67
/*
@@ -191,14 +192,19 @@ public static function getNamedType($type)
191192
public static function resolve($type)
192193
{
193194
if (is_callable($type)) {
195+
trigger_error(
196+
'Passing type as closure is deprecated (see https://github.com/webonyx/graphql-php/issues/35 for alternatives)',
197+
E_USER_DEPRECATED
198+
);
194199
$type = $type();
195200
}
196201

197-
Utils::invariant(
198-
$type instanceof Type,
199-
'Expecting instance of ' . __CLASS__ . ' (or callable returning instance of that type), got "%s"',
200-
Utils::getVariableType($type)
201-
);
202+
if (!$type instanceof Type) {
203+
throw new InvariantViolation(sprintf(
204+
'Expecting instance of ' . __CLASS__ . ', got "%s"',
205+
Utils::getVariableType($type)
206+
));
207+
}
202208
return $type;
203209
}
204210

src/Type/Introspection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public static function _type()
498498
}
499499
],
500500
'possibleTypes' => [
501-
'type' => Type::listOf(Type::nonNull([__CLASS__, '_type'])),
501+
'type' => Type::listOf(Type::nonNull(self::_type())),
502502
'resolve' => function ($type, $args, $context, ResolveInfo $info) {
503503
if ($type instanceof InterfaceType || $type instanceof UnionType) {
504504
return $info->schema->getPossibleTypes($type);

tests/Executor/ExecutorSchemaTest.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public function testExecutesUsingASchema()
2929

3030
$BlogAuthor = new ObjectType([
3131
'name' => 'Author',
32-
'fields' => [
33-
'id' => ['type' => Type::string()],
34-
'name' => ['type' => Type::string()],
35-
'pic' => [
36-
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
37-
'type' => $BlogImage,
38-
'resolve' => function ($obj, $args) {
39-
return $obj['pic']($args['width'], $args['height']);
40-
}
41-
],
42-
'recentArticle' => ['type' => function () use (&$BlogArticle) {
43-
return $BlogArticle;
44-
}]
45-
]
32+
'fields' => function() use (&$BlogArticle, &$BlogImage) {
33+
return [
34+
'id' => ['type' => Type::string()],
35+
'name' => ['type' => Type::string()],
36+
'pic' => [
37+
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
38+
'type' => $BlogImage,
39+
'resolve' => function ($obj, $args) {
40+
return $obj['pic']($args['width'], $args['height']);
41+
}
42+
],
43+
'recentArticle' => $BlogArticle
44+
];
45+
}
4646
]);
4747

4848
$BlogArticle = new ObjectType([

tests/Executor/ExecutorTest.php

+38-36
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,25 @@ public function testExecutesArbitraryCode()
116116
$deepDataType = null;
117117
$dataType = new ObjectType([
118118
'name' => 'DataType',
119-
'fields' => [
120-
'a' => [ 'type' => Type::string() ],
121-
'b' => [ 'type' => Type::string() ],
122-
'c' => [ 'type' => Type::string() ],
123-
'd' => [ 'type' => Type::string() ],
124-
'e' => [ 'type' => Type::string() ],
125-
'f' => [ 'type' => Type::string() ],
126-
'pic' => [
127-
'args' => [ 'size' => ['type' => Type::int() ] ],
128-
'type' => Type::string(),
129-
'resolve' => function($obj, $args) {
130-
return $obj['pic']($args['size']);
131-
}
132-
],
133-
'promise' => ['type' => function() use (&$dataType) {return $dataType;}],
134-
'deep' => [ 'type' => function() use(&$deepDataType) {return $deepDataType; }],
135-
]
119+
'fields' => function() use (&$dataType, &$deepDataType) {
120+
return [
121+
'a' => [ 'type' => Type::string() ],
122+
'b' => [ 'type' => Type::string() ],
123+
'c' => [ 'type' => Type::string() ],
124+
'd' => [ 'type' => Type::string() ],
125+
'e' => [ 'type' => Type::string() ],
126+
'f' => [ 'type' => Type::string() ],
127+
'pic' => [
128+
'args' => [ 'size' => ['type' => Type::int() ] ],
129+
'type' => Type::string(),
130+
'resolve' => function($obj, $args) {
131+
return $obj['pic']($args['size']);
132+
}
133+
],
134+
'promise' => ['type' => $dataType],
135+
'deep' => ['type' => $deepDataType],
136+
];
137+
}
136138
]);
137139

138140
$deepDataType = new ObjectType([
@@ -170,25 +172,25 @@ public function testMergesParallelFragments()
170172

171173
$Type = new ObjectType([
172174
'name' => 'Type',
173-
'fields' => [
174-
'a' => ['type' => Type::string(), 'resolve' => function () {
175-
return 'Apple';
176-
}],
177-
'b' => ['type' => Type::string(), 'resolve' => function () {
178-
return 'Banana';
179-
}],
180-
'c' => ['type' => Type::string(), 'resolve' => function () {
181-
return 'Cherry';
182-
}],
183-
'deep' => [
184-
'type' => function () use (&$Type) {
185-
return $Type;
186-
},
187-
'resolve' => function () {
188-
return [];
189-
}
190-
]
191-
]
175+
'fields' => function() use (&$Type) {
176+
return [
177+
'a' => ['type' => Type::string(), 'resolve' => function () {
178+
return 'Apple';
179+
}],
180+
'b' => ['type' => Type::string(), 'resolve' => function () {
181+
return 'Banana';
182+
}],
183+
'c' => ['type' => Type::string(), 'resolve' => function () {
184+
return 'Cherry';
185+
}],
186+
'deep' => [
187+
'type' => $Type,
188+
'resolve' => function () {
189+
return [];
190+
}
191+
]
192+
];
193+
}
192194
]);
193195
$schema = new Schema(['query' => $Type]);
194196
$expected = [

tests/Executor/NonNullTest.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ public function setUp()
5858

5959
$dataType = new ObjectType([
6060
'name' => 'DataType',
61-
'fields' => [
62-
'sync' => ['type' => Type::string()],
63-
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
64-
'nest' => ['type' => function () use (&$dataType) {
65-
return $dataType;
66-
}],
67-
'nonNullNest' => ['type' => function () use (&$dataType) {
68-
return Type::nonNull($dataType);
69-
}]
70-
]
61+
'fields' => function() use (&$dataType) {
62+
return [
63+
'sync' => ['type' => Type::string()],
64+
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
65+
'nest' => $dataType,
66+
'nonNullNest' => Type::nonNull($dataType)
67+
];
68+
}
7169
]);
7270

7371
$this->schema = new Schema(['query' => $dataType]);

tests/Type/DefinitionTest.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ public function setUp()
9292

9393
$this->blogAuthor = new ObjectType([
9494
'name' => 'Author',
95-
'fields' => [
96-
'id' => ['type' => Type::string()],
97-
'name' => ['type' => Type::string()],
98-
'pic' => [ 'type' => $this->blogImage, 'args' => [
99-
'width' => ['type' => Type::int()],
100-
'height' => ['type' => Type::int()]
101-
]],
102-
'recentArticle' => ['type' => function() {return $this->blogArticle;}],
103-
],
95+
'fields' => function() {
96+
return [
97+
'id' => ['type' => Type::string()],
98+
'name' => ['type' => Type::string()],
99+
'pic' => [ 'type' => $this->blogImage, 'args' => [
100+
'width' => ['type' => Type::int()],
101+
'height' => ['type' => Type::int()]
102+
]],
103+
'recentArticle' => $this->blogArticle,
104+
];
105+
},
104106
]);
105107

106108
$this->blogArticle = new ObjectType([

tests/Type/SchemaValidatorTest.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,11 @@ public function testRejectsASchemaThatUsesAnInputTypeAsAField()
398398
foreach ($kinds as $kind) {
399399
$someOutputType = new $kind([
400400
'name' => 'SomeOutputType',
401-
'fields' => [
402-
'sneaky' => ['type' => function() {return $this->someInputObjectType;}]
403-
]
401+
'fields' => function() {
402+
return [
403+
'sneaky' => $this->someInputObjectType
404+
];
405+
}
404406
]);
405407

406408
$schema = new Schema(['query' => $someOutputType]);
@@ -545,9 +547,11 @@ private function schemaWithFieldArgOfType($argType)
545547
{
546548
$someIncorrectInputType = new InputObjectType([
547549
'name' => 'SomeIncorrectInputType',
548-
'fields' => [
549-
'val' => ['type' => function() use ($argType) {return $argType;} ]
550-
]
550+
'fields' => function() use ($argType) {
551+
return [
552+
'val' => ['type' => $argType ]
553+
];
554+
}
551555
]);
552556

553557
$queryType = new ObjectType([

tests/Validator/QuerySecuritySchema.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@ public static function buildHumanType()
5959
self::$humanType = new ObjectType(
6060
[
6161
'name' => 'Human',
62-
'fields' => [
63-
'firstName' => ['type' => Type::nonNull(Type::string())],
64-
'dogs' => [
65-
'type' => function () {
66-
return Type::nonNull(
62+
'fields' => function() {
63+
return [
64+
'firstName' => ['type' => Type::nonNull(Type::string())],
65+
'dogs' => [
66+
'type' => Type::nonNull(
6767
Type::listOf(
6868
Type::nonNull(self::buildDogType())
6969
)
70-
);
71-
},
72-
'complexity' => function ($childrenComplexity, $args) {
73-
$complexity = isset($args['name']) ? 1 : 10;
74-
75-
return $childrenComplexity + $complexity;
76-
},
77-
'args' => ['name' => ['type' => Type::string()]],
78-
],
79-
],
70+
),
71+
'complexity' => function ($childrenComplexity, $args) {
72+
$complexity = isset($args['name']) ? 1 : 10;
73+
74+
return $childrenComplexity + $complexity;
75+
},
76+
'args' => ['name' => ['type' => Type::string()]],
77+
],
78+
];
79+
},
8080
]
8181
);
8282

tests/Validator/TestCase.php

+23-19
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ public static function getDefaultSchema()
9494
$Cat = new ObjectType([
9595
'name' => 'Cat',
9696
'isTypeOf' => function() {return true;},
97-
'fields' => [
98-
'name' => [
99-
'type' => Type::string(),
100-
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
101-
],
102-
'nickname' => ['type' => Type::string()],
103-
'meows' => ['type' => Type::boolean()],
104-
'meowVolume' => ['type' => Type::int()],
105-
'furColor' => ['type' => function() use (&$FurColor) {return $FurColor;}]
106-
],
97+
'fields' => function() use (&$FurColor) {
98+
return [
99+
'name' => [
100+
'type' => Type::string(),
101+
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
102+
],
103+
'nickname' => ['type' => Type::string()],
104+
'meows' => ['type' => Type::boolean()],
105+
'meowVolume' => ['type' => Type::int()],
106+
'furColor' => $FurColor
107+
];
108+
},
107109
'interfaces' => [$Being, $Pet]
108110
]);
109111

@@ -128,15 +130,17 @@ public static function getDefaultSchema()
128130
'name' => 'Human',
129131
'isTypeOf' => function() {return true;},
130132
'interfaces' => [$Being, $Intelligent],
131-
'fields' => [
132-
'name' => [
133-
'type' => Type::string(),
134-
'args' => ['surname' => ['type' => Type::boolean()]]
135-
],
136-
'pets' => ['type' => Type::listOf($Pet)],
137-
'relatives' => ['type' => function() use (&$Human) {return Type::listOf($Human); }],
138-
'iq' => ['type' => Type::int()]
139-
]
133+
'fields' => function() use (&$Human, $Pet) {
134+
return [
135+
'name' => [
136+
'type' => Type::string(),
137+
'args' => ['surname' => ['type' => Type::boolean()]]
138+
],
139+
'pets' => ['type' => Type::listOf($Pet)],
140+
'relatives' => ['type' => Type::listOf($Human)],
141+
'iq' => ['type' => Type::int()]
142+
];
143+
}
140144
]);
141145

142146
$Alien = new ObjectType([

0 commit comments

Comments
 (0)