Skip to content

Commit 0969073

Browse files
committed
Reverted DefinitionContainer (YAGNI)
1 parent 7c0aa4c commit 0969073

24 files changed

+49
-185
lines changed

docs/type-system/index.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $myType = new ObjectType([
2626
]);
2727
```
2828

29-
Class per type, using inheritance:
29+
Class per type:
3030
```php
3131
<?php
3232
namespace MyApp;
@@ -50,31 +50,6 @@ class MyType extends ObjectType
5050
}
5151
```
5252

53-
Class per type, using composition:
54-
```php
55-
<?php
56-
namespace MyApp;
57-
58-
use GraphQL\Type\Definition\ObjectType;
59-
use GraphQL\Type\Definition\Type;
60-
use GraphQL\Type\DefinitionContainer;
61-
62-
class MyType implements DefinitionContainer
63-
{
64-
private $definition;
65-
66-
public function getDefinition()
67-
{
68-
return $this->definition ?: ($this->definition = new ObjectType([
69-
'name' => 'MyType',
70-
'fields' => [
71-
'id' => Type::id()
72-
]
73-
]));
74-
}
75-
}
76-
```
77-
7853
You can also mix-and-match styles for convenience. For example:
7954
```php
8055
<?php

docs/type-system/scalar-types.md

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ or hardcoded in source code) to **serialized** representation included in respon
3535
Those cases are covered by methods `serialize`, `parseValue` and `parseLiteral` of abstract `ScalarType`
3636
class respectively.
3737

38-
Here is an example of simple `Email` type (using inheritance):
38+
Here is an example of simple `Email` type:
3939

4040
```php
4141
<?php
@@ -109,30 +109,6 @@ class EmailType extends ScalarType
109109
}
110110
```
111111

112-
Same example, using composition over inheritance:
113-
```php
114-
<?php
115-
namespace MyApp;
116-
117-
use GraphQL\Type\DefinitionContainer;
118-
use GraphQL\Type\Definition\CustomScalarType;
119-
120-
class EmailType implements DefinitionContainer
121-
{
122-
private $definition;
123-
124-
public function getDefinition()
125-
{
126-
return $this->definition ?: ($this->definition = new CustomScalarType([
127-
'name' => 'Email',
128-
'serialize' => function($value) {/* See function body above */},
129-
'parseValue' => function($value) {/* See function body above */},
130-
'parseLiteral' => function($valueNode) {/* See function body above */},
131-
]));
132-
}
133-
}
134-
```
135-
136112
Or with inline style:
137113

