|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BlogSchema.php |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Examples\Blog\Schema; |
|
7
|
|
|
|
|
8
|
|
|
use Youshido\GraphQL\Config\Schema\SchemaConfig; |
|
9
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
10
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
|
11
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
|
12
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
13
|
|
|
|
|
14
|
|
|
class BlogSchema extends AbstractSchema |
|
15
|
|
|
{ |
|
16
|
|
|
public function build(SchemaConfig $config) |
|
17
|
|
|
{ |
|
18
|
|
|
$config->getQuery()->addFields([ |
|
19
|
|
|
'latestPost' => [ |
|
20
|
|
|
'type' => new PostType(), |
|
21
|
|
|
'resolve' => function ($value, array $args, ResolveInfo $info) { |
|
22
|
|
|
return $info->getReturnType()->getOne(empty($args['id']) ? 1 : $args['id']); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
], |
|
25
|
|
|
'randomBanner' => [ |
|
26
|
|
|
'type' => new BannerType(), |
|
27
|
|
|
'resolve' => function () { |
|
28
|
|
|
return DataProvider::getBanner(rand(1, 10)); |
|
29
|
|
|
} |
|
30
|
|
|
], |
|
31
|
|
|
'pageContentUnion' => [ |
|
32
|
|
|
'type' => new ListType(new ContentBlockUnion()), |
|
33
|
|
|
'resolve' => function () { |
|
34
|
|
|
return [DataProvider::getPost(1), DataProvider::getBanner(1)]; |
|
35
|
|
|
} |
|
36
|
|
|
], |
|
37
|
|
|
'pageContentInterface' => [ |
|
38
|
|
|
'type' => new ListType(new ContentBlockInterface()), |
|
39
|
|
|
'resolve' => function () { |
|
40
|
|
|
return [DataProvider::getPost(2), DataProvider::getBanner(3)]; |
|
41
|
|
|
} |
|
42
|
|
|
] |
|
43
|
|
|
]); |
|
44
|
|
|
$config->getMutation()->addFields([ |
|
45
|
|
|
new LikePostField(), |
|
46
|
|
|
'createPost' => [ |
|
47
|
|
|
'type' => new PostType(), |
|
48
|
|
|
'args' => [ |
|
49
|
|
|
'post' => new PostInputType(), |
|
50
|
|
|
'author' => new StringType() |
|
51
|
|
|
], |
|
52
|
|
|
'resolve' => function ($value, array $args, ResolveInfo $info) { |
|
|
|
|
|
|
53
|
|
|
// code for creating a new post goes here |
|
54
|
|
|
// we simple use our DataProvider for now |
|
55
|
|
|
$post = DataProvider::getPost(10); |
|
56
|
|
|
if (!empty($args['post']['title'])) $post['title'] = $args['post']['title']; |
|
57
|
|
|
|
|
58
|
|
|
return $post; |
|
59
|
|
|
} |
|
60
|
|
|
] |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: