-
Notifications
You must be signed in to change notification settings - Fork 22
/
Schema.php
266 lines (246 loc) · 8.34 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace Mohiohio\GraphQLWP;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ListOfType;
use GraphQLRelay\Relay;
use Mohiohio\GraphQLWP\Type\Definition\WPQuery;
use Mohiohio\GraphQLWP\Type\Definition\WPPost;
use Mohiohio\GraphQLWP\Type\Definition\WPTerm;
use Mohiohio\GraphQLWP\Type\Definition\Post;
use Mohiohio\GraphQLWP\Type\Definition\Page;
use Mohiohio\GraphQLWP\Type\Definition\Attachment;
use Mohiohio\GraphQLWP\Type\Definition\Product;
use Mohiohio\GraphQLWP\Type\Definition\Order;
use Mohiohio\GraphQLWP\Type\Definition\Category;
use Mohiohio\GraphQLWP\Type\Definition\Tag;
use Mohiohio\GraphQLWP\Type\Definition\User;
use Mohiohio\GraphQLWP\Type\Definition\PostFormat;
// use Mohiohio\GraphQLWP\Type\Definition\PostInput;
use Mohiohio\GraphQLWP\Type\Definition\MenuItem;
use Mohiohio\GraphQLWP\Type\Definition\BlogInfo;
use Mohiohio\GraphQLWP\Mutations\Post as PostMutation;
use Mohiohio\GraphQLWP\Mutations\Term as TermMutation;
use Mohiohio\GraphQLWP\Mutations\Login;
use Mohiohio\GraphQLWP\Mutations\Register;
use Mohiohio\GraphQLWP\Mutations\RefreshToken;
use Mohiohio\GraphQLWP\Type\Definition\CurrentUser;
class Schema
{
static protected $query = null;
static protected $mutation = null;
static protected $nodeDefinition = null;
static function build()
{
// Add WooCommerce Schema if required
add_filter('graphql-wp/schema-types', function ($types) {
if (self::withWooCommerce()) {
return $types + [
Order::getInstance(),
Product::getInstance(),
] + $types;
}
return $types;
});
return new \GraphQL\Type\Schema([
'query' => static::getQuery(),
'mutation' => static::getMutation(),
'types' => apply_filters('graphql-wp/get_post_types', apply_filters('graphql-wp/schema-types', [
WPPost::getInstance(),
WPTerm::getInstance(),
Post::getInstance(),
Page::getInstance(),
Attachment::getInstance(),
Category::getInstance(),
Tag::getInstance(),
PostFormat::getInstance(),
User::getInstance()
]))
]);
}
static function withWooCommerce()
{
return (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))));
}
static function getNodeDefinition()
{
return static::$nodeDefinition ?: static::$nodeDefinition = Relay::nodeDefinitions(
function ($globalID) {
$idComponents = Relay::fromGlobalId($globalID);
switch ($idComponents['type']) {
case WPPost::TYPE;
return get_post($idComponents['id']);
case WPTerm::TYPE;
return get_term($idComponents['id']);
case User::TYPE;
return get_user_by('id', $idComponents['id']);
// case WPQuery::TYPE;
// global $wp_query;
// return $wp_query;
default;
return null;
}
},
function ($obj, $context, $info) {
if ($obj instanceof \WP_Post) {
return WPPost::resolveType($obj, $context, $info);
}
if ($obj instanceof \WP_Term) {
return WPTerm::resolveType($obj, $context, $info);
}
}
);
}
static function getQuery()
{
return static::$query ?: static::$query = new ObjectType(static::getQuerySchema());
}
static function getQuerySchema()
{
$schema = apply_filters('graphql-wp/get_query_schema', [
'name' => 'Query',
'fields' => function () {
return [
'id' => Relay::globalIdField('Query', function () {
return 0;
}),
'wp_query' => [
'type' => WPQuery::getInstance(),
'resolve' => function ($root, $args) {
global $wp_query;
return $wp_query;
}
],
'wp_post' => [
'type' => WPPost::getInstance(),
'args' => [
'ID' => [
'name' => 'ID',
'description' => 'ID of the post',
'type' => Type::int()
],
'slug' => [
'name' => 'slug',
'description' => 'Name of the post',
'type' => Type::string()
],
'post_type' => [
'name' => 'post_type',
'description' => 'Type of the post',
'type' => Type::string()
],
'preview' => [
'name' => 'preview',
'description' => 'Pass true to show preview post, only available in conjunction with ID param',
'type' => Type::boolean()
]
],
'resolve' => function ($root, $args) {
if (isset($args['ID'])) {
if (isset($args['preview']) && $args['preview']) {
$get_posts = new \WP_Query;
// Ref https://stackoverflow.com/questions/21544161/wordpress-query-for-preview
$queryArgs = [
'post_status' => 'any',
'post_parent' => $args['ID'],
'post_type' => 'revision',
'sort_column' => 'ID',
'sort_order' => 'desc',
'posts_per_page' => 1
];
$posts = $get_posts->query($queryArgs);
if (!empty($posts)) {
return $posts[0];
}
}
return get_post($args['ID']);
}
return get_page_by_path($args['slug'], 'OBJECT', isset($args['post_type']) ? $args['post_type'] : WPPost::DEFAULT_TYPE);
}
],
'term' => [
'type' => WPTerm::getInstance(),
'args' => [
'id' => [
'type' => Type::string(),
'description' => 'Term id'
]
],
'resolve' => function ($root, $args) {
return get_term($args['id']);
}
],
'menu' => [
'type' => new ListOfType(MenuItem::getInstance()),
'args' => [
'name' => [
'type' => Type::nonNull(Type::string()),
'description' => "Menu 'id','name' or 'slug'"
]
],
'resolve' => function ($root, $args) {
return wp_get_nav_menu_items($args['name']) ?: [];
}
],
'bloginfo' => [
'type' => BlogInfo::getInstance(),
'resolve' => function ($root, $args) {
return isset($args['filter']) ? $args['filter'] : 'raw';
}
],
'home_page' => [
'type' => WPPost::getInstance(),
'resolve' => function () {
return get_post(get_option('page_on_front'));
}
],
'terms' => [
'type' => WPTerm::getConnectionInstance(),
'description' => 'Retrieve the terms in a given taxonomy or list of taxonomies. ',
'args' => WPTerm::getArgs(),
'resolve' => function ($root, $args) {
return WPTerm::resolve($root, $args);
}
],
'current_user' => [
'type' => CurrentUser::getInstance(),
'resolve' => function () {
$user = wp_get_current_user();
if ($user->ID) {
return $user;
}
}
],
'query' => [
'type' => static::getQuery(),
'description' => 'Query node one level deep, makes working with Relay easier',
'resolve' => function () {
return static::getQuery();
}
],
'node' => static::getNodeDefinition()['nodeField'],
];
}
]);
return $schema;
}
static function getMutation()
{
return static::$mutation ?: static::$mutation = new ObjectType(static::getMutationSchema());
}
static function getMutationSchema()
{
return apply_filters('graphql-wp/get_mutation_schema', [
'name' => 'Mutation',
'fields' => function () {
return [
'save_post' => PostMutation::init(),
'save_term' => TermMutation::init(),
'login' => Login::init(),
'register' => Register::init(),
'refresh_token' => RefreshToken::init()
];
}
]);
}
}