138114
```php

examples/01-blog/Blog/Type/BaseType.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/01-blog/Blog/Type/CommentType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
use GraphQL\Type\Definition\ObjectType;
99
use GraphQL\Type\Definition\ResolveInfo;
1010

11-
class CommentType extends BaseType
11+
class CommentType extends ObjectType
1212
{
1313
public function __construct()
1414
{
15-
// Option #1: using composition over inheritance to define type, see ImageType for inheritance example
16-
$this->definition = new ObjectType([
15+
$config = [
1716
'name' => 'Comment',
1817
'fields' => function() {
1918
return [
@@ -43,7 +42,8 @@ public function __construct()
4342
return $value->{$info->fieldName};
4443
}
4544
}
46-
]);
45+
];
46+
parent::__construct($config);
4747
}
4848

4949
public function author(Comment $comment)
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<?php
22
namespace GraphQL\Examples\Blog\Type\Enum;
33

4-
use GraphQL\Examples\Blog\Type\BaseType;
54
use GraphQL\Type\Definition\EnumType;
65

7-
class ContentFormatEnum extends BaseType
6+
class ContentFormatEnum extends EnumType
87
{
98
const FORMAT_TEXT = 'TEXT';
109
const FORMAT_HTML = 'HTML';
1110

1211
public function __construct()
1312
{
14-
// Option #1: Define type using composition over inheritance, see ImageSizeEnumType for inheritance option
15-
$this->definition = new EnumType([
13+
$config = [
1614
'name' => 'ContentFormatEnum',
1715
'values' => [self::FORMAT_TEXT, self::FORMAT_HTML]
18-
]);
16+
];
17+
parent::__construct($config);
1918
}
2019
}

examples/01-blog/Blog/Type/Enum/ImageSizeEnumType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class ImageSizeEnumType extends EnumType
88
{
99
public function __construct()
1010
{
11-
// Option #2: Define enum type using inheritance
1211
$config = [
1312
// Note: 'name' option is not needed in this form - it will be inferred from className
1413
'values' => [

examples/01-blog/Blog/Type/ImageType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class ImageType extends ObjectType
1111
{
1212
public function __construct()
1313
{
14-
// Option #2: define type using inheritance, see any other object type for compositional example
1514
$config = [
1615
'name' => 'ImageType',
1716
'fields' => [

examples/01-blog/Blog/Type/NodeType.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
use GraphQL\Examples\Blog\Types;
88
use GraphQL\Type\Definition\InterfaceType;
99

10-
class NodeType extends BaseType
10+
class NodeType extends InterfaceType
1111
{
1212
public function __construct()
1313
{
14-
// Option #1: using composition over inheritance to define type, see ImageType for inheritance example
15-
$this->definition = new InterfaceType([
14+
$config = [
1615
'name' => 'Node',
1716
'fields' => [
1817
'id' => Types::id()
1918
],
20-
'resolveType' => [$this, 'resolveType']
21-
]);
19+
'resolveType' => [$this, 'resolveNodeType']
20+
];
21+
parent::__construct($config);
2222
}
2323

24-
public function resolveType($object, Types $types)
24+
public function resolveNodeType($object)
2525
{
2626
if ($object instanceof User) {
2727
return Types::user();

examples/01-blog/Blog/Type/QueryType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
use GraphQL\Type\Definition\ResolveInfo;
99
use GraphQL\Type\Definition\Type;
1010

11-
class QueryType extends BaseType
11+
class QueryType extends ObjectType
1212
{
1313
public function __construct()
1414
{
15-
// Option #1: using composition over inheritance to define type, see ImageType for inheritance example
16-
$this->definition = new ObjectType([
15+
$config = [
1716
'name' => 'Query',
1817
'fields' => [
1918
'user' => [
@@ -61,7 +60,8 @@ public function __construct()
6160
'resolveField' => function($val, $args, $context, ResolveInfo $info) {
6261
return $this->{$info->fieldName}($val, $args, $context, $info);
6362
}
64-
]);
63+
];
64+
parent::__construct($config);
6565
}
6666

6767
public function user($rootValue, $args)

examples/01-blog/Blog/Type/Scalar/EmailType.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22
namespace GraphQL\Examples\Blog\Type\Scalar;
33

44
use GraphQL\Error\Error;
5-
use GraphQL\Examples\Blog\Type\BaseType;
65
use GraphQL\Language\AST\StringValueNode;
76
use GraphQL\Type\Definition\CustomScalarType;
87
use GraphQL\Utils;
98

10-
class EmailType extends BaseType
9+
class EmailType
1110
{
12-
public function __construct()
11+
public static function create()
1312
{
14-
// Option #1: define scalar types using composition (see UrlType fo option #2 using inheritance)
15-
$this->definition = new CustomScalarType([
13+
return new CustomScalarType([
1614
'name' => 'Email',
17-
'serialize' => [$this, 'serialize'],
18-
'parseValue' => [$this, 'parseValue'],
19-
'parseLiteral' => [$this, 'parseLiteral'],
15+
'serialize' => [__CLASS__, 'serialize'],
16+
'parseValue' => [__CLASS__, 'parseValue'],
17+
'parseLiteral' => [__CLASS__, 'parseLiteral'],
2018
]);
2119
}
2220

@@ -26,7 +24,7 @@ public function __construct()
2624
* @param string $value
2725
* @return string
2826
*/
29-
public function serialize($value)
27+
public static function serialize($value)
3028
{
3129
// Assuming internal representation of email is always correct:
3230
return $value;
@@ -42,7 +40,7 @@ public function serialize($value)
4240
* @param mixed $value
4341
* @return mixed
4442
*/
45-
public function parseValue($value)
43+
public static function parseValue($value)
4644
{
4745
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
4846
throw new \UnexpectedValueException("Cannot represent value as email: " . Utils::printSafe($value));
@@ -57,7 +55,7 @@ public function parseValue($value)
5755
* @return string
5856
* @throws Error
5957
*/
60-
public function parseLiteral($valueNode)
58+
public static function parseLiteral($valueNode)
6159
{
6260
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
6361
// error location in query:

0 commit comments

Comments
 (0)