|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Examples\Blog\Type; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Examples\Blog\AppContext; |
|
5
|
|
|
use GraphQL\Examples\Blog\Data\DataSource; |
|
6
|
|
|
use GraphQL\Examples\Blog\Types; |
|
7
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
9
|
|
|
use GraphQL\Type\Definition\Type; |
|
10
|
|
|
|
|
11
|
|
|
class QueryType extends ObjectType |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$config = [ |
|
16
|
|
|
'name' => 'Query', |
|
17
|
|
|
'fields' => [ |
|
18
|
|
|
'user' => [ |
|
19
|
|
|
'type' => Types::user(), |
|
20
|
|
|
'description' => 'Returns user by id (in range of 1-5)', |
|
21
|
|
|
'args' => [ |
|
22
|
|
|
'id' => Types::nonNull(Types::id()) |
|
23
|
|
|
] |
|
24
|
|
|
], |
|
25
|
|
|
'viewer' => [ |
|
26
|
|
|
'type' => Types::user(), |
|
27
|
|
|
'description' => 'Represents currently logged-in user (for the sake of example - simply returns user with id == 1)' |
|
28
|
|
|
], |
|
29
|
|
|
'stories' => [ |
|
30
|
|
|
'type' => Types::listOf(Types::story()), |
|
31
|
|
|
'description' => 'Returns subset of stories posted for this blog', |
|
32
|
|
|
'args' => [ |
|
33
|
|
|
'after' => [ |
|
34
|
|
|
'type' => Types::id(), |
|
35
|
|
|
'description' => 'Fetch stories listed after the story with this ID' |
|
36
|
|
|
], |
|
37
|
|
|
'limit' => [ |
|
38
|
|
|
'type' => Types::int(), |
|
39
|
|
|
'description' => 'Number of stories to be returned', |
|
40
|
|
|
'defaultValue' => 10 |
|
41
|
|
|
] |
|
42
|
|
|
] |
|
43
|
|
|
], |
|
44
|
|
|
'lastStoryPosted' => [ |
|
45
|
|
|
'type' => Types::story(), |
|
46
|
|
|
'description' => 'Returns last story posted for this blog' |
|
47
|
|
|
], |
|
48
|
|
|
'deprecatedField' => [ |
|
49
|
|
|
'type' => Types::string(), |
|
50
|
|
|
'deprecationReason' => 'This field is deprecated!' |
|
51
|
|
|
], |
|
52
|
|
|
'fieldWithException' => [ |
|
53
|
|
|
'type' => Types::string(), |
|
54
|
|
|
'resolve' => function() { |
|
55
|
|
|
throw new \Exception("Exception message thrown in field resolver"); |
|
56
|
|
|
} |
|
57
|
|
|
], |
|
58
|
|
|
'hello' => Type::string() |
|
59
|
|
|
], |
|
60
|
|
|
'resolveField' => function($rootValue, $args, $context, ResolveInfo $info) { |
|
61
|
|
|
return $this->{$info->fieldName}($rootValue, $args, $context, $info); |
|
62
|
|
|
} |
|
63
|
|
|
]; |
|
64
|
|
|
parent::__construct($config); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function user($rootValue, $args) |
|
68
|
|
|
{ |
|
69
|
|
|
return DataSource::findUser($args['id']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function viewer($rootValue, $args, AppContext $context) |
|
73
|
|
|
{ |
|
74
|
|
|
return $context->viewer; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function stories($rootValue, $args) |
|
78
|
|
|
{ |
|
79
|
|
|
$args += ['after' => null]; |
|
80
|
|
|
return DataSource::findStories($args['limit'], $args['after']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function lastStoryPosted() |
|
84
|
|
|
{ |
|
85
|
|
|
return DataSource::findLatestStory(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function hello() |
|
89
|
|
|
{ |
|
90
|
|
|
return 'Your graphql-php endpoint is ready! Use GraphiQL to browse API'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function deprecatedField() |
|
94
|
|
|
{ |
|
95
|
|
|
return 'You can request deprecated field, but it is not displayed in auto-generated documentation by default.'; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|