1
|
|
|
<?php |
2
|
|
|
namespace GraphQL\Examples\Blog; |
3
|
|
|
|
4
|
|
|
use GraphQL\Examples\Blog\Type\CommentType; |
5
|
|
|
use GraphQL\Examples\Blog\Type\Enum\ContentFormatEnum; |
6
|
|
|
use GraphQL\Examples\Blog\Type\Enum\ImageSizeEnumType; |
7
|
|
|
use GraphQL\Examples\Blog\Type\Field\HtmlField; |
8
|
|
|
use GraphQL\Examples\Blog\Type\SearchResultType; |
9
|
|
|
use GraphQL\Examples\Blog\Type\NodeType; |
10
|
|
|
use GraphQL\Examples\Blog\Type\QueryType; |
11
|
|
|
use GraphQL\Examples\Blog\Type\Scalar\EmailType; |
12
|
|
|
use GraphQL\Examples\Blog\Type\StoryType; |
13
|
|
|
use GraphQL\Examples\Blog\Type\Scalar\UrlType; |
14
|
|
|
use GraphQL\Examples\Blog\Type\UserType; |
15
|
|
|
use GraphQL\Examples\Blog\Type\ImageType; |
16
|
|
|
use GraphQL\Type\Definition\ListOfType; |
17
|
|
|
use GraphQL\Type\Definition\NonNull; |
18
|
|
|
use GraphQL\Type\Definition\Type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Acts as a registry and factory for your types. |
22
|
|
|
* |
23
|
|
|
* As simplistic as possible for the sake of clarity of this example. |
24
|
|
|
* Your own may be more dynamic (or even code-generated). |
25
|
|
|
*/ |
26
|
|
|
class Types |
27
|
|
|
{ |
28
|
|
|
// Object types: |
29
|
|
|
private static $user; |
30
|
|
|
private static $story; |
31
|
|
|
private static $comment; |
32
|
|
|
private static $image; |
33
|
|
|
private static $query; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return UserType |
37
|
|
|
*/ |
38
|
|
|
public static function user() |
39
|
|
|
{ |
40
|
|
|
return self::$user ?: (self::$user = new UserType()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return StoryType |
45
|
|
|
*/ |
46
|
|
|
public static function story() |
47
|
|
|
{ |
48
|
|
|
return self::$story ?: (self::$story = new StoryType()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return CommentType |
53
|
|
|
*/ |
54
|
|
|
public static function comment() |
55
|
|
|
{ |
56
|
|
|
return self::$comment ?: (self::$comment = new CommentType()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ImageType |
61
|
|
|
*/ |
62
|
|
|
public static function image() |
63
|
|
|
{ |
64
|
|
|
return self::$image ?: (self::$image = new ImageType()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return QueryType |
69
|
|
|
*/ |
70
|
|
|
public static function query() |
71
|
|
|
{ |
72
|
|
|
return self::$query ?: (self::$query = new QueryType()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
// Interface types |
77
|
|
|
private static $node; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return NodeType |
81
|
|
|
*/ |
82
|
|
|
public static function node() |
83
|
|
|
{ |
84
|
|
|
return self::$node ?: (self::$node = new NodeType()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
// Unions types: |
89
|
|
|
private static $mention; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return SearchResultType |
93
|
|
|
*/ |
94
|
|
|
public static function mention() |
95
|
|
|
{ |
96
|
|
|
return self::$mention ?: (self::$mention = new SearchResultType()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
// Enum types |
101
|
|
|
private static $imageSizeEnum; |
102
|
|
|
private static $contentFormatEnum; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ImageSizeEnumType |
106
|
|
|
*/ |
107
|
|
|
public static function imageSizeEnum() |
108
|
|
|
{ |
109
|
|
|
return self::$imageSizeEnum ?: (self::$imageSizeEnum = new ImageSizeEnumType()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return ContentFormatEnum |
114
|
|
|
*/ |
115
|
|
|
public static function contentFormatEnum() |
116
|
|
|
{ |
117
|
|
|
return self::$contentFormatEnum ?: (self::$contentFormatEnum = new ContentFormatEnum()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Custom Scalar types: |
121
|
|
|
private static $urlType; |
122
|
|
|
private static $emailType; |
123
|
|
|
|
124
|
|
|
public static function email() |
125
|
|
|
{ |
126
|
|
|
return self::$emailType ?: (self::$emailType = EmailType::create()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return UrlType |
131
|
|
|
*/ |
132
|
|
|
public static function url() |
133
|
|
|
{ |
134
|
|
|
return self::$urlType ?: (self::$urlType = new UrlType()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $name |
139
|
|
|
* @param null $objectKey |
|
|
|
|
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public static function htmlField($name, $objectKey = null) |
143
|
|
|
{ |
144
|
|
|
return HtmlField::build($name, $objectKey); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
// Let's add internal types as well for consistent experience |
150
|
|
|
|
151
|
|
|
public static function boolean() |
152
|
|
|
{ |
153
|
|
|
return Type::boolean(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \GraphQL\Type\Definition\FloatType |
158
|
|
|
*/ |
159
|
|
|
public static function float() |
160
|
|
|
{ |
161
|
|
|
return Type::float(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return \GraphQL\Type\Definition\IDType |
166
|
|
|
*/ |
167
|
|
|
public static function id() |
168
|
|
|
{ |
169
|
|
|
return Type::id(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return \GraphQL\Type\Definition\IntType |
174
|
|
|
*/ |
175
|
|
|
public static function int() |
176
|
|
|
{ |
177
|
|
|
return Type::int(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return \GraphQL\Type\Definition\StringType |
182
|
|
|
*/ |
183
|
|
|
public static function string() |
184
|
|
|
{ |
185
|
|
|
return Type::string(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param Type $type |
190
|
|
|
* @return ListOfType |
191
|
|
|
*/ |
192
|
|
|
public static function listOf($type) |
193
|
|
|
{ |
194
|
|
|
return new ListOfType($type); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param Type $type |
199
|
|
|
* @return NonNull |
200
|
|
|
*/ |
201
|
|
|
public static function nonNull($type) |
202
|
|
|
{ |
203
|
|
|
return new NonNull($type); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